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);
}
}
"Accept":"application/json"then it will not redirectauth/getloginit should be public api so anyone can login