Caching session
Answered
Savannah posted this in #help-forum
SavannahOP
Hello, what technique/technology do you recommend for caching user sessions?
ps: I don't use vercel
ps: I don't use vercel
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?
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"]
}
}But im using additional calls to the database?
@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:
There is no additional call to the database
const session = await getServerSession(authOptions);There is no additional call to the database
@B33fb0n3 if you getting just the session by doing for example:
tsx
const session = await getServerSession(authOptions);
There is no additional call to the database
SavannahOP
Yeah, thatās why am I doing in addition to useSession() client side
@Savannah Yeah, thatās why am I doing in addition to useSession() client side
the useSession() result gives you the same result as this example: https://nextjs-forum.com/post/1253617728535924776#message-1253674744637558877
@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!
@Savannah Sorry I haven't been in front of the computer since our conversation. Okay thanks for your help I get it now!
Sure thing. Please mark solution
@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?
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
@B33fb0n3 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
SavannahOP
Okay, I see. So you'd send a request every time the user bookmarked something?
@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