Need help understanding server context

I need to communicate with an API that requires authentication every so often. I need to hide the API from the client so I am using Nuxt server API routes to proxy any requests. What I would like to do is read the authentication token from a server-only variable on each of these routes, and grab a new token automagically if it is expired. My first thought was to use a simple util like this:
let token: string | null = null;

export const getToken = async() => {
if(!token || isTokenExpired()) {
token = await getTokenFromAPI();
}
return token;
};

const getTokenFromAPI = async(): Promise<string> => {
// Logic to fetch and return token from API
};

const isTokenExpired = (): boolean => {
// Logic to check if token is expired
};
let token: string | null = null;

export const getToken = async() => {
if(!token || isTokenExpired()) {
token = await getTokenFromAPI();
}
return token;
};

const getTokenFromAPI = async(): Promise<string> => {
// Logic to fetch and return token from API
};

const isTokenExpired = (): boolean => {
// Logic to check if token is expired
};
I am struggling a bit understanding the documentation and have the following questions: 1. Is it possible to create a utils folder under /server and if so will this utility file be accessible client side? 2. Is there any possibility the token could be leaked to the browser? 3. Is there a better solution? This doesn't feel great to be honest.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server