Coder Social home page Coder Social logo

arodu / utilcake Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 13 KB

UtilCake plugin for CakePHP 4.x

Home Page: https://packagist.org/packages/arodu/utilcake

License: MIT License

PHP 100.00%
cakephp cakephp4 utilities plugin recaptcha-v3 composer utilcake-plugin google-login

utilcake's Introduction

UtilCake plugin for CakePHP 4.x

CakePHP Plugin, collection of utilities for CakePHP 4.x

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require arodu/utilcake

Configuration

You can load the plugin using the shell command:

bin/cake plugin load UtilCake

Or you can manually add the loading statement in the src/Application.php file of your application:

public function bootstrap(){
    parent::bootstrap();
    $this->addPlugin('UtilCake');
}

How to use

Google reCaptcha V3

In the controller file

public function initialize(): void{
  parent::initialize();
  $this->loadComponent('UtilCake.ReCaptcha', [
    'public_key'=>'RECAPTCHA_PUBLIC_KEY',
    'secret_key'=>'RECAPTCHA_SECRET_KEY',
  ]);
}

// any action
public function action(){
  // ...
  if ($this->request->is('post')) {
    if($this->ReCaptcha->verify($this->request->getData())){
      // when the verification is successful
      // ...
    }else{
      // when the verification is not successful
      $this->Flash->error(__('reCaptcha failed, try again'));
    }
  }
  // ...
}

In the template templates/ControllerName/action.php

echo $this->Form->create(null, ['id'=>'form-id']);
  // ...
echo $this->Form->end();

$this->ReCaptcha->script('#form-id');

Google login

with AuthenticationComponent

In the login template, ex:/templates/Users/login.php

echo $this->GoogleLogin->link(__('Sign in with Google'),
  ['class' => 'btn btn-block btn-danger', 'escape' => false]
);

In the controller file, ex:/Controller/UsersController.php

public function initialize(): void{
  parent::initialize();
  $this->loadComponent('UtilCake.GoogleLogin', [
    'client_id' => GOOGLE_CLIENT_ID,
    'client_secret' => GOOGLE_CLIENT_SECRET,
    'redirect_uri' => Router::url([
        'controller' => 'Users',
        'action' => 'googleLogin',
        'prefix' => false,
        '_full' => true
      ]),
  ]);
}

//...

public function googleLogin() {
  if (!empty($this->request->getQuery('code'))) {
    try {
      $data = $this->GoogleLogin->getAccessToken($this->request->getQuery('code'));
      $user_profile_info = $this->GoogleLogin->getUserProfileInfo($data['access_token']);

      $user = $this->Users->find()
        ->where(['Users.email' => $user_profile_info['email']])
        ->first();

      if ($user) {
        $this->Authentication->setIdentity($user);
        $target = $this->Authentication->getLoginRedirect() ?? '/';
        return $this->redirect($target);
      }

      $this->Flash->error('Invalid google login');
    } catch (\Exception $e) {
      throw new NotFoundException($e->getMessage());
    }
  }

  return $this->redirect(['action' => 'login']);
}

utilcake's People

Contributors

arodu avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.