如何在 laravel 护照中实现多身份验证

how to implement multiauth in laravel passport(如何在 laravel 护照中实现多身份验证)
本文介绍了如何在 laravel 护照中实现多身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个用户 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 tried in 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 中

and in api.php

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

Route::post('login', 'APIAdminAdminController@login')->name('admin.login');
Route::post('register', 'APIAdminAdminController@register')->name('admin.register');

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


});

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

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

BadMethodCallException方法 IlluminateAuthRequestGuard::attempt 不存在.

BadMethodCallException Method IlluminateAuthRequestGuard::attempt does not exist.

您能否分享您对如何在 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

用户控制器.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', 'APIAdminAdminController@getDetails');
});

这篇关于如何在 laravel 护照中实现多身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)