| [e1175d1] | 1 | 'use server'
|
|---|
| 2 |
|
|---|
| 3 | import { z } from 'zod';
|
|---|
| [69d38f6] | 4 | import { sql } from '@/app/lib/db';
|
|---|
| [25b259a] | 5 | import { signIn } from '@/auth';
|
|---|
| [bd7f7a7] | 6 | import bcrypt from "bcryptjs";
|
|---|
| [25b259a] | 7 | import { AuthError } from 'next-auth';
|
|---|
| [e1175d1] | 8 |
|
|---|
| [25b259a] | 9 | export async function authenticate(
|
|---|
| 10 | prevState: string | undefined,
|
|---|
| 11 | formData: FormData,
|
|---|
| 12 | ) {
|
|---|
| 13 | try {
|
|---|
| [2e0a138] | 14 | const redirectTo =
|
|---|
| [f9fef87] | 15 | (formData.get('redirectTo') as string)?.startsWith('/')
|
|---|
| 16 | ? (formData.get('redirectTo') as string)
|
|---|
| [794232d] | 17 | : '/dashboard';
|
|---|
| [2e0a138] | 18 |
|
|---|
| 19 | await signIn('credentials', {
|
|---|
| 20 | ...Object.fromEntries(formData),
|
|---|
| 21 | redirectTo,
|
|---|
| 22 | });
|
|---|
| [25b259a] | 23 | } catch (error) {
|
|---|
| 24 | if (error instanceof AuthError) {
|
|---|
| 25 | switch (error.type) {
|
|---|
| 26 | case 'CredentialsSignin':
|
|---|
| [2e0a138] | 27 | return 'Invalid email or password.';
|
|---|
| [25b259a] | 28 | default:
|
|---|
| [2e0a138] | 29 | return 'Something went wrong. Please try again.';
|
|---|
| [25b259a] | 30 | }
|
|---|
| 31 | }
|
|---|
| 32 | throw error;
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| [bd7f7a7] | 36 | export async function register(
|
|---|
| 37 | prevState: string | undefined,
|
|---|
| 38 | formData: FormData,
|
|---|
| 39 | ) {
|
|---|
| 40 | const schema = z.object({
|
|---|
| [2253a52] | 41 | user_name: z.string().min(1),
|
|---|
| [bd7f7a7] | 42 | email: z.string().email(),
|
|---|
| 43 | password: z.string().min(6),
|
|---|
| 44 | redirectTo: z.string().optional(),
|
|---|
| 45 | });
|
|---|
| 46 |
|
|---|
| 47 | const parsed = schema.safeParse({
|
|---|
| [2253a52] | 48 | user_name: formData.get('user_name'),
|
|---|
| [bd7f7a7] | 49 | email: formData.get('email'),
|
|---|
| 50 | password: formData.get('password'),
|
|---|
| 51 | redirectTo: formData.get('redirectTo'),
|
|---|
| 52 | });
|
|---|
| 53 |
|
|---|
| 54 | if (!parsed.success) {
|
|---|
| 55 | return 'Invalid form data.';
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| [2253a52] | 58 | const { user_name, email, password, redirectTo } = parsed.data;
|
|---|
| [bd7f7a7] | 59 |
|
|---|
| [9cae1de] | 60 | // sanitize redirect
|
|---|
| 61 | const safeRedirect =
|
|---|
| [794232d] | 62 | redirectTo?.startsWith('/') ? redirectTo : '/dashboard';
|
|---|
| [9cae1de] | 63 |
|
|---|
| [bd7f7a7] | 64 | const hashed = await bcrypt.hash(password, 10);
|
|---|
| 65 |
|
|---|
| [9cae1de] | 66 | try {
|
|---|
| [b04ba1e] | 67 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|---|
| 68 | await sql.begin(async (tx: any) => {
|
|---|
| 69 | const existing = await tx`SELECT user_id FROM "user" WHERE email=${email}`;
|
|---|
| 70 |
|
|---|
| 71 | if (existing.length > 0) {
|
|---|
| 72 | throw new Error('User already exists.');
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | await tx`
|
|---|
| 76 | INSERT INTO "user" (user_name, email, password)
|
|---|
| 77 | VALUES (${user_name}, ${email}, ${hashed})
|
|---|
| 78 | `;
|
|---|
| 79 | });
|
|---|
| 80 | } catch (e: any) {
|
|---|
| 81 | if (e instanceof Error && e.message === 'User already exists.') {
|
|---|
| 82 | return e.message;
|
|---|
| 83 | }
|
|---|
| [9cae1de] | 84 | return 'Failed to create user.';
|
|---|
| 85 | }
|
|---|
| [bd7f7a7] | 86 |
|
|---|
| [9cae1de] | 87 | try {
|
|---|
| 88 | await signIn('credentials', {
|
|---|
| 89 | email,
|
|---|
| 90 | password,
|
|---|
| 91 | redirectTo: safeRedirect,
|
|---|
| 92 | });
|
|---|
| 93 | } catch (error) {
|
|---|
| 94 | if (error instanceof AuthError) {
|
|---|
| 95 | return 'Account created, but auto-login failed. Please log in.';
|
|---|
| 96 | }
|
|---|
| 97 | throw error;
|
|---|
| 98 | }
|
|---|
| [f3de0a3] | 99 | }
|
|---|