How can i access the session in a server-component? i tried a few things but didnt work so far
Unanswered
American Robin posted this in #help-forum
American RobinOP
im trying to access the session inside a server-component, but its not working how can i make it work?
94 Replies
American RobinOP
or should i just make a client component?
i tried getServerSession so far didnt work for me
Large garden bumble bee
getServerSession is the way, but you need to pass the function the same options you passed to the NextAuth() init
and scroll to getServerSession
@Large garden bumble bee getServerSession is the way, but you need to pass the function the same options you passed to the NextAuth() init
American RobinOP
i tried this, but it didnt work somehow
i guess i didnt implement it correctly?
Large garden bumble bee
can you show the code?
American RobinOP
sure wait a second
export const authOptions: NextAuthOptions = {
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID ?? "",
clientSecret: process.env.GOOGLE_CLIENT_SECRET ?? "",
authorization: {
params: {
prompt: "consent",
// access_type: "offline",
// response_type: "code",
},
},
}),
],
adapter: SupabaseAdapter({
url: process.env.NEXT_PUBLIC_SUPABASE_URL ?? "",
secret: process.env.SUPABASE_SERVICE_ROLE_KEY ?? "",
}) as Adapter,
session: {
strategy: "database",
// maxAge: 30 * 24 * 60 * 60, // 30 days
maxAge: 30 * 24 * 60 * 30, // 15 days
},
callbacks: {
async session({ session, user }) {
const signingSecret = process.env.SUPABASE_JWT_SECRET;
if (signingSecret) {
const payload = {
aud: "authenticated",
exp: Math.floor(new Date(session.expires).getTime() / 1000),
sub: user.id,
email: user.email,
role: "authenticated",
};
session.supabaseAccessToken = jwt.sign(payload, signingSecret);
}
return session;
},
},
};this is in the route.ts from [...nextauth]
oh
it works now e.e???
i didnt change anything
but it works now??
American RobinOP
how can i access it in a middleware?
im getting this error:
i guess because the middleware is not in the <SessionProvider>
or?
Large garden bumble bee
American RobinOP
i dont get it, how to use it
the doc for the middleware/auth
Large garden bumble bee
If you only want to secure certain pages, export a config object with a matcher:
export { default } from "next-auth/middleware"
export const config = { matcher: ["/dashboard"] }
Now you will still be able to visit every page, but only /dashboard will require authentication.
export { default } from "next-auth/middleware"
export const config = { matcher: ["/dashboard"] }
Now you will still be able to visit every page, but only /dashboard will require authentication.
American RobinOP
i did this
in the middleware.ts right?
Large garden bumble bee
yes
American RobinOP
still get this error
i mean wait
do i have to use this:
const session = await getServerSession(authOptions);
in the middleware aswell?
const session = await getServerSession(authOptions);
in the middleware aswell?
Large garden bumble bee
nah
American RobinOP
what do i have to use instead
Large garden bumble bee
are you using database or jwt?
American RobinOP
strategy: "database"
but i do use this: session.supabaseAccessToken = jwt.sign(payload, signingSecret);
@American Robin still get this error
Large garden bumble bee
can you show the whole error? i mean the error lines and maybe logs from the console or something
American RobinOP
this error came from: const session = await getServerSession(authOptions); in the middleware
since u said it should not be there
i removed it
i dont get any error, but i cant access it or dont know how to access it via the middleware
Large garden bumble bee
i need you to write down more information on how you want to use the middleware
because i really dont know what you are trying to achvive
American RobinOP
i want to store the supabaseaccestoken in the cookies aswell
Large garden bumble bee
then thats not middlewares job
you need to edit the callback on session
American RobinOP
oh oke i see
American RobinOP
@Large garden bumble bee hello! do u have time for a question?
Large garden bumble bee
sure, ask away
American RobinOP
how can i get the session data in middleware nextjs nextauth with database strategy
im trying to make a protected route
Large garden bumble bee
do you need the user to just be logged in?
American RobinOP
no, he has a role aswell
this musst be "ADMIN"
im using the database strategy
and every tutorial is using jwt
Large garden bumble bee
American RobinOP
ye
i found this too
is it smart to make
a db call?
in the middleware
since i have the role in the db aswell
Large garden bumble bee
depends on if you can cache it in the middleware, if so then it could be possible
American RobinOP
i can assess the cookies
Large garden bumble bee
or just check it in every page on its own
@Large garden bumble bee or just check it in every page on its own
American RobinOP
i did this
but i wanted a better way via middleware
where i just define the matcher
i can access this
@Large garden bumble bee or just check it in every page on its own
Large garden bumble bee
yeah i understand but it seems like this is the only way for now
American RobinOP
in the middleware
should i make a db call?
and get the role via the query
Large garden bumble bee
you can, but be sure to cache it, so that it doesn't have to run for every route
American RobinOP
its a cookie, or what u mean by cache it
Large garden bumble bee
the db call
cache the db call
American RobinOP
ah i see
how would i do that? didnt watch / read about it yet
seems like you create a function in some utils class and just wrap it in cache()
American RobinOP
oh oke, doesnt sound so hard to make
do u suggest me
to rewrite everything
and use the jwt strategy?
or just stick with database
im using supabase adapter
and everything works fine so far
with nextauth
Large garden bumble bee
i dont know anything about supabase but i would stick with database, although it really depends on you and how you want to do it
American RobinOP
oke thanks!