-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
102 lines (94 loc) · 2.35 KB
/
Copy pathauth.ts
File metadata and controls
102 lines (94 loc) · 2.35 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
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
99
100
101
102
import { UserMethods } from "@/database/user.db";
import NextAuth, { User } from "next-auth";
import Credentials from "next-auth/providers/credentials";
import GithubProvider from "next-auth/providers/github";
import Nodemailer from "next-auth/providers/nodemailer";
import { PrismaAdapter } from "@auth/prisma-adapter";
import db from "@/database";
declare module "next-auth" {
interface Session {
user: User & {
id: string;
email: string;
emailVerified: Date | null;
name: string;
role: string;
active: string | null;
image: string;
};
}
}
const userMethods = new UserMethods();
export const { handlers, signIn, signOut, auth } = NextAuth({
pages: {
newUser: "/register",
},
adapter: PrismaAdapter(db),
session: {
strategy: "jwt",
},
providers: [
Credentials({
credentials: {
email: {
type: "email",
label: "E-mail:",
placeholder: "Type your e-mail...",
},
password: {
type: "password",
label: "Password",
placeholder: "Type your password...",
},
},
async authorize(credentials) {
if (!credentials.email || !credentials.password) {
return null;
}
const { password, email } = credentials as LoginPayload;
const user = await userMethods.login({ password, email });
return user;
},
}),
GithubProvider({}),
Nodemailer({
server: {
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
},
from: process.env.EMAIL_FROM,
}),
],
callbacks: {
jwt({ token, user }) {
return { userData: user, ...token };
},
session({ session, token }) {
const userData = token.userData as {
id: string;
email: string;
emailVerified: Date | null;
name: string;
role: string;
active: string | null;
image: string;
};
const { active, email, emailVerified, id, image, name, role } = userData;
session.user = {
...session.user,
id,
name,
email,
emailVerified,
image,
role,
active,
};
return session;
},
},
});