S
SolidJS3y ago
Mikle

Fetching data with authorisation token

I’m using auth.js with cognito and my backend requires the authorisation token to be present with every request. I’ve managed to get this working by using createServerData$ and then passing it to a generic GET function that will retrieve the token from the request. Is this the best way of doing this? I’m struggling to create a wrapper around createServerData. I have a dashboard of components that each hit different endpoints, my thoughts were to have them handle their fetching themselves but it’s a lot of duplicate code with createServerData each time. Has anyone else had a similar problem?
4 Replies
vibeman1987
vibeman19873y ago
send us your createServerData function show us your code ;D
vibeman1987
vibeman19873y ago
which is the one that works?
Mikle
MikleOP3y ago
It all works, Im just wondering if the way im going about it is the best practice for appending the accessToken to every request or whether ive set something up wrong architecturally Ideally I want to cut down the lines of code required for each component that makes a request and just have simple get functions in the routeData, even if that means wrapping the createServerData but I couldnt manage to get a wrapper to work
shogun2077
shogun20773y ago
This seems to be the usual way of doing it. Unless you want to make use of an axios instance. e.g
export const apiInstance = axios.create({
baseURL: "https://somerandom.url",
headers: {
"Authorization":"Bearer MYUSERTOKEN",
},
})
export const apiInstance = axios.create({
baseURL: "https://somerandom.url",
headers: {
"Authorization":"Bearer MYUSERTOKEN",
},
})

Did you find this page helpful?