-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.config.ts
60 lines (48 loc) · 1.53 KB
/
auth.config.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
import { defineConfig } from 'auth-astro';
import { db, eq, User } from 'astro:db';
import Credentials from '@auth/core/providers/credentials';
import bcrypt from 'bcryptjs';
import type { Adapter, AdapterUser } from '@auth/core/adapters';
export default defineConfig({
providers: [
//TODO:
// GitHub({
// clientId: import.meta.env.GITHUB_CLIENT_ID,
// clientSecret: import.meta.env.GITHUB_CLIENT_SECRET,
// }),
Credentials({
credentials: {
email: {label: 'Correo', type:'email'},
password: {label: 'Contraseña', type:'password'},
},
authorize: async ( { email, password }) => {
const [user] = await db
.select()
.from(User)
.where(eq(User.email, email as string));
if ( !user ) {
throw new Error (' Usuario no encontrado')
}
if (!bcrypt.compareSync(password as string, user.password)) {
throw new Error(' Contraseña incorrecta')
}
//no enviamos la contraseña al cliente
const { password: _, ...rest} = user;
console.log({rest});
return rest;
},
}),
],
callbacks: {
jwt: ({ token, user }) => {
if (user) {
token.user = user;
}
return token;
},
session: ({ session, token }) => {
session.user = token.user as AdapterUser;
return session;
}
}
});