#Access token from session in authentication header
1 messages · Page 1 of 1 (latest)
Get the session with sessionStorage.getSession passing the cookie header value, then do session.get(“token”)
Check the using session docs https://remix.run/docs/en/v1/api/remix#using-sessions
Thank you., but I have a problem that I don't have a Request in the api method and therefore I can't get to the session. I have hundreds of those API methods, I can't send it manually every request. Look:
// companies.tsx
export const loader: LoaderFunction = async () => {
const companies = await getCompanies()
return json<LoaderData>({companies})
}
// companies.api.ts
async function getCompanies() {
return api('/companies', 'GET')
}
// api.ts
function api(url: string, method: 'GET' | 'POST', 'PUT') {
const accessToken = '??'
return fetch(...., {
headers: {
'Authorization': `Bearer ${accessToken}`
}
})
}
Get the token in the loader and pass the token as argument to your api function
I probably have to do that in every loader. Or I can send the entire request to the api and get the access token from the session there. Good?