Next.js Discord

Discord Forum

Caching session

Answered
Savannah posted this in #help-forum
Open in Discord
SavannahOP
Hello, what technique/technology do you recommend for caching user sessions?

ps: I don't use vercel
Answered by B33fb0n3
yea, either remove or create it inside the join table šŸ‘
View full answer

28 Replies

@Savannah Hello, what technique/technology do you recommend for caching user sessions? ps: I don't use vercel
Don’t cache the user session. Else you might work with your cache and your user itself may be not even signed in. But the cache is still valid. And that just one of many problem that you can have with cache. So don’t cache it.

Choose one these methods for auth:
jwt: json object that is signed by the server. It can’t be changed. No more database calls needed to retrieve data. The whole object will be saved in client

Session: you only save a session token clientside and you need to make an extra call to your database to retrieve the data of the session.

I prefer jwt. What about you?
SavannahOP
I’m using both (At least I think so)
In my callbacks I’ve jwt and session
@Savannah In my callbacks I’ve jwt and session
do you only get an id or do you get a full object?
SavannahOP
But I wanted to save as a session, because when I log on to my Dashboard, the session items don't set and therefore the items related to the session (such as the user's role) aren't displayed directly.)
@B33fb0n3 do you only get an id or do you get a full object?
SavannahOP
Full object
(Sorry I’m at school and didnt have discord on computer šŸ˜‚)
@Savannah Full object
can you scroll a little bit to the top. There should be a "strategie"
SavannahOP
@Savannah Click to see attachment
you are using "jwt" as method. So your "session" contains all the fields, that you added and is saved on the client. So you don't need additional database calls and your "session" is cached clientside
So be able to access your additional values form your session (typescript), you can add a next-auth.d.ts under types/... inside your root folder. Add the following to it:
import {DefaultSession} from "next-auth"

declare module "next-auth/jwt" {
    /** Returned by the `jwt` callback and `getToken`, when using JWT sessions */
    interface JWT {
        /** OpenID ID Token */
        idToken?: string
    }
}

declare module "next-auth" {
    /**
     * Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
     */
    interface Session {
        user: {
            id: string
            groups: string[] // here you can add your additional fields. In my case "groups"
        } & DefaultSession["user"]
    }
}
@B33fb0n3 why?
SavannahOP
No, but I was asking the question because I don't know if I do or not.
@Savannah No, but I was asking the question because I don't know if I do or not.
if you getting just the session by doing for example:
const session = await getServerSession(authOptions);

There is no additional call to the database
@B33fb0n3 the useSession() result gives you the same result as this example: https://discord.com/channels/752553802359505017/1253617728535924776/1253674744637558877
SavannahOP
Sorry I haven't been in front of the computer since our conversation. Okay thanks for your help I get it now!
@B33fb0n3 Sure thing. Please mark solution
SavannahOP
Yeah sure, but can I ask you a question before ?

I'd like to make a system that would allow users to put something in favorite .

What would be the best way to do this?
@Savannah Yeah sure, but can I ask you a question before ? I'd like to make a system that would allow users to put something in favorite . What would be the best way to do this?
I would create a join table for it. A join table of ā€žSomethingā€œ and ā€žUserā€œ and if a user likes ā€žSomethingā€œ create a entry inside this join table. You need the join table, because one user can have many ā€žSomethingā€œ and one ā€žSomethingā€œ can have many ā€žUserā€œ. So a many-to-many connection. Solve it by using a join table
@Savannah Okay, I see. So you'd send a request every time the user bookmarked something?
yea, either remove or create it inside the join table šŸ‘
Answer
@B33fb0n3 yea, either remove or create it inside the join table šŸ‘
SavannahOP
Perfect, thanks dude šŸ‘
sure thing