-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
98 lines (91 loc) · 2.52 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import type { Provider } from "next-auth/providers";
import { Usuario } from "@/lib/definitions";
import { ZodError } from "zod";
import { signInSchema } from "@/lib/zod";
const providers: Provider[] = [
CredentialsProvider({
// The name to display on the sign in form (e.g. "Sign in with...")
name: "Credenciales",
credentials: {
// id: { label: "ID", type: "text", placeholder: "PER-XXXXXXXX" },
// password: { label: "Contraseña", type: "password" },
id: {},
password: {},
},
async authorize(credentials, request) {
// TODO: Implementar autenticación
try {
const { id, password } = await signInSchema.parseAsync(credentials);
console.log("credentials: ", credentials.id, credentials.password);
const user: Usuario = {
id: id,
Nombre: "Pepe",
Apellidos: "Perez",
Foto: "https://via.placeholder.com/150",
Password: password,
};
if (user) {
return user;
} else {
return null;
}
} catch (error) {
if (error instanceof ZodError) {
return null;
} else {
return null;
}
}
},
}),
];
export const providerMap = providers.map((provider) => {
if (typeof provider === "function") {
const providerData = provider();
return { id: providerData.id, name: providerData.name };
} else {
return { id: provider.id, name: provider.name };
}
});
export const { auth, handlers, signIn, signOut } = NextAuth({
providers,
pages: {
signIn: "/signin",
},
session: {
strategy: "jwt",
maxAge: 30 * 24 * 60 * 60, // 30 days
},
jwt: {
// signingKey: process.env.NEXTAUTH_SECRET,
},
callbacks: {
async signIn({ user, account, profile, email, credentials }: any) {
// TODO: Implementar autenticación
const isAllowedToSignIn = true;
if (isAllowedToSignIn) {
return true;
} else {
// Return false to display a default error message
return false;
// Or you can return a URL to redirect to:
// return '/unauthorized'
}
},
async redirect({ url, baseUrl }: any) {
return baseUrl;
},
async session({ session, token }: any) {
session.user = token.user;
return session;
},
async jwt({ token, user }: any) {
if (user) {
token.user = user;
}
return token;
},
},
});