0

I am working on Laravel application that requires generating access token for multiple users. I am using Laravel Passport with Password Grant to issue token. I have followed the Laravel documentation but it seems I am doing something wrong as I got redirected to Login route when I try to call my API endpoint (Student) with Postman. I later found out that the redirection is coming from the middleware I created but I still don't know what went wrong

auth.php

        'guards' => [
                'web' => [
                    'driver' => 'session',
                    'provider' => 'users',
                ],
        
                'api' => [
                    'driver' => 'passport',
                    'provider' => 'users',
                    'hash' => false,
                ],
               
                'api-student' => [
                    'driver' => 'passport',
                    'provider' => 'students',
                    'hash' => false,
                ]
    ];
    
    
         'providers' => [
                'users' => [
                    'driver' => 'eloquent',
                    'model' => App\User::class,
                ],
                'students' => [
                    'driver' => 'eloquent',
                    'model' => App\Student::class,
                ],
                 
            ],

api.php

    Route::post('auth/getlogin', 'Api\StudentController@authenticate')->middleware('auth:api-student');
    
    //Api\StudentController
    public function authenticate(Request $request, Student $user)
        {
            $credentials = [
                'username'=>$request->username,
                'password'=>$request->password];
          
            if(!Auth::guard('api-student')->attempt($credentials)) {
                return response()->json([
                    'message'=>'Error'
                ], 401);
            }
             
            if(Auth::guard('api-student')->attempt($credentials)){
                $user = Auth::guard('client');
                $success['token'] =  $user->createToken('MyApp')->accessToken;
                return response()->json(['success' => $success], $this->successStatus);
            }
            else{
                return response()->json(['error'=>'Username or password incorrect'], 401);
            }
        }
5
  • welcome to SO .. what error you getting .? Commented Sep 12, 2020 at 6:49
  • I am getting redirected to my login route instead of being issued the access token Commented Sep 12, 2020 at 6:52
  • add header "Accept":"application/json" then it will not redirect Commented Sep 12, 2020 at 6:54
  • Thanks. Its not redirecting anymore . But I am getting {"message":"Unauthenticated."} after I called the endpoint Commented Sep 12, 2020 at 7:00
  • remove middleware from here auth/getlogin it should be public api so anyone can login Commented Sep 12, 2020 at 7:01

1 Answer 1

0

remove middleware so anyone can login

Route::post('auth/getlogin', 'Api\StudentController@authenticate')->middleware('auth:api-student');
    

to

Route::post('auth/getlogin', 'Api\StudentController@authenticate'); 

you applied middleware here so before doing any action it checking route if auth or not

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. But now i got "message": "Method Illuminate\\Auth\\RequestGuard::attempt does not exist.". Does it mean I didn't call the Auth correctly?
The attempt method is part of the SessionGuard you cannot use Passport just generate token and send .

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.