60 lines
1.3 KiB
TypeScript
60 lines
1.3 KiB
TypeScript
'use server'
|
|||
|
|
|
||
|
|
import { SignupFormSchema, FormState } from '@/app/lib/definitions'
|
||
|
|
import { createSession, deleteSession } from '@/app/lib/session'
|
||
|
|
import { redirect } from 'next/navigation'
|
||
|
|
|
||
|
|
export async function logout() {
|
||
|
|
await deleteSession()
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function authorization(
|
||
|
|
state: string | undefined,
|
||
|
|
formData: FormData,
|
||
|
|
) {
|
||
|
|
try {
|
||
|
|
console.log(formData);
|
||
|
|
} catch (error) {
|
||
|
|
if (error) {
|
||
|
|
return 'Something went wrong.';
|
||
|
|
}
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function registration(
|
||
|
|
state: string | undefined,
|
||
|
|
formData: FormData,
|
||
|
|
) {
|
||
|
|
const validatedFields = SignupFormSchema.safeParse({
|
||
|
|
name: formData.get('full_name'),
|
||
|
|
email: formData.get('email'),
|
||
|
|
password: formData.get('password'),
|
||
|
|
})
|
||
|
|
|
||
|
|
if (!validatedFields.success) {
|
||
|
|
return 'Something went wrong from valid.';
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
const { name, email, password } = validatedFields.data;
|
||
|
|
const response = await fetch('http://localhost:8080/api/auth/register', {
|
||
|
|
method: 'POST',
|
||
|
|
body: JSON.stringify({ fullName: name, email, password }),
|
||
|
|
headers: {
|
||
|
|
"Content-Type": "application/json",
|
||
|
|
"Accept": "application/json"
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
let parsed = await response.json();
|
||
|
|
await createSession(parsed.token);
|
||
|
|
} catch (error) {
|
||
|
|
if (error) {
|
||
|
|
return 'Something went wrong. error';
|
||
|
|
}
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
|
||
|
|
redirect('/pages/dashboard');
|
||
|
|
}
|