Best practices for managing auth token

For brevity I’ll assuming you’re calling an endpoint that you can’t change. How you should implement will heavily depend on whether the token is app or user based (one token for all users on a shared app instance or one token per user).

If it’s one auth token for the entire app:

  • Store it in memory along with a time-to-live timestamp (or alternatively catch the token expired error, request a new token and retry the original request), refresh it if it doesn’t exist/is expired
  • If you’re concerned about re-requesting API tokens after an application restart also store it in the database and load it at startup if it exists

If it’s one token per user:

  • Store it in your user session, it’s exactly what sessions are used for, if you’re authing users then they’ll have a session and the overhead is already there
  • If you don’t want to re-request a token everytime they login store their current token in the DB and and load it into their session when they login

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)