Next.js Discord

Discord Forum

next auth session

Unanswered
lor3512 posted this in #help-forum
Open in Discord
Can someone help me my app in localhost works and not in vercel, the issue is that getSession is failing for next auth, i go to api/auth/session and it works, but the redirect still goes off for some weird reason, someone please help i've been here for 4 hours

58 Replies

@B33fb0n3 What do I ad?
@lor3512 <@301376057326567425> What do I ad?
... for example: relevant code snippets, a reproduction repository, and/or more detailed error messages.
There is no error message
... getSession is failing for ...
then the reason for this
@B33fb0n3 This is my nextauth code: import NextAuth from 'next-auth'
import DiscordProvider from 'next-auth/providers/discord'

export default NextAuth({
secret: process.env.secret,
providers: [
DiscordProvider({
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET,
authorization: { params: { scope: 'identify guilds email' } },
}),
],
callbacks: {
async jwt({ token, account }) {
if (account) {
token.accessToken = account.access_token
}
return token
},
async session({ session, token }) {
session.accessToken = token.accessToken
return session
},
},
})
And then the redirect is here:
go to api/auth/session and it works
what works? To retrieve the session?
Yes it returns the session
but not getSession
redirect still goes off for
I thought it was a next-auth issue and not a redirect issue?
@lor3512 And then the redirect is here:
import { getSession } from 'next-auth/react'

export default async (context, callback) => {
const session = await getSession(context)

if (!session) {
return {
redirect: {
destination: '/',
permanent: false,
},
}
}

return callback({ session })
}
So there are a few things, that you need to clarify
This is the code that redirects if there is no session
And in localhost it works but when i deploy to vercel it stops working
what if you get the session using
const abc = await getServerSession(yourAuthOptions)
?
Let me try
import { getServerSession } from 'next-auth/react'

export default async (context, callback) => {
const session = await getServerSession(context)

if (!session) {
return {
redirect: {
destination: '/',
permanent: false,
},
}
}

return callback({ session })
}
It does not work
instead of passing the context to it, try passing yourAuthOptions to it
like the constant, where your provider and stuff is
What are those
Let me try that
import { getServerSession } from 'next-auth/react'
import NextAuth from 'next-auth'
import DiscordProvider from 'next-auth/providers/discord'

export default async (context, callback) => {
const session = await getServerSession({
secret: process.env.secret,
providers: [
DiscordProvider({
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET,
authorization: { params: { scope: 'identify guilds email' } },
}),
],
callbacks: {
async jwt({ token, account }) {
if (account) {
token.accessToken = account.access_token
}
return token
},
async session({ session, token }) {
session.accessToken = token.accessToken
return session
},
},
})

if (!session) {
return {
redirect: {
destination: '/',
permanent: false,
},
}
}

return callback({ session })
}
This is secure.js
here you can extract it as const and then just import it as authOptions to it.

Like
export default NextAuth(yourAuthOptions)
Wait can we call and I screenshare
This will be way easier
we can't
Is there a rule or smth
just create a global variable called authOptions and import it where you need it.
where do I need authoptions I'm dumb
So I have 2 scripts
One that handles the api stuff like api/auth/session, with this code: import NextAuth from 'next-auth'
import DiscordProvider from 'next-auth/providers/discord'

export default NextAuth({
secret: process.env.secret,
providers: [
DiscordProvider({
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET,
authorization: { params: { scope: 'identify guilds email' } },
}),
],
callbacks: {
async jwt({ token, account }) {
if (account) {
token.accessToken = account.access_token
}
return token
},
async session({ session, token }) {
session.accessToken = token.accessToken
return session
},
},
})
for example:
auth.js
export const yourAuthOptions = { /*... your provider stuff and more*/ }

some/route.ts
And then import it where you need it. For example on the route handler:
export default NextAuth(yourAuthOptions)

some/place/with/page.tsx (or other server side file)
Or (what we are trying to do) for the server session:
const session = await getServerSession(yourAuthOptions)
One that I call from the page to check if there is a session or no: import { getServerSession } from 'next-auth/react'
import NextAuth from 'next-auth'
import DiscordProvider from 'next-auth/providers/discord'

export default async (context, callback) => {
const session = await getServerSession({
secret: process.env.secret,
providers: [
DiscordProvider({
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET,
authorization: { params: { scope: 'identify guilds email' } },
}),
],
callbacks: {
async jwt({ token, account }) {
if (account) {
token.accessToken = account.access_token
}
return token
},
async session({ session, token }) {
session.accessToken = token.accessToken
return session
},
},
})

if (!session) {
return {
redirect: {
destination: '/',
permanent: false,
},
}
}

return callback({ session })
}
Which one do I edit 😭
Both
where do I put auth.js @B33fb0n3
it's a file. Put it wherever you want. I like to put it inside my utils/
K
@B33fb0n3 getServerSession is not a function
Also I use JS not TypeScript
@lor3512 <@301376057326567425> getServerSession is not a function
show me from where you importated getServerSession. Also write AuthOptions with a lowercase letter at the begin like authOptions
import { getServerSession, getSession } from 'next-auth/react'
import NextAuth from 'next-auth'
import DiscordProvider from 'next-auth/providers/discord'
import { authOptions } from './auth'

export default async (context, callback) => {
const session = await getSession(authOptions)

if (!session) {
return {
redirect: {
destination: '/',
permanent: false,
},
}
}

return callback({ session })
}
Thats the code
@lor3512 Also I use JS not TypeScript
thats fine. Just remove the types
Redirects well, js that
what's your secure.js? Is that a route handler? A client component? A function? Something that is called somewhere? Please clarify
secure.js is the following: import {getServerSession} from "next-auth";
import NextAuth from 'next-auth'
import DiscordProvider from 'next-auth/providers/discord'
import { authOptions } from './auth'

export default async (context, callback) => {
const session = await getServerSession(authOptions)

if (!session) {
return {
redirect: {
destination: '/',
permanent: false,
},
}
}

return callback({ session })
}
the course that i bought uses it so that a person cannot go to /dashboard withouth being logged in.
So maybe a function yea
For me, it's not an ordinary pattern. That's why I don't know what happens where and when. Unfortunately, I can't help you. Sorry.
What is the issue
@B33fb0n3