Next.js Discord

Discord Forum

How to persist logged in user in Next.js

Answered
Dwarf Crocodile posted this in #help-forum
Open in Discord
Dwarf CrocodileOP
How can I make the session information of a logged-in user persistent? Should I store it on a cookie or something similar? It wouldn't make sense to call my 'authenticatedUserFetch' function every time I need user information.

export const authenticatedUserFetch = async () => {
    const { value: token } = cookie.get("token");

    const response = await fetch(`${process.env.API_URL}/profile`, {
        headers: {
            Accept: "application/json",
            Authorization: `Bearer ${token}`,
        },
    });

    const data = await response.json();
    return data;
};

9 Replies

put it in cookies
encrypt it and store the string in your cookies
Answer
@Dwarf Crocodile
Dwarf CrocodileOP
thank you 🙂
@Arinji put it in cookies
Spectacled bear
Silly question, why cant we store the details inside session object which is available most of the times?
Spectacled bear
next auth? 😅
@Spectacled bear next auth? 😅
sorry, this thread isnt for next auth + i dont use next auth, make a different thread.