web/auth.ts
GHOSCHT b4503db688
Add PrismaORM
preparation for access_token refresh, since JWT cookies cannot be refreshed from the server side
2024-05-19 18:08:47 +02:00

45 lines
1.2 KiB
TypeScript

import NextAuth,{ type DefaultSession } from "next-auth";
import Authentik from "next-auth/providers/authentik";
import { PrismaAdapter } from "@auth/prisma-adapter"
import { PrismaClient } from "@prisma/client"
const prisma = new PrismaClient()
declare module "next-auth" {
interface Session {
access_token: string | null;
}
}
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(prisma),
providers: [Authentik({
clientId: process.env.AUTH_OIDC_CLIENT_ID,
clientSecret: process.env.AUTH_OIDC_CLIENT_SECRET,
issuer: process.env.AUTH_OIDC_ISSUER,
authorization: { params: { scope: 'openid profile email offline_access' } },
})],
callbacks: {
async jwt({token, account}) {
if (account) {
token = Object.assign({}, token, { access_token: account.access_token });
}
return token
},
async session({session, user}) {
const getToken = await prisma.account.findFirst({
where: {
userId: user.id,
},
});
let accessToken: string | null = null;
if (getToken) {
accessToken = getToken.access_token!;
}
session.access_token = accessToken;
return session;
}
}
});