128 lines
2.9 KiB
TypeScript
128 lines
2.9 KiB
TypeScript
'use server'
|
|
|
|
import { SignupFormSchema, loginFormSchema } from '@/app/lib/definitions';
|
|
import { createSession, deleteSession, getSessionData } from '@/app/lib/session';
|
|
import { redirect } from 'next/navigation';
|
|
|
|
const API_BASE_URL = process.env.NODE_ENV === 'development'
|
|
? 'http://localhost'
|
|
: '';
|
|
|
|
export async function logout() {
|
|
const token = await getSessionData('token');
|
|
|
|
try {
|
|
await fetch(`${API_BASE_URL}/api/auth/logout`, {
|
|
method: 'POST',
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json",
|
|
"Authorization": `Bearer ${token}`,
|
|
}
|
|
});
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
|
|
await deleteSession();
|
|
redirect('/login');
|
|
}
|
|
|
|
export async function authorization(
|
|
state: string | undefined,
|
|
formData: FormData,
|
|
) {
|
|
const validatedFields = loginFormSchema.safeParse({
|
|
email: formData.get('email'),
|
|
password: formData.get('password'),
|
|
})
|
|
|
|
if (!validatedFields.success) {
|
|
return 'error';
|
|
}
|
|
|
|
try {
|
|
const { email, password } = validatedFields.data;
|
|
const response = await fetch(`${API_BASE_URL}/api/auth/login`, {
|
|
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();
|
|
await createSession(parsed.token, parsed.email);
|
|
} else {
|
|
throw new Error('Something went wrong. from server');
|
|
}
|
|
|
|
} catch (error) {
|
|
if (error) {
|
|
return 'Something went wrong. from server';
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
redirect('/pages/dashboard');
|
|
}
|
|
|
|
export async function registration(
|
|
state: Record<string, string> | undefined,
|
|
formData: FormData,
|
|
) {
|
|
const validatedFields = SignupFormSchema.safeParse({
|
|
full_name: formData.get('full_name'),
|
|
email: formData.get('email'),
|
|
password: formData.get('password'),
|
|
confirm_password: formData.get('confirm_password'),
|
|
})
|
|
|
|
if (!validatedFields.success) {
|
|
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;
|
|
}
|
|
|
|
try {
|
|
const { full_name, email, password } = validatedFields.data;
|
|
const response = await fetch(`${API_BASE_URL}/api/auth/register`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ fullName: full_name, email, password }),
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json"
|
|
}
|
|
});
|
|
|
|
if (response.ok) {
|
|
let parsed = await response.json();
|
|
await createSession(parsed.token, email);
|
|
} else {
|
|
throw new Error('Something went wrong. from server');
|
|
}
|
|
} catch (error) {
|
|
if (error) {
|
|
const errors: Record<string, string> = {}
|
|
errors['server'] = 'Something went wrong. from server'
|
|
return errors;
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
redirect('/pages/dashboard');
|
|
} |