rederlo/t-auth

There is no license information available for the latest version (dev-master) of this package.

Auth plugin for CakePHP

Maintainers

Details

github.com/rederlo/TAuth

Source

Issues

Installs: 13

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 0

Open Issues: 0

Type:cakephp-plugin

dev-master 2017-04-28 00:08 UTC

This package is not auto-updated.

Last update: 2022-05-20 08:08:31 UTC


README

in config/bootstrap.php

//Enable Cors

DispatcherFactory::add('TAuth.Cors', ['priority' => 1]);
//Enable plugin

Plugin::load('TAuth', ['bootstrap' => false, 'routes' => false]);

in src/AppController.php

use \TAuth\Controller\AuthTrait;
    public function initialize()
    {
        parent::initialize();
        $auth = new \TAuth\Controller\AuthBuilder([
            'model' => 'peoples',
            'scope_auth' => ['username' => 'email'],
            'scope_jwt' => ['username' => 'id']
        ]);
        
        $this->loadComponent('Auth', $auth->getConfig());
    }

is get user id

    public function getUserId()
    {
        return $this->getUser();
    }

Generate database

bin/cake migrations migrate --plugin TAuth

Generate data in tables

bin/cake migrations seed --seed <fileSeed> --plugin TAuth

New User

public function add()
{
    $users = $this->Users->newEntity();
    if($this->request->is('post')){
        $users = $this->Users->patchEntity($users, $this->request->data());
        if ($this->Users->save($users)) {
            $auth = new AuthFactory();
            //Expected Type Entity
            $auth->create($users);
            $token = $auth->build();
        }
    }
    $this->set(compact('token'));
    $this->set('_serialize', ['token']);
}

Login

public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error(__('Usernamme or password ínvalid, try again.'));
    }
}
    

Authorized

/**
* @param $user
* @return bool
*/
public function isAuthorized($user)
{
    return (new Authorize($this, $user['id']))->exec();
}

Params token

$auth = new AuthFactory();
$auth->create($users, ['group_id' => $group_id]);

GetParamsToken

/**
* @return Array
*/
$this->getParamsHeader();