-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
46 lines (42 loc) · 1.21 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import NextAuth from "next-auth";
import authConfig from "@/auth.config";
import { MikroOrmAdapter } from "@auth/mikro-orm-adapter";
import "next-auth/jwt";
import config from "./mikro-orm.config";
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: MikroOrmAdapter(config),
debug: !!process.env.AUTH_DEBUG,
secret: process.env.AUTH_SECRET,
...authConfig,
basePath: "/auth",
session: { strategy: "jwt" },
callbacks: {
authorized({ request, auth }) {
const { pathname } = request.nextUrl;
if (pathname === "/middleware-example") return !!auth;
return true;
},
jwt({ token, trigger, session, account }) {
if (trigger === "update") token.name = session.user.name;
if (account?.provider === "keycloak") {
return { ...token, accessToken: account.access_token };
}
return token;
},
async session({ session, token }) {
if (token?.accessToken) session.accessToken = token.accessToken;
return session;
},
},
experimental: { enableWebAuthn: true },
});
declare module "next-auth" {
interface Session {
accessToken?: string;
}
}
declare module "next-auth/jwt" {
interface JWT {
accessToken?: string;
}
}