-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathauth.ts
More file actions
137 lines (132 loc) · 4.11 KB
/
auth.ts
File metadata and controls
137 lines (132 loc) · 4.11 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import NextAuth from "next-auth";
import { ZodError } from "zod";
import Credentials from "next-auth/providers/credentials";
import { signInSchema } from "@/app/lib/zod";
import { verifyPassword } from "@/app/utils/password";
import { db } from '@/app/db';
import { users } from '@/app/db/schema';
import Feishu from "@/app/auth/providers/feishu";
import Wecom from "@/app/auth/providers/wecom";
import Dingding from "@/app/auth/providers/dingding";
import { eq } from 'drizzle-orm';
let authProviders: any[] = [];
if (process.env.FEISHU_AUTH_STATUS === 'ON') {
const feishuAuth = Feishu({
clientId: process.env.FEISHU_CLIENT_ID!,
clientSecret: process.env.FEISHU_CLIENT_SECRET!,
});
authProviders.push(feishuAuth);
}
if (process.env.WECOM_AUTH_STATUS === 'ON') {
const wecomAuth = Wecom({
clientId: process.env.WECOM_CLIENT_ID!,
clientSecret: process.env.WECOM_CLIENT_SECRET!,
});
authProviders.push(wecomAuth);
}
if (process.env.DINGDING_AUTH_STATUS === 'ON') {
const dingdingAuth = Dingding({
clientId: process.env.DINGDING_CLIENT_ID!,
clientSecret: process.env.DINGDING_CLIENT_SECRET!,
});
authProviders.push(dingdingAuth);
}
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
...authProviders,
Credentials({
// You can specify which fields should be submitted, by adding keys to the `credentials` object.
// e.g. domain, username, password, 2FA token, etc.
credentials: {
email: {},
password: {},
},
authorize: async (credentials) => {
try {
const { email, password } = await signInSchema.parseAsync(credentials);
const user = await db.query.users
.findFirst({
where: eq(users.email, email)
})
if (!user || !user.password) {
return null;
}
const passwordMatch = await verifyPassword(password, user.password);
if (passwordMatch) {
return {
id: user.id,
name: user.name,
email: user.email,
isAdmin: user.isAdmin || false,
};
} else {
return null;
}
} catch (error) {
if (error instanceof ZodError) {
// 如果验证失败,返回 null 表示凭据无效
return null;
}
// 处理其他错误
throw error;
}
},
}),
],
pages: {
error: '/auth/error', // 自定义错误页面
},
callbacks: {
async jwt({ token, user, account }) {
if (user) {
token.id = user.id;
token.isAdmin = user.isAdmin;
}
if (account?.provider === "credentials" && token.sub) {
token.provider = 'credentials';
}
if (account?.provider === "feishu" && token.sub) {
const dbUser = await db.query.users.findFirst({
where: eq(users.feishuUserId, account.providerAccountId)
});
if (dbUser) {
token.id = dbUser.id;
token.isAdmin = dbUser.isAdmin || false;
}
token.provider = 'feishu';
}
if (account?.provider === "wecom" && token.sub) {
const dbUser = await db.query.users.findFirst({
where: eq(users.wecomUserId, account.providerAccountId)
});
if (dbUser) {
token.id = dbUser.id;
token.isAdmin = dbUser.isAdmin || false;
}
token.provider = 'wecom';
}
if (account?.provider === "dingding" && token.sub) {
const dbUser = await db.query.users.findFirst({
where: eq(users.dingdingUnionId, account.providerAccountId)
});
if (dbUser) {
token.id = dbUser.id;
token.isAdmin = dbUser.isAdmin || false;
}
token.provider = 'dingding';
}
return token;
},
async session({ session, token }) {
if (token) {
session.user = {
...session.user, // 保留已有的属性
id: String(token.id),
isAdmin: Boolean(token.isAdmin), // 添加 isAdmin
provider: token.provider as string,
};
}
return session;
},
},
})