#so if i am using server components i
1 messages · Page 1 of 1 (latest)
i am fetching a api to verify the user
it needs autorization token from local storaage
everytime i call the api
why don't you use cookies? Save the token in the cookies and the server can read them. Then fetch serverside
try {
const res = await fetch(`${BASE_URL}/autologin`, {
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
});
console.log(res);
if (!res.ok) {
throw new Error(`Failed to fetch data: ${res.status}`);
}
return res.json() as Promise<IAuthResponse>;
} catch (error: any) {
// Handle fetch errors (like network issues)
console.error("Fetch error:", error.message);
throw error;
}
}```
i am not sure how to do this and we dont logout user until the use logout
you set the token as cookie, when the user login
i mean the old user where using token from localstorgae
and backend jsut verify the token
i know the authentication sucks
but we have over million users
yes, auth like that sucks xD
what's the problem on a logout? You should log them out after a specific time my natural.. for safety reasons alone
no we dont do that
no, it's supported by next
which auth library are u using?
our dashbaord is a subdomain and we use the same localstorage method to login
for now all react i am just starting to create the landing page innext
making sure everything go smooth before i actually start coding
try {
const res = await fetch(`${BASE_URL}/autologin`, {
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
});
console.log(res);
if (!res.ok) {
throw new Error(`Failed to fetch data: ${res.status}`);
}
return res.json() as Promise<IAuthResponse>;
} catch (error: any) {
console.error("Fetch error:", error.message);
throw error;
}
}```
solve me this issue
Can I give you a good and easy to setup auth library with cookie based AND localstorage based?
sure why not
and it should not affect the old users
who are using localstorage
you said you using react to auth the user. So the auth currently is only clientside?
we were using react before
now no i am changing all the component to server side
yeah

i still i have to use localstorage in next as well ?
so if i am using server components i have to make a client component which gets this data and use in server compoent ?
something this will work right ?
No, you can’t. You can only go one direction: server -> client or client -> server thought params. And I don’t think that the user want, that their token will be published throught the url
If I were you, I would upgrade to an library, which can handle both auth modes (ssr though cookies and clientside though localstorage) and then you are ready to go for everything
its to lait for this now
Why?
as i said you our authentication process is messed up
and we have like 400-500 always online
it would be better if we cahnge the authentication but will have to do so many changes
but let me tell you we still doing this for login user to send user from landing to dashbaord
Of course it’s a completely change. I think it is everytime, when you switch from one framework to another. But that’s also the chance to make a great app. To fix stuff. To also get a good auth system, …
You already said it ^
You NEED to change something. It can’t be how it is!
your favicon needs to be in the app dir:
https://nextjs.org/docs/app/api-reference/file-conventions/metadata/app-icons#image-files-ico-jpg-png