Files
no-copy-frontend/src/app/actions/auth.ts
T

124 lines
2.8 KiB
TypeScript
Raw Normal View History

2025-11-27 21:33:53 +07:00
'use server'
2025-11-28 15:28:15 +07:00
import { SignupFormSchema, loginFormSchema } from '@/app/lib/definitions';
2025-12-02 17:11:53 +07:00
import { createSession, deleteSession, getSessionData } from '@/app/lib/session';
2025-11-27 21:33:53 +07:00
import { redirect } from 'next/navigation'
export async function logout() {
2025-12-02 17:11:53 +07:00
const token = await getSessionData('token');
2025-11-28 15:28:15 +07:00
try {
2025-12-02 17:11:53 +07:00
await fetch('http://localhost/api/auth/logout', {
2025-11-28 15:28:15 +07:00
method: 'POST',
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": `Bearer ${token}`,
}
});
} catch (error) {
throw error;
}
await deleteSession();
redirect('/login');
2025-11-27 21:33:53 +07:00
}
export async function authorization(
state: string | undefined,
formData: FormData,
) {
2025-11-28 15:28:15 +07:00
const validatedFields = loginFormSchema.safeParse({
email: formData.get('email'),
password: formData.get('password'),
})
if (!validatedFields.success) {
return 'error';
}
2025-11-27 21:33:53 +07:00
try {
2025-11-28 15:28:15 +07:00
const { email, password } = validatedFields.data;
2025-12-02 17:11:53 +07:00
const response = await fetch('http://localhost/api/auth/login', {
2025-11-28 15:28:15 +07:00
method: 'POST',
body: JSON.stringify({
email: email,
password: password
}),
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
});
if (response.ok) {
let parsed = await response.json();
2025-12-02 17:11:53 +07:00
await createSession(parsed.token, parsed.email);
2025-11-28 15:28:15 +07:00
} else {
throw new Error('Something went wrong. from server');
}
2025-11-27 21:33:53 +07:00
} catch (error) {
if (error) {
2025-11-28 15:28:15 +07:00
return 'Something went wrong. from server';
2025-11-27 21:33:53 +07:00
}
throw error;
}
2025-11-28 15:28:15 +07:00
redirect('/pages/dashboard');
2025-11-27 21:33:53 +07:00
}
export async function registration(
2025-11-28 12:38:14 +07:00
state: Record<string, string> | undefined,
2025-11-27 21:33:53 +07:00
formData: FormData,
) {
const validatedFields = SignupFormSchema.safeParse({
2025-11-28 12:38:14 +07:00
full_name: formData.get('full_name'),
2025-11-27 21:33:53 +07:00
email: formData.get('email'),
password: formData.get('password'),
2025-11-28 12:38:14 +07:00
confirm_password: formData.get('confirm_password'),
2025-11-27 21:33:53 +07:00
})
if (!validatedFields.success) {
2025-11-28 12:38:14 +07:00
const errors: Record<string, string> = {}
JSON.parse(validatedFields.error.message).forEach((obj: {
message: string,
path: string
}) => {
if (errors[obj.path[0]]) {
errors[obj.path[0]] = `${errors[obj.path[0]]} ${obj.message}`;
} else {
errors[obj.path[0]] = obj.message;
}
});
return errors;
2025-11-27 21:33:53 +07:00
}
try {
2025-11-28 12:38:14 +07:00
const { full_name, email, password } = validatedFields.data;
2025-12-02 17:11:53 +07:00
const response = await fetch('http://localhost/api/auth/register', {
2025-11-27 21:33:53 +07:00
method: 'POST',
2025-11-28 12:38:14 +07:00
body: JSON.stringify({ fullName: full_name, email, password }),
2025-11-27 21:33:53 +07:00
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
});
2025-11-28 15:28:15 +07:00
if (response.ok) {
let parsed = await response.json();
2025-12-02 17:11:53 +07:00
await createSession(parsed.token, email);
2025-11-28 15:28:15 +07:00
} else {
throw new Error('Something went wrong. from server');
}
2025-11-27 21:33:53 +07:00
} catch (error) {
if (error) {
2025-11-28 12:38:14 +07:00
const errors: Record<string, string> = {}
errors['server'] = 'Something went wrong. from server'
return errors;
2025-11-27 21:33:53 +07:00
}
throw error;
}
redirect('/pages/dashboard');
}