windwalker / authenticate
2.0.9
2015-07-27 10:12 UTC
Requires
- php: >=5.3.10
Requires (Dev)
- windwalker/test: ~2.0
README
通过 Composer 安装
将以下内容添加到您的 composer.json
文件的 require 块中。
{ "require": { "windwalker/authenticate": "~2.0" } }
入门指南
这是一个简单的登录认证过程。
public function login($username, $password) { $auth = new Authenticate; // Attach methods $auth->addMethod(new LocalMethod); $auth->addMethod(new MyMethod); $credential = new Credential; $credential->username = $username; $credential->password = $password; // Do authenticate $result = $auth->authenticate($credential); // False means login fail if (!$result) { // Print results to know what happened print_r($auth->getResults()); throw new Exception('Username or password not matched'); } $user = $auth->getCredential(); return $user; }
创建自定义方法
use Windwalker\Authenticate\Method\AbstractMethod; class MyMethod extends AbstractMethod { public function authenticate(Credential $credential) { $username = $credential->username; $password = $credential->password; if (!$username || !$password) { $this->status = Authenticate::EMPTY_CREDENTIAL; return false; } $user = Database::loadOne(array('username' => $username)); if (!$user) { $this->status = Authenticate::USER_NOT_FOUND; return false; } if (!password_verify($password, $user->password)) { $this->status = Authenticate::INVALID_CREDENTIAL; return false; } // Success $this->status = Authenticate::SUCCESS; // Set some data to Credential $credential->bind($user); unset($credential->password); return true; } }