Verify User Token in Laravel

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 (TymonJWTAuthExceptionsTokenExpiredException $e) {

return response()->json(['success' => false, 'error' => 'Login Token expired.',$e->getStatusCode()], 401);

} catch (TymonJWTAuthExceptionsTokenInvalidException $e) {

return response()->json(['success' => false, 'error' => 'Login Token invalid.',$e->getStatusCode()], 401);

} catch (TymonJWTAuthExceptionsJWTException $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": "TymonJWTAuthExceptionsJWTException",.

enter image description here

from Newest questions tagged laravel-5 – Stack Overflow https://ift.tt/2Y1R3vK
via IFTTT

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.