I have an ionic 3 application that is using laravel as backend . For auth I am using JWT and everything is working fine from registration , login and posting data within the app.
Problem starts when the user closes the mobile app and opens it again, I want the app to auto login the user using the saved token in storage.
The code below is for checking if the user has a token in storage, if there is then the token needs to be sent to laravel backend for verification.
verifyToken() {
return new Promise((resolve, reject) => {
this.storage.get("token").then((value) => {
let headers = new Headers();
headers.append("Accept", "application/json");
headers.append("Content-Type", "application/json");
headers.append("Authorization", "Bearer " + value);
this.http.post(ApiUrl + "/verifyToken", { headers: headers }).subscribe(
(res) => {
let data = res.json();
console.log("Valid token results:", data);
resolve(data);
},
(error: any) => {
console.log("Token was invalid", error);
reject(JSON.parse(error._body));
}
);
});
});
}
On Laravel backend I have this code for verification
public function verifyToken()
{
try {
if (! $user = JWTAuth::parseToken()->authenticate()) {
return response()->json(['success' => false, 'error' => 'User not found.',$e->getStatusCode()], 404);
}
} catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
return response()->json(['success' => false, 'error' => 'Login Token expired.',$e->getStatusCode()], 401);
} catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
return response()->json(['success' => false, 'error' => 'Login Token invalid.',$e->getStatusCode()], 401);
} catch (Tymon\JWTAuth\Exceptions\JWTException $e) {
return response()->json(['success' => false, 'error' => 'Login Token not supplied.',$e->getStatusCode()], 401);
}
// the token is valid and we have found the user
return response()->json(['success' => true, 'message' => 'Token is valid.'], 200);
}
When I test this via postman and provide valid user token, I get status 200 but when I do the same on the mobile app I get an error 500
"message": "The token could not be parsed from the request",
"exception": "Tymon\JWTAuth\Exceptions\JWTException",.
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Y1R3vK
via IFTTT
Aucun commentaire:
Enregistrer un commentaire