-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
77 lines (68 loc) · 1.75 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
import NextAuth, { Session, JWT, DefaultSession } from 'next-auth';
import Credentials from 'next-auth/providers/credentials';
// TypeScript type declaration extensions for additional session and JWT fields
declare module 'next-auth' {
interface Session {
user: {
id?: string;
} & DefaultSession['user'];
}
interface JWT {
id?: string;
}
}
// Simulated in-memory user data
const users = [
{
id: '1',
email: '[email protected]'
}
];
export const { auth, handlers, signIn, signOut } = NextAuth({
providers: [
Credentials({
credentials: {
email: { label: 'Email', type: 'email' }
},
authorize: async (credentials, req) => {
if (!credentials) {
throw new Error('No credentials provided');
}
const { email } = credentials;
console.log('credentials', credentials);
if (typeof email !== 'string') {
throw new Error('Invalid email type');
}
// Find the user with the given email
const user = users.find((u) => u.email === email);
console.log('user', user);
if (user) {
console.log('user', user);
return { id: user.id, email: user.email };
}
// In case of an invalid email
throw new Error('Invalid email');
}
})
],
pages: {
signIn: '/login' // Your custom sign-in page
},
callbacks: {
async session({ session, token }) {
if (token && session.user) {
console.log('session', session);
session.user.id = token.id as string;
}
return session;
},
async jwt({ token, user }) {
if (user) {
console.log('user', user);
token.id = user.id;
}
return token;
}
}
});
export default auth;