Reputation: 1001
I try to implement JWT Auth in my Laravel + Vue SPA.
In my Controller I check the credentials like
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,
]
);
My API call look like this:
axios.post('/api/user/signin', payload)
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})
If I pass valid credentials with the payload
I get the token in the then
block and can log this in the console.
If I pass invalid credentials I get an error 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)
How I can get the error "Invalid Credentials"? If I remove the status code from the response or set it to 200 I get the error "Invalid Credentials".
Upvotes: 0
Views: 1516
Reputation: 2553
You can use response
key of error. To get the response.
axios.post('/api/user/signin', payload)
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err.response)
})
Check Here : https://github.com/mzabriskie/axios/blob/master/UPGRADE_GUIDE.md#012x---0130
Upvotes: 1