Created
September 4, 2025 09:54
-
-
Save finnmglas/e4cff5022dc0d61b21647ece2f3a6f74 to your computer and use it in GitHub Desktop.
nextauth example callbacks - @skander what would they be in betterauth?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import NextAuth from 'next-auth'; | |
import GoogleProvider from 'next-auth/providers/google'; | |
import EmailProvider from 'next-auth/providers/email'; | |
import LinkedInProvider from 'next-auth/providers/linkedin'; | |
import { PrismaAdapter } from '@next-auth/prisma-adapter'; | |
import { PrismaClient } from '@prisma/client'; | |
import { SessionStrategy } from 'next-auth'; | |
const prisma = new PrismaClient(); | |
export const authOptions = { | |
session: { | |
strategy: 'jwt' as SessionStrategy, // else "database" for db sessions | |
}, | |
providers: [ | |
/* Providers listed here: | |
GoogleProvider({ | |
clientId: process.env.GOOGLE_CLIENT_ID!, | |
clientSecret: process.env.GOOGLE_CLIENT_SECRET!, | |
}), | |
LinkedInProvider({ | |
clientId: process.env.LINKEDIN_CLIENT_ID!, | |
clientSecret: process.env.LINKEDIN_CLIENT_SECRET!, | |
issuer: 'https://www.linkedin.com/oauth', // 👈 disables OIDC expectation | |
jwks_endpoint: 'https://www.linkedin.com/oauth/openid/jwks', | |
authorization: { | |
url: 'https://www.linkedin.com/oauth/v2/authorization', | |
params: { | |
scope: 'profile email openid', | |
prompt: 'consent', | |
access_type: 'offline', | |
response_type: 'code', | |
}, | |
}, | |
async profile(profile) { | |
return { | |
id: profile.sub, | |
name: profile.name, | |
email: profile.email, | |
image: profile.picture, | |
}; | |
}, | |
})*/ EmailProvider({ | |
server: { | |
host: process.env.EMAIL_SERVER_HOST, | |
port: process.env.EMAIL_SERVER_PORT ? parseInt(process.env.EMAIL_SERVER_PORT, 10) : 587, | |
auth: { | |
user: process.env.EMAIL_SERVER_USER, | |
pass: process.env.EMAIL_SERVER_PASSWORD, | |
}, | |
}, | |
// here you could put a custom mail callback too | |
// sendVerificationRequest: sendVerificationRequest, | |
from: process.env.EMAIL_FROM, | |
}), | |
], | |
debug: true, // Aktiviert detaillierte Logs | |
logger: { | |
error(code: any, ...message: any[]) { | |
console.error(code, ...message); | |
}, | |
warn(code: any, ...message: any[]) { | |
console.warn(code, ...message); | |
}, | |
debug(code: any, ...message: any[]) { | |
console.debug(code, ...message); | |
}, | |
}, | |
events: { | |
async signIn({ user, isNewUser }: any) { | |
/* Here you can have custom logic to do stuff | |
with the user data after signin -> ex. send it to backend | |
const existing = await prisma.userSubscription.findFirst({ | |
where: { userId: user.id } | |
}) | |
*/ | |
}, | |
async createUser({ user }: any) { | |
// Here also custom logic to send to backend? | |
}, | |
}, | |
pages: { | |
verifyRequest: '/login-mail', // redirect after mail verification | |
}, | |
callbacks: { | |
async jwt({ token, user }: any) { // custom data saving | |
if (user) { | |
token.id = user.id; | |
/* ex. | |
token.isEmployee = user.isEmployee; | |
token.isSuperUser = user.isSuperUser; | |
*/ | |
} | |
return token; | |
}, | |
async session({ session, token }: any) { // custom data saving | |
if (session.user && token) { | |
session.user.id = token.id; | |
/* ex. | |
session.user.isEmployee = token.isEmployee; | |
session.user.isSuperUser = token.isSuperUser; | |
*/ | |
} | |
return session; | |
}, | |
}, | |
adapter: PrismaAdapter(prisma), | |
}; | |
export default NextAuth(authOptions as any); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment