One of the challenges I'm facing is implementing JWT Auth in my Laravel + Vue SPA.
When checking the credentials in my Controller, the code looks like this:
try {
if (!$token = JWTAuth::attempt($credentials)) {
return response()->json(
[
'error' => 'Invalid Credentials',
], 401
);
}
} catch (JWTException $e) {
return response()->json(
[
'error' => 'Could not create token!',
]
);
}
return response()->json(
[
'token' => $token,
]
);
When making an API call, the code looks like this:
axios.post('/api/user/signin', payload)
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})
If valid credentials are passed with the payload
, I receive the token in the then
block, which I can log in the console.
If invalid credentials are passed, an error is displayed in the console:
Error: Request failed with status code 401 at createError (app.js:10562) at settle (app.js:21378) at XMLHttpRequest.handleLoad (app.js:10401)
I am trying to figure out how to specifically get the error message "Invalid Credentials". By removing the status code from the response or setting it to 200, I am able to receive the "Invalid Credentials" error message.