The main issue here is the inability to export authOptions
from
app/api/auth/[...nextauth]/route.ts
due to it being a reserved file for setting up authentication middleware in NextJS.
A workaround would be to store the authOptions
configuration in a separate file, such as auth.ts
, located in a designated folder like lib
(e.g., app/lib/auth.ts
), and then import it into your route.ts
file.
Here's an example of how you could structure the files:
# 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;
}
}
};
To utilize the configuration, you can import it as follows:
# 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 }