Im using Laravel Passport and I'm trying to convert my code from using tokens stored in the localhost to using httponly cookies.
I followed this documentation: https://laravel.com/docs/5.8/passport#consuming-your-api-with-javascript , and added this to my Kernel.php:
'web' => [ // Other middleware... \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class, ],
This is my Login function in my AuthController:
public function login(Request $request) {
if(Auth::attempt(['email' => $request->email, 'password' => $request->password])){
$user = Auth::user();
$token = $user->createToken('API')->accessToken;
return response()->json(['user' => $user], 200)->withCookie(
'X-Secure-Token',
$token,
null,
null,
null,
false,
true
);
} else {
return response()->json(['message' => 'Unauthorized'], 401);
}
}
I'm successfully seeing the 'X-Secure-Token' in my Postman environment after I use the login route. But when I try to access in Postman the other routes protected by the 'auth:api' middleware, I am still returned with the Unauthenticated message. In the Authorization header of the request, I set the Auth Type to "No Auth".
I'm fairly a beginner in API creation. What should I do to make my authentication system work with httponly cookie in Postman?