如何在Laravel护照中实现multiauth

如何在Laravel护照中实现multiauth

本文介绍了如何在Laravel护照中实现multiauth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个用户admin/user,我想对这两个用户进行api身份验证,它仅适用于一个用户,但不适用于管理员

I have two users admin/user i want to authenticate this two users for api,it is working for one user but its not working for admin

看看我尝试了什么在管理控制器中

see what i have triedin admin controller

public function login(Request $request){

//        $res=;
   // dd ($res);
     if(Auth::guard('admin')->attempt(['email' =>  $request->email, 'password' =>  $request->password]))
    {

  // if successful, then redirect to their intended location


        $user = auth()->guard('admin')->user();
        $success['token'] =  $user->createToken('admin')->accessToken;
        return response()->json(['success' => $success], $this->successStatus);
    }
    else{
        return response()->json(['error'=>'Unauthorised','email'=> $request->email,'password'=> $request->password], 401);
    }
}

和api.php中的

Route::prefix('admin')->group(function () {

Route::post('login', 'API\Admin\AdminController@login')->name('admin.login');
Route::post('register', 'API\Admin\AdminController@register')->name('admin.register');

Route::group(['middleware' => 'auth:admin-api'], function(){
 Route::post('get-details', 'API\Admin\AdminController@getDetails');
});


});

当我尝试为管理员调用try函数时,它给我错误

When i try to call attempt function for admin its giving me error

能否请您分享有关如何在laravel/passport中进行多重身份验证的想法

can you please share your ideas over how to do multiauth in laravel/passport

推荐答案

您可以在laravel护照中实现多重验证,您必须遵循以下步骤

you can implement multiauth in laravel passport you have to follow below steps

1)在我所创建的情况下创建4个警卫

1) create 4 guards in my situation i created like

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

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
     ],
     'admin-api' => [
        'driver' => 'passport',
        'provider' => 'admin',
    ],
     'admin' => [
        'driver' => 'session',
        'provider' => 'admin',
    ],
],

然后在控制器中记录该类型的用户时,请确保您使用的是sessio驱动程序防护之类的

then when logging that type of user in controller make sure you are using sessio driver guards like

usercontroller.php

usercontroller.php

 public function login(){
    if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
        $user = Auth::user();
        $success['token'] =  $user->createToken('MyApp')->accessToken;
        return response()->json(['success' => $success], $this->successStatus);
    }
    else{
        return response()->json(['error'=>'Unauthorised'], 401);
    }
}

在admincontroller.php中

in admincontroller.php

 public function login(){
    if(Auth::guard('admin')->attempt(['email' => request('email'), 'password' => request('password')])){
        $user = Auth::guard('admin')->user();
        $success['token'] =  $user->createToken('admin')->accessToken;
        return response()->json(['success' => $success], $this->successStatus);
    }
    else{
        return response()->json(['error'=>'Unauthorised'], 401);
    }
}

在检查令牌时,请使用护照司机护罩,例如

and when checking the token use the passport driver guard like

public function getDetails()
{
    $user = Auth::guard('admin-api')->user();
    return response()->json(['success' => $user], $this->successStatus);
}

和中间件

Route::group(['middleware' => 'auth:api'], function(){
 Route::post('admin/get-details', 'API\Admin\AdminController@getDetails');
});

这篇关于如何在Laravel护照中实现multiauth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 03:09