rederlo / t-auth
该包的最新版本(dev-master)没有可用的许可证信息。
CakePHP认证插件
dev-master
2017-04-28 00:08 UTC
Requires
- php: >=5.5.9
- admad/cakephp-jwt-auth: ^2.0
- cakephp/cakephp: 3.3.*
- cakephp/plugin-installer: *
- firebase/php-jwt: ~4.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-20 19:10:29 UTC
README
在 config/bootstrap.php 中
//Enable Cors DispatcherFactory::add('TAuth.Cors', ['priority' => 1]); //Enable plugin Plugin::load('TAuth', ['bootstrap' => false, 'routes' => false]);
在 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()); }
获取用户 ID
public function getUserId() { return $this->getUser(); }
生成数据库
bin/cake migrations migrate --plugin TAuth
在表中生成数据
bin/cake migrations seed --seed <fileSeed> --plugin TAuth
新用户
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']); }
登录
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.')); } }
授权
/** * @param $user * @return bool */ public function isAuthorized($user) { return (new Authorize($this, $user['id']))->exec(); }
参数令牌
$auth = new AuthFactory(); $auth->create($users, ['group_id' => $group_id]);
GetParamsToken
/** * @return Array */ $this->getParamsHeader();