chore: Update auth files for NextAuth v5 compatibility
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
This commit is contained in:
@@ -2,8 +2,9 @@ import NextAuth from 'next-auth'
|
|||||||
import GitHub from 'next-auth/providers/github'
|
import GitHub from 'next-auth/providers/github'
|
||||||
import Google from 'next-auth/providers/google'
|
import Google from 'next-auth/providers/google'
|
||||||
import Credentials from 'next-auth/providers/credentials'
|
import Credentials from 'next-auth/providers/credentials'
|
||||||
|
import type { NextAuthConfig } from 'next-auth'
|
||||||
|
|
||||||
const handler = NextAuth({
|
export const config: NextAuthConfig = {
|
||||||
providers: [
|
providers: [
|
||||||
GitHub({
|
GitHub({
|
||||||
clientId: process.env.GITHUB_ID,
|
clientId: process.env.GITHUB_ID,
|
||||||
@@ -49,8 +50,8 @@ const handler = NextAuth({
|
|||||||
},
|
},
|
||||||
async session({ session, token }) {
|
async session({ session, token }) {
|
||||||
if (session.user) {
|
if (session.user) {
|
||||||
session.user.id = token.id
|
session.user.id = token.id as string
|
||||||
session.user.email = token.email
|
session.user.email = token.email as string
|
||||||
}
|
}
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
@@ -59,6 +60,8 @@ const handler = NextAuth({
|
|||||||
strategy: 'jwt',
|
strategy: 'jwt',
|
||||||
maxAge: 30 * 24 * 60 * 60, // 30 days
|
maxAge: 30 * 24 * 60 * 60, // 30 days
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
export { handler as GET, handler as POST }
|
export const { handlers, auth, signIn, signOut } = NextAuth(config)
|
||||||
|
|
||||||
|
export { handlers as GET, handlers as POST }
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { getServerSession } from 'next-auth'
|
import { auth } from '@/app/api/auth/[...nextauth]/route'
|
||||||
import { redirect } from 'next/navigation'
|
import { redirect } from 'next/navigation'
|
||||||
|
|
||||||
export async function requireAuth() {
|
export async function requireAuth() {
|
||||||
const session = await getServerSession()
|
const session = await auth()
|
||||||
|
|
||||||
if (!session || !session.user) {
|
if (!session || !session.user) {
|
||||||
redirect('/auth/signin')
|
redirect('/auth/signin')
|
||||||
|
|||||||
Reference in New Issue
Block a user