0

I am using Laravel as my backend together with Sanctum which generates personal access token for mobile users. For my mobile application I am using flutter.

To authenticate users they login with their username/password and get a personal access token in return. This works but requires a user to login every time they open the application again so I did what most tutorials suggest which is saving the token on the mobile device using shared preferences/secure storage.

Now comes the question how do you invalidate a user when you remove their token from the backend? On initial login it appears everything is still fine because like in most tutorial I check for the existence of a token. After that whenever I want to make a request which uses the token I obviously run into problems because it not longer exists on the backend.

Most tutorials/guide suggest saving the token and using that a reference to see if the user is logged in or not but this seems flawed because it gives the false impression you actually have a valid token.

My guess is this can be solved by always performing a heartbeat/ping action to check if the current token is valid and if not send them to the login screen instead of simply checking for the existence of the token.

Thoughts on this?

Stephan-v
  • 19,255
  • 31
  • 115
  • 201
  • Your guess sounds reasonable. I wouldn't assume a token is valid just because it's there. People can just put one there via their device's debug mode if they want to be malicious – apokryfos Dec 06 '20 at 09:06

1 Answers1

0

I can suggest a hack or trick here in every launch of the app you can send a request to an API to check if the user's token is valid or not and if it is valid then you can continue the app otherwise force the user to login and generate new token this way your app will be secure via server / API.

For this, you can store the user's secret token in the database and check it via HTTP API call and send a response from the API accordingly and check the response in app and do the next operation according to the response you get.

I don't know if this is a great way of doing this job but it is a kind of hack/trick to achieve what is needed.

Thanks

Shoaib Khan
  • 830
  • 1
  • 9
  • 17