-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
69 lines (68 loc) · 2.12 KB
/
Copy pathauth.ts
File metadata and controls
69 lines (68 loc) · 2.12 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
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { eq } from "drizzle-orm";
import db from "@/lib/supabase/db";
import { users } from "@/lib/supabase/schema";
import { verifyPassword } from "@/lib/password";
import { localAuthenticate, localMode } from "@/lib/local-store";
export const { handlers, auth, signIn, signOut } = NextAuth({
secret:
process.env.AUTH_SECRET ||
process.env.NEXTAUTH_SECRET ||
"clubmap-development-secret-change-me",
session: { strategy: "jwt" },
pages: { signIn: "/login" },
providers: [
Credentials({
credentials: { email: {}, password: {} },
authorize: async (credentials) => {
const email = String(credentials.email || "")
.trim()
.toLowerCase();
const password = String(credentials.password || "");
if (localMode) {
const user = await localAuthenticate(email, password);
return user
? {
id: user.id,
name: `${user.firstName} ${user.lastName}`,
email: user.email,
role: user.role,
universityVerified: user.universityVerified,
}
: null;
}
const [user] = await db
.select()
.from(users)
.where(eq(users.email, email))
.limit(1);
if (!user || !(await verifyPassword(password, user.passwordHash)))
return null;
return {
id: user.id,
name: `${user.firstName} ${user.lastName}`,
email: user.email,
role: user.role,
universityVerified: user.universityVerified,
};
},
}),
],
callbacks: {
jwt({ token, user }) {
if (user) {
token.id = user.id;
token.role = user.role;
token.universityVerified = user.universityVerified;
}
return token;
},
session({ session, token }) {
session.user.id = String(token.id);
session.user.role = String(token.role);
session.user.universityVerified = Boolean(token.universityVerified);
return session;
},
},
});