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