PHP code example of fx / hyperf-http-auth

1. Go to this page and download the library: Download fx/hyperf-http-auth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fx / hyperf-http-auth example snippets



declare (strict_types=1);

namespace App\Model;

use Fx\HyperfHttpAuth\Contract\Authenticatable;
use Hyperf\DbConnection\Model\Model;

class User extends Model implements Authenticatable
{
    use \Fx\HyperfHttpAuth\Authenticatable;
}
bash
php bin/hyperf.php vendor:publish fx/hyperf-http-auth


declare(strict_types=1);

namespace App\Controller;

use App\Model\User;
use Fx\HyperfHttpAuth\Contract\HttpAuthContract;
use Hyperf\Di\Annotation\Inject;

class IndexController extends AbstractController
{
    /**
     * @Inject()
     * @var HttpAuthContract
     */
    protected $auth;

    public function index()
    {
        return $this->data();
    }

    /**
     * 登录
     */
    public function login()
    {
        /** 方式 1 */
        // 等价于 auth()->login(User::first());
        $this->auth->login(User::first());

        /** 方式 2 */
        // 等价于 auth()->attempt(['email' => 'xxx', 'password' => '123456']);
        $this->auth->attempt(['email' => 'xxx', 'password' => '123456']);

        return $this->data();
    }

    /**
     * 登出
     */
    public function logout()
    {
        // 等价于 auth()->logout();
        $this->auth->logout();
        return $this->data();
    }

    protected function data()
    {
        return [
            'user' => auth()->user(),
            'is_login' => auth()->check(),
        ];
    }
}