0

If a user is returning to my site with existing JWT cookies I want to check if they are still valid. I think I want to preload my cache when the application loads by testing my auth token against the server.

Is this a good way/place to check if a token is still active? Yes, each call to the server will test this but I'd like to show a logged in status. Should I place a call in each component that needs to react to logged in status? Seems inefficient.

Here's what I'm thinking but it doesn't verify that the access token is still valid. Can I run a query here? What would that look like?

const client = new ApolloClient({
    cache,
    link,
});

cache.writeData({
    data: {
        isLoggedIn: !!Cookies.get('access-token'),
    },
});
Scott
  • 3,204
  • 3
  • 31
  • 41
  • If you have a component that checks the status via query why not just use a `@client` query to check the current state? – paulpdaniels Apr 23 '20 at 05:56
  • I did create a hook that checks `isLoggedIn @client` but I thought I’d want to check it against the server once during app load. Otherwise, which component should get that responsibility? – Scott Apr 23 '20 at 06:07
  • decode token, get its expiry time > logged or not ... https://stackoverflow.com/q/26739167/6124657 – xadm Apr 23 '20 at 09:39

0 Answers0