I am working on mobile app and I connected my app to server through API now every time I try to do actions that requires users authentication I get 401
error while same action with postman
do the job without issue.
What I did
- stored token to device local storage
- retrieved that token and send it as request header to server
- Additionally added
'Accept': 'application/json, text/plain',
to header request - Added this package in order to open my back-end CROS origins.
Code
app
logout() {
const headers = new HttpHeaders({
'Accept': 'application/json, text/plain',
'Authorization': this.token["token_type"] + " " + this.token["access_token"]
});
return this.http.post(this.env.BASE_URL + '/logout', { headers: headers })
.pipe(
tap(data => {
this.storage.remove("token");
this.isLoggedIn = false;
delete this.token;
return data;
})
)
}
route (back-end)
Route::group(['middleware' => 'auth:api'], function(){
Route::post('logout', 'Api\AuthController@logout');
});
controller (back-end)
public function logout(Request $request)
{
$request->user()->token()->revoke();
return response()->json([
'message' => 'Successfully logged out'
]);
}
For this sample I shared my logout method other methods such as update, delete,store are the same structure.
Results
Any idea?