如何在Laravel Passport上进行多重身份验证工作?

时间:2019-08-06 17:14:53

标签: laravel laravel-passport laravel-authorization laravel-authentication laravel-admin

例如,我在此网站上看到一个答案,该答案关于如何向Laravel Passport https://stackoverflow.com/a/49449524/5029058添加多重身份验证

但是我不明白该答案中用户如何成为管理员? 使用is_admin在数据库中是否有多余的行?还是有一张整张桌子供管理员使用? 在哪里尝试获取此信息以查看哪个用户是管理员,并被允许对api进行某些调用?

1 个答案:

答案 0 :(得分:0)

您可以通过多种方式进行管理,可以在用户表中设置属性“ is_admin”,也可以为管理员创建新表[我认为这更安全]。

要创建对管理员的身份验证

config \ auth.php

html = urllib2.urlopen(url).read()
doc = BeautifulSoup(html, "html.parser")
if not len(doc.find_all(attrs={"data-label":"All Games in Series"})) == 0 :
    return False
line = ""```
'guards' => [
  /* ... */
  // ** News guard **
  'admin' => [
    'driver' => 'passport',
    'provider' => 'admins',
  ],
],
'providers' => [
  /* ... */
  // ** News provider **
  'admins' => [
    'driver' => 'eloquent',
    'model' => App\Administrator::class,
  ],
],

管理员模型

'passwords' => [
  // ** News resettings **
  'admins' => [
    'provider' => 'admins',
    'table' => 'password_resets',
    'expire' => 60,
  ],
],

在控制器中创建此“ AUTH”很容易

<?php

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;

class Administrator extends Authenticatable
{
    use HasApiTokens, Notifiable;
    use SoftDeletes;
}

要在路由中进行身份验证,只需添加中间件

public function login()
{
        $user = Administrator::where("email", request('email'))->first();
        if(!isset($user)){
            return "Admin Not found";
        }
        if (!Hash::check(request('password'), $user->password)) {
            return "Incorrect password";
        } 
        $tokenResult = $user->createToken('Admin');
        $user->access_token = $tokenResult->accessToken;
        $user->token_type = 'Bearer';
        return $user;
}

要更改您的结果并且让未经身份验证的管理员进入Route::resource('admins', 'AdminController')->middleware('auth:admin');