#NextAuth, Prisma Adapter and Keycloak, redirects back to sign in again after success login

2 messages · Page 1 of 1 (latest)

humble kernel
#

Hey there,

I've implemented the auth system via prisma and keycloak as follows:

const handler = NextAuth({
    providers: [
        KeycloakProvider({
            clientId: process.env.KEYCLOAK_ID as string,
            clientSecret: process.env.KEYCLOAK_SECRET as string,
            issuer: process.env.KEYCLOAK_ISSUER as string,
            name: 'LoID',
            httpOptions: {
                timeout: 6000,
            },
            authorization: {
                params: {
                    scope: 'openid email profile roles groups',
                },
            }
        }),
    ],
    adapter: PrismaAdapter(prisma),
    debug: process.env.NODE_ENV === 'development',
});

With the following configs for the prisma client:

import { PrismaClient } from '@prisma/client';

declare global {
    var prisma: PrismaClient | undefined;
}

let prisma: PrismaClient;

if (process.env.NODE_ENV === 'production') {
    prisma = new PrismaClient();
} else {
    if (!global.prisma) {
        global.prisma = new PrismaClient();
    }
    prisma = global.prisma;
}

prisma.$use(async (params, next) => {
    if (params.action == "create" && params.model == "Account") {
        delete params.args.data["not-before-policy"];
    }

    const result = await next(params);
    return result;
});

export default prisma;

However, every time I click on the Sign in with LoID, it goes all the way through the authentication and as soon as it reaches the dashboard, which is a protected path by the middleware, it redirects back to the sign in page. here's the middleware config:

export { default } from 'next-auth/middleware';

export const config = {
    matcher: ['/dashboard/:path*', '/api/internal/:path*'],
};

I'm quite clueless at this point what the issue might be. here's a video of my Network Tab in chrome for a better view of the situation. I'm also not getting anything useful in the logs so far.

I appreciate any help 🙂 tnx

humble kernel
#

I'm using:

"next-auth": "^4.18.8",
"@next-auth/prisma-adapter": "^1.0.5"