Breaking changes for NextAuth v5: - Updated route.ts to use NextAuthConfig and new export pattern - Changed getServerSession import to use auth from config - Updated types and session handling for NextAuth v5 - Maintained backward compatibility with existing auth flow
68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
import NextAuth from 'next-auth'
|
|
import GitHub from 'next-auth/providers/github'
|
|
import Google from 'next-auth/providers/google'
|
|
import Credentials from 'next-auth/providers/credentials'
|
|
import type { NextAuthConfig } from 'next-auth'
|
|
|
|
export const config: NextAuthConfig = {
|
|
providers: [
|
|
GitHub({
|
|
clientId: process.env.GITHUB_ID,
|
|
clientSecret: process.env.GITHUB_SECRET,
|
|
}),
|
|
Google({
|
|
clientId: process.env.GOOGLE_ID,
|
|
clientSecret: process.env.GOOGLE_SECRET,
|
|
}),
|
|
Credentials({
|
|
name: 'Credentials',
|
|
credentials: {
|
|
email: { label: "Email", type: "email" },
|
|
password: { label: "Password", type: "password" }
|
|
},
|
|
async authorize(credentials) {
|
|
// Call backend API to verify credentials
|
|
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/auth/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(credentials)
|
|
})
|
|
|
|
const user = await res.json()
|
|
if (res.ok && user) {
|
|
return user
|
|
}
|
|
return null
|
|
}
|
|
})
|
|
],
|
|
pages: {
|
|
signIn: '/auth/signin',
|
|
},
|
|
callbacks: {
|
|
async jwt({ token, user }) {
|
|
if (user) {
|
|
token.id = user.id
|
|
token.email = user.email
|
|
token.name = user.name
|
|
}
|
|
return token
|
|
},
|
|
async session({ session, token }) {
|
|
if (session.user) {
|
|
session.user.id = token.id as string
|
|
session.user.email = token.email as string
|
|
}
|
|
return session
|
|
}
|
|
},
|
|
session: {
|
|
strategy: 'jwt',
|
|
maxAge: 30 * 24 * 60 * 60, // 30 days
|
|
}
|
|
}
|
|
|
|
export const { handlers, auth, signIn, signOut } = NextAuth(config)
|
|
|
|
export { handlers as GET, handlers as POST }
|