Next-Auth Sessions
Unanswered
Red-billed Tropicbird posted this in #help-forum
Red-billed TropicbirdOP
Does any one know how to debug whether a session is working or not in next-auth?
6 Replies
Red-billed TropicbirdOP
Here is my auth file:
import { linkOAuthAccount } from "@/actions/auth"
import { getUserById } from "@/actions/user"
import { PrismaAdapter } from "@auth/prisma-adapter"
import NextAuth from "next-auth"
import { env } from "@/env.mjs"
import authConfig from "@/config/auth"
import { prisma } from "@/config/db"
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
debug: env.NODE_ENV === "development",
pages: {
signIn: "/signin",
signOut: "/signout",
verifyRequest: "/signin/magic-link-signin",
},
secret: env.AUTH_SECRET,
session: {
strategy: "jwt",
maxAge: 30 * 24 * 60 * 60, // 30 days
updateAge: 24 * 60 * 60, // 24 hours
},
events: {
async linkAccount({ user }) {
if (user.id) await linkOAuthAccount({ userId: user.id })
},
},
callbacks: {
jwt({ token, user }) {
if (user) token.role = user.role
return token
},
session({ session, token }) {
session.user.role = token.role as "USER" | "ADMIN"
return session
},
async signIn({ user, account }) {
if (!user.id) return false
if (account?.provider !== "credentials") return true
const existingUser = await getUserById({ id: user.id })
return !existingUser?.emailVerified ? false : true
},
},
adapter: PrismaAdapter(prisma) as unknown as undefined,
...authConfig,
})
import { linkOAuthAccount } from "@/actions/auth"
import { getUserById } from "@/actions/user"
import { PrismaAdapter } from "@auth/prisma-adapter"
import NextAuth from "next-auth"
import { env } from "@/env.mjs"
import authConfig from "@/config/auth"
import { prisma } from "@/config/db"
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
debug: env.NODE_ENV === "development",
pages: {
signIn: "/signin",
signOut: "/signout",
verifyRequest: "/signin/magic-link-signin",
},
secret: env.AUTH_SECRET,
session: {
strategy: "jwt",
maxAge: 30 * 24 * 60 * 60, // 30 days
updateAge: 24 * 60 * 60, // 24 hours
},
events: {
async linkAccount({ user }) {
if (user.id) await linkOAuthAccount({ userId: user.id })
},
},
callbacks: {
jwt({ token, user }) {
if (user) token.role = user.role
return token
},
session({ session, token }) {
session.user.role = token.role as "USER" | "ADMIN"
return session
},
async signIn({ user, account }) {
if (!user.id) return false
if (account?.provider !== "credentials") return true
const existingUser = await getUserById({ id: user.id })
return !existingUser?.emailVerified ? false : true
},
},
adapter: PrismaAdapter(prisma) as unknown as undefined,
...authConfig,
})
@Red-billed Tropicbird Does any one know how to debug whether a session is working or not in next-auth?
Enter any server side component and do
const session = await getServerSession(yourAuthOptions) and then console.log the session. If it's null, the user isn't logged in if it has values, the user is logged inRed-billed TropicbirdOP
What is the yourAuthOptions?s
and is getServerSessions buult in?
Red-billed TropicbirdOP
for next-auth
Red-billed TropicbirdOP
I get this error message on my adapter as well:
I get this error message with my adapter for Prisma:
How do i fix this error:
Type 'import("/Users/mehtabghuman/Desktop/ReZume/Rezume/node_modules/.pnpm/@auth+core@0.32.0_nodemailer@6.9.13/node_modules/@auth/core/adapters").Adapter' is not assignable to type 'import("/Users/mehtabghuman/Desktop/ReZume/Rezume/node_modules/.pnpm/@auth+core@0.27.0_nodemailer@6.9.13/node_modules/@auth/core/adapters").Adapter'.
Types of property 'getAuthenticator' are incompatible.
Type '((credentialID: string) => import("/Users/mehtabghuman/Desktop/ReZume/Rezume/node_modules/.pnpm/@auth+core@0.32.0_nodemailer@6.9.13/node_modules/@auth/core/types").Awaitable<import("/Users/mehtabghuman/Desktop/ReZume/Rezume/node_modules/.pnpm/@auth+core@0.32.0_nodemailer@6.9.13/node_modules/@auth/core/adapters").Ada...' is not assignable to type '((credentialID: string) => import("/Users/mehtabghuman/Desktop/ReZume/Rezume/node_modules/.pnpm/@auth+core@0.27.0_nodemailer@6.9.13/node_modules/@auth/core/types").Awaitable<import("/Users/mehtabghuman/Desktop/ReZume/Rezume/node_modules/.pnpm/@auth+core@0.27.0_nodemailer@6.9.13/node_modules/@auth/core/adapters").Ada...'.
Type '(credentialID: string) =>
Type
Type 'AdapterAuthenticator' is not assignable to type 'Awaitable<AdapterAuthenticator | null>'.
Type
Types of property 'transports' are incompatible.
(property) adapter?: Adapter | undefined
I get this error message with my adapter for Prisma:
How do i fix this error:
Type 'import("/Users/mehtabghuman/Desktop/ReZume/Rezume/node_modules/.pnpm/@auth+core@0.32.0_nodemailer@6.9.13/node_modules/@auth/core/adapters").Adapter' is not assignable to type 'import("/Users/mehtabghuman/Desktop/ReZume/Rezume/node_modules/.pnpm/@auth+core@0.27.0_nodemailer@6.9.13/node_modules/@auth/core/adapters").Adapter'.
Types of property 'getAuthenticator' are incompatible.
Type '((credentialID: string) => import("/Users/mehtabghuman/Desktop/ReZume/Rezume/node_modules/.pnpm/@auth+core@0.32.0_nodemailer@6.9.13/node_modules/@auth/core/types").Awaitable<import("/Users/mehtabghuman/Desktop/ReZume/Rezume/node_modules/.pnpm/@auth+core@0.32.0_nodemailer@6.9.13/node_modules/@auth/core/adapters").Ada...' is not assignable to type '((credentialID: string) => import("/Users/mehtabghuman/Desktop/ReZume/Rezume/node_modules/.pnpm/@auth+core@0.27.0_nodemailer@6.9.13/node_modules/@auth/core/types").Awaitable<import("/Users/mehtabghuman/Desktop/ReZume/Rezume/node_modules/.pnpm/@auth+core@0.27.0_nodemailer@6.9.13/node_modules/@auth/core/adapters").Ada...'.
Type '(credentialID: string) =>
Type
Type 'AdapterAuthenticator' is not assignable to type 'Awaitable<AdapterAuthenticator | null>'.
Type
Types of property 'transports' are incompatible.
(property) adapter?: Adapter | undefined