6

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);
    }
}

and in 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');
});


});

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

BadMethodCallException Method Illuminate\Auth\RequestGuard::attempt does not exist.

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

4
  • I tried a similar way of authenticating both users but I failed, so instead I added an enum "type" field to my users table. Commented Mar 23, 2018 at 8:28
  • so there is no way of doing multiauth for api in laravel/passport? Commented Mar 23, 2018 at 8:29
  • check this repo out github.com/sfelix-martins/passport-multiauth Commented Mar 23, 2018 at 8:34
  • ok will check that Commented Mar 23, 2018 at 8:35

2 Answers 2

5

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

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',
    ],
],

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

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);
    }
}

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);
}

and in middleware

Route::group(['middleware' => 'auth:api'], function(){
 Route::post('admin/get-details', 'API\Admin\AdminController@getDetails');
});
Sign up to request clarification or add additional context in comments.

Comments

0

I managed to do it through middleware, hopefully, this will help you - Laravel Passport Multiple Authentication using Guards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.