skylerdj
Auth Headers in Singleton Pattern
There's also a simplified version of this if you don't care about using a single source of truth and don't already have jotai in your app and don't want to add it, using regular js, but i recommend the first approach
10 replies
Auth Headers in Singleton Pattern
Since you're using a SPA with something like vite, you can use something like jotai so you can access the token from both inside and outside of react doing something like this:
1. first create the token atom
2. then sync the token atom with your hook
3. Then in your react app you can use either the
useAuthToken
to use the atom as a single source of truth or just keep using your provider hook
4. For outside of react like trpc or api calls, you can just call the getAuthToken
function which will retreive the value from the atom
10 replies