S
SolidJS16mo 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?
5 Replies
vibeman1987
vibeman198716mo ago
send us your createServerData function show us your code ;D
Mikle
MikleOP16mo ago
This is one of the createServerData's And then the generic get function I'm using to add the accessToken to the header I'm definitely doing something wrong here 😂
No description
No description
vibeman1987
vibeman198716mo ago
which is the one that works?
Mikle
MikleOP16mo 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
shogun207716mo 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",
},
})
Want results from more Discord servers?
Add your server