当前位置:网站首页>Laravel8 uses passport authentication to log in and generate a token

Laravel8 uses passport authentication to log in and generate a token

2022-07-06 23:24:00 Elante pace

install passport

composer require laravel/passport

Execute the migration command

php artisan migrate 

Create encryption keys required to generate security access tokens

php artisan passport:install

App\Models\ApiUser lead Trait

<?php
    
namespace App\Models;
    
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as AuthUser;
    
class ApiUser extends AuthUser
{
    use HasApiTokens, Notifiable;
}   

App\Providers\AuthServiceProvider

Let go of   'App\Models\Model' => 'App\Policies\ModelPolicy' Notes

boot Next  Passport::routes();

<?php
    
namespace App\Providers;
    
use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
    
class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Models\Model' => 'App\Policies\ModelPolicy',
    ];
    
    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();
    
        Passport::routes();

    //token The period of validity of the certification     
    Passport::tokensExpireIn(now()->addHour(2));
    // Refresh token The period of validity of the certification 
    Passport::refreshTokensExpireIn(now()->addDays(30));
    Passport::personalAccessTokensExpireIn(now()->addMonths(6));

    }

stay config/auth.php Middle configuration

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'apiweb'=>[

        'driver' => 'session',
        'provider' => 'apiusers',

     ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'apiusers',
        // 'hash' => false,
    ],
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],
    'apiusers' => [
        'driver' => 'eloquent',
        'model' => \App\Models\ApiUser::class,
    ],
]

 App\Http\Controllers\Api\LoginController

public function login(Request $request)
{
    try {
        $this->validate($request,[
           'username'=>'required',
           'password'=>'required'
        ]);
    } catch (\Exception $e){
        return response()->json(['code'=>403,'msg'=>' Account number or password cannot be empty ','data'=>[]]);
    }
    //  Verify account and password 
    $bool = auth()->guard('apiweb')->attempt($request->except('/api/login'));
    if($bool)
    {
        $user = auth()->guard('apiweb')->user();
        if($user->click > env('APINUM'))
        {
            return response()->json(['code'=>405,'msg'=>' The number of requests on that day has reached the maximum ','data'=>[]]);
        }
        $token = $user->createToken('api')->accessToken;
        $user->increment('click');
        $data = [
            'expire'=>3600*24*30,
            'token'=>$token
        ];
        return response()->json(['code'=>400,'msg'=>' Generate token success ','data'=>$data]);
    }else{
        return response()->json(['code'=>403,'msg'=>' Incorrect account or password ','data'=>[]]);
    }
}
原网站

版权声明
本文为[Elante pace]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131037242003.html

随机推荐