2024-11-14 12:16:05 -05:00
|
|
|
'use server';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-11-14 12:16:05 -05:00
|
|
|
import { z } from 'zod';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-11-15 10:13:21 -05:00
|
|
|
import { createUser, getUser } from '@/lib/db/queries';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-11-14 12:16:05 -05:00
|
|
|
import { signIn } from './auth';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
const authFormSchema = z.object({
|
|
|
|
|
email: z.string().email(),
|
|
|
|
|
password: z.string().min(6),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export interface LoginActionState {
|
2024-11-14 12:16:05 -05:00
|
|
|
status: 'idle' | 'in_progress' | 'success' | 'failed' | 'invalid_data';
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const login = async (
|
|
|
|
|
_: LoginActionState,
|
2024-11-15 13:00:15 -05:00
|
|
|
formData: FormData,
|
2024-10-11 18:00:22 +05:30
|
|
|
): Promise<LoginActionState> => {
|
|
|
|
|
try {
|
|
|
|
|
const validatedData = authFormSchema.parse({
|
2024-11-14 12:16:05 -05:00
|
|
|
email: formData.get('email'),
|
|
|
|
|
password: formData.get('password'),
|
2024-10-11 18:00:22 +05:30
|
|
|
});
|
|
|
|
|
|
2024-11-14 12:16:05 -05:00
|
|
|
await signIn('credentials', {
|
2024-10-11 18:00:22 +05:30
|
|
|
email: validatedData.email,
|
|
|
|
|
password: validatedData.password,
|
|
|
|
|
redirect: false,
|
|
|
|
|
});
|
|
|
|
|
|
2024-11-14 12:16:05 -05:00
|
|
|
return { status: 'success' };
|
2024-10-11 18:00:22 +05:30
|
|
|
} catch (error) {
|
|
|
|
|
if (error instanceof z.ZodError) {
|
2024-11-14 12:16:05 -05:00
|
|
|
return { status: 'invalid_data' };
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
|
2024-11-14 12:16:05 -05:00
|
|
|
return { status: 'failed' };
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export interface RegisterActionState {
|
|
|
|
|
status:
|
2024-11-14 12:16:05 -05:00
|
|
|
| 'idle'
|
|
|
|
|
| 'in_progress'
|
|
|
|
|
| 'success'
|
|
|
|
|
| 'failed'
|
|
|
|
|
| 'user_exists'
|
|
|
|
|
| 'invalid_data';
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const register = async (
|
|
|
|
|
_: RegisterActionState,
|
2024-11-15 13:00:15 -05:00
|
|
|
formData: FormData,
|
2024-10-11 18:00:22 +05:30
|
|
|
): Promise<RegisterActionState> => {
|
|
|
|
|
try {
|
|
|
|
|
const validatedData = authFormSchema.parse({
|
2024-11-14 12:16:05 -05:00
|
|
|
email: formData.get('email'),
|
|
|
|
|
password: formData.get('password'),
|
2024-10-11 18:00:22 +05:30
|
|
|
});
|
|
|
|
|
|
2024-11-15 10:37:01 -05:00
|
|
|
const [user] = await getUser(validatedData.email);
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
if (user) {
|
2024-11-14 12:16:05 -05:00
|
|
|
return { status: 'user_exists' } as RegisterActionState;
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
2024-11-15 12:18:17 -05:00
|
|
|
await createUser(validatedData.email, validatedData.password);
|
|
|
|
|
await signIn('credentials', {
|
|
|
|
|
email: validatedData.email,
|
|
|
|
|
password: validatedData.password,
|
|
|
|
|
redirect: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return { status: 'success' };
|
2024-10-11 18:00:22 +05:30
|
|
|
} catch (error) {
|
|
|
|
|
if (error instanceof z.ZodError) {
|
2024-11-14 12:16:05 -05:00
|
|
|
return { status: 'invalid_data' };
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
|
2024-11-14 12:16:05 -05:00
|
|
|
return { status: 'failed' };
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
};
|