I’m integrating Okta as an OIDC provider in a Next.js (App Router) application using NextAuth.js. My setup is standard: I use the well-known endpoint, client ID/secret, and the recommended scopes (openid email profile). The provider config in NextAuth looks like this:
function createInternalOktaProvider() {
return {
id: 'okta-internal',
name: 'SSO',
type: 'oauth' as const,
wellKnown: `${process.env.NEXT_PUBLIC_OKTA_ISSUER!}/.well-known/openid-configuration`,
authorization: {
params: {
scope: 'openid email profile',
},
},
idToken: true,
clientId: process.env.INTERNAL_OKTA_CLIENT_ID!,
clientSecret: process.env.INTERNAL_OKTA_CLIENT_SECRET!,
issuer: process.env.NEXT_PUBLIC_OKTA_ISSUER!,
profile(profile: OIDCProfile) {
return {
id: profile.sub,
name: profile.name ?? `${profile.given_name || ''}`.trim(),
email: profile.email || '',
provider: 'okta-internal',
environment: 'internal',
};
},
};
}
export const authOptions: NextAuthOptions = {
providers: [createInternalOktaProvider()],
session: {
strategy: 'jwt',
maxAge: 30 * 24 * 60 * 60, // 30 days
},
secret: process.env.NEXTAUTH_SECRET,
pages: {
signIn: '/auth/signin',
error: '/auth/error',
},
callbacks: {
async jwt({ token, account, profile }) {
if (account && profile) {
const oidcProfile = profile as OIDCProfile & { environment?: string };
token.id = oidcProfile.sub;
token.provider = account.provider || 'okta-internal';
token.environment = oidcProfile.environment || 'internal';
console.log('JWT Callback - Token');
}
return token;
},
async session({ session, token }) {
if (token.id && session.user) {
session.user.id = token.id;
session.user.provider = token.provider;
session.user.environment = token.environment;
console.log('Session Callback - User logged in:', session.user);
} else {
console.log('Session Callback - No user logged in');
}
return session;
},
},
};
Problem:
When I hit the okta chicklet having the oidc endpoint, it takes me to the (/api/auth/callback/okta-internal) which is authored in okta sign-in redirect config, I get the following error from NextAuth:
OAUTH_CALLBACK_ERROR next auth - error: i: state missing from the response
post this it lands me on the /auth/signin?callbackUrl=https%3A%2F%2Fmy.company.com%2F&error=OAuthCallback
Ideally it should take to sites home page with a logged in user. Am I missing something here?