The issue is that you cannot export authOptions
from app/api/auth/[...nextauth]/route.ts
because it's a reserved file used for configuring an authentication middleware in NextJS.
So placing authOptions
config in an auth.ts
file in a lib
folder for example (app/lib/auth.ts
) and then importing it into your route.ts
file is probably the way to go.
See example below:
# app/lib/auth.ts
import { NextAuthOptions } from "next-auth";
import DuendeIdentityServer6 from "next-auth/providers/duende-identity-server6";
export const authOptions: NextAuthOptions = {
session: {
strategy: "jwt",
},
providers: [
DuendeIdentityServer6({
id: 'id-server',
clientId: 'clientApp',
clientSecret: 'strongSecret',
issuer: 'http://localhost:5055',
authorization: {
params: {
scope: 'openid profile demoApp'
}
},
idToken: true,
})
],
callbacks: {
async jwt({token, profile, account}) {
if (profile) {
token.username = profile.username;
}
if (account) {
token.access_token = account.access_token;
}
return token;
},
async session({ session, token }) {
if (token) {
session.user.username = token.username;
}
return session;
}
}
};
Import the config:
# app/api/auth/[...nextauth]/route.ts
import { authOptions } from "@/app/lib/auth";
import NextAuth from "next-auth/next";
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST }
export
from authOptions: – Poltroonery