next auth session
Unanswered
lor3512 posted this in #help-forum
lor3512OP
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
lor3512OP
@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.
lor3512OP
There is no error message
... getSession is failing for ...then the reason for this
lor3512OP
@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
},
},
})
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 workswhat works? To retrieve the session?
lor3512OP
Yes it returns the session
but not getSession
redirect still goes off forI thought it was a next-auth issue and not a redirect issue?
@lor3512 And then the redirect is here:
lor3512OP
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 })
}
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
lor3512OP
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)?lor3512OP
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 })
}
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 itlike the constant, where your provider and stuff is
lor3512OP
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 })
}
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
Like
authOptions to it.Like
export default NextAuth(yourAuthOptions)lor3512OP
Wait can we call and I screenshare
This will be way easier
we can't
lor3512OP
Is there a rule or smth
just create a global variable called
authOptions and import it where you need it.lor3512OP
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
},
},
})
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:
And then import it where you need it. For example on the route handler:
Or (what we are trying to do) for the server session:
auth.js export const yourAuthOptions = { /*... your provider stuff and more*/ }some/route.tsAnd 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)lor3512OP
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 })
}
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 😭
lor3512OP
where do I put auth.js @B33fb0n3
it's a file. Put it wherever you want. I like to put it inside my
utils/@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 authOptionslor3512OP
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 })
}
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 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 })
}
import it from
import {getServerSession} from "next-auth";@lor3512 Also I use JS not TypeScript
thats fine. Just remove the types
what's your
secure.js? Is that a route handler? A client component? A function? Something that is called somewhere? Please clarifylor3512OP
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.
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.
lor3512OP
What is the issue
@B33fb0n3