-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
74 lines (64 loc) · 2.15 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
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { authConfig } from "./auth.config";
import { z } from "zod";
import bcrypt from "bcrypt";
import { User, UserSchema } from "./lib/definitions";
import { isOfTypeWithErrors, ValidationError } from "./lib/validationProcessor";
function getUser(username: string): User | undefined {
const user1 = {
username: process.env.USERNAME1 ?? "",
password: process.env.PASSWORD1 ?? "",
userid: process.env.USERID1 ? parseInt(process.env.USERID1) : 0,
};
const user2 = {
username: process.env.USERNAME2 ?? "",
password: process.env.PASSWORD2 ?? "",
userid: process.env.USERID2 ? parseInt(process.env.USERID2) : 0,
};
const guestUser = {
username: "Guest",
password: "",
userid: 3,
};
const [validUser1, errors1] = isOfTypeWithErrors(user1, UserSchema);
const [validUser2, errors2] = isOfTypeWithErrors(user2, UserSchema);
if (!validUser1) {
console.error("Validation failed:", errors1);
throw new ValidationError(errors1);
}
if (!validUser2) {
console.error("Validation failed:", errors2);
throw new ValidationError(errors1);
}
const allowedUsers = [user1, user2, guestUser];
const user = allowedUsers.filter((user) => user.username === username);
return user[0];
}
export const { auth, signIn, signOut, handlers } = NextAuth({
...authConfig,
providers: [
Credentials({
async authorize(credentials) {
const parsedCredentials = z
.object({
username: z.string(),
password: z.string(),
})
.safeParse(credentials);
if (parsedCredentials.success) {
const { username, password } = parsedCredentials.data;
const user = getUser(username);
if (!user) return null;
if (user.username === "Guest") return user;
const passwordsMatch = await bcrypt.compare(password, user.password);
if (passwordsMatch) return user;
} else {
console.table(parsedCredentials.error.issues);
}
console.log("Invalid credentials");
return null;
},
}),
],
});