-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathauth.ts
More file actions
44 lines (41 loc) · 1.27 KB
/
auth.ts
File metadata and controls
44 lines (41 loc) · 1.27 KB
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
import NextAuth from "next-auth";
import { PrismaAdapter } from "@auth/prisma-adapter";
import authConfig from "@/auth.config";
import { db } from "@/lib/db";
export const { handlers, signIn, signOut, auth } = NextAuth({
adapter: PrismaAdapter(db),
...authConfig,
session: { strategy: "jwt" },
callbacks: {
// jwt() se ejecuta cada vez que se crea o actualiza un token JWT.
// Aquí es donde puedes agregar información adicional al token.
jwt({ token, user }) {
if (user) {
token.role = user.role;
}
return token;
},
// session() se utiliza para agregar la información del token a la sesión del usuario,
// lo que hace que esté disponible en el cliente.
session({ session, token }) {
if (session.user) {
session.user.role = token.role;
}
return session;
},
},
events: {
// El evento linkAccount se dispara cuando una cuenta (proveedor OAuth: GitHub, Google, Facebook, etc.) se vincula a un usuario existente en tu base de datos.
async linkAccount({ user }) {
await db.user.update({
where: { id: user.id },
data: {
emailVerified: new Date(),
},
});
},
},
pages: {
signIn: "/login",
},
});