Next.js Discord

Discord Forum

Having trouble accessing session data in route handler. (next auth)

Unanswered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
Every time I try to access the session data from my next auth session in a server component, it crashes. Is there a work around? Maybe I am trying to access it the wrong way. Here is my route handler:

import NextAuth from "next-auth/next";
import GoogleProvider from "next-auth/providers/google";
import { checkUser, putItem } from "../../../Methods/dynamo";
import { getSession } from "next-auth/react";


const handler = NextAuth({

providers: [
GoogleProvider({
clientId:process.env.GOOGLE_CLIENT_ID as string,
clientSecret:process.env.GOOGLE_CLIENT_SECRET as string,
})
],
callbacks: {
async redirect({url, baseUrl}) {
const session = await getSession();
const path = await checkUser(session?.user?.email as string);
return ${baseUrl}${path};
}
}
})
export { handler as GET, handler as POST }

21 Replies

use getServerSession instead of getSession
getSession is for client-side use to fetch to next-auth api to get session manualy
Asiatic LionOP
Thank you. So for getServerSession I need to pass it a config file with my providers...etc?
usually the authOption is exported
Asiatic LionOP
here's my new code
import NextAuth from "next-auth/next";
import GoogleProvider from "next-auth/providers/google";
import { checkUser, putItem } from "../../../Methods/dynamo";
import { getServerSession } from "next-auth";
import type { NextAuthOptions } from "next-auth"

const config = {
providers: [
GoogleProvider({
clientId:process.env.GOOGLE_CLIENT_ID as string,
clientSecret:process.env.GOOGLE_CLIENT_SECRET as string,
})
],
secret: process.env.NEXTAUTH_SECRET
}

const handler = NextAuth({

providers: [
GoogleProvider({
clientId:process.env.GOOGLE_CLIENT_ID as string,
clientSecret:process.env.GOOGLE_CLIENT_SECRET as string,
})
],
callbacks: {
async redirect({url, baseUrl}) {
const session = await getServerSession(config);
const path = await checkUser(session?.user?.email as string);
return ${baseUrl}${path};
}
}
})


export { handler as GET, handler as POST }
why do you need to redirect to ${baseUrl}${path}
Asiatic LionOP
this is how I am redirecting the user, if they sign in with google and their email is found in my db then it will send them somewhere, it's not found they will go somewhere else
so after they hit signin -> it redirects them to this dynamic path
according to the docs this is a more appropriate way to redirect after they hit signin
the redirect callback is only used to process the incoming URL
Asiatic LionOP
ah ok
so i can pull the session data into the component that's rendering those buttons
Asiatic LionOP
thank you
when you say make a reusable function, what logic were you thinking would go in there?
@Asiatic Lion when you say make a reusable function, what logic were you thinking would go in there?
in my app, i redirect to pages when its a new member, so roughly like

export const authorize = cache(async function authorize(){
  const session = await getServerSession(...)
  if (!session) redirect('/login')
  if (!session.user.username) redirect ('/createUsername')
  return session
})

cache here ensures that the logic only runs once despite being called multiple times in multiple component in a single request.

more info here: https://nextjs.org/blog/security-nextjs-server-components-actions#data-access-layer
Asiatic LionOP
ok thank you