-
Notifications
You must be signed in to change notification settings - Fork 1
/
actions.ts
59 lines (53 loc) · 1.66 KB
/
actions.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
"use server";
import { AuthError } from "next-auth";
import { signIn } from "@/auth";
import { registerUser } from "./services/user";
import { DatabaseErrorType } from "./types/errorTypes";
import { DatabaseError } from "pg";
export const loginAction = async (formData: FormData) => {
try {
await signIn("credentials", formData);
} catch (error) {
if (error instanceof AuthError) {
switch (error.type) {
case "CredentialsSignin":
return "Invalid credentials.";
default:
return "Something went wrong.";
}
}
throw error;
}
};
export const registerAction = async (formData: FormData) => {
try {
const name = formData.get("name")?.toString();
const jobTitle = formData.get("jobTitle")?.toString();
const department = formData.get("department")?.toString();
const email = formData.get("email")?.toString();
const password = formData.get("password")?.toString();
await registerUser(name, email, password, department, jobTitle);
} catch (e) {
const dbError = e as DatabaseError;
let errorType: DatabaseErrorType;
if (dbError.message === "Email already registered 23505")
errorType = "UniqueConstraintViolation";
else if (dbError.message === "Error registering the user")
errorType = "ConnectionError";
else errorType = "GeneralError";
return errorType;
}
try {
await signIn("credentials", formData);
} catch (error) {
if (error instanceof AuthError) {
switch (error.type) {
case "CredentialsSignin":
return "Invalid credentials.";
default:
return "Something went wrong.";
}
}
throw error;
}
};