Spaces:
Sleeping
Sleeping
Download src/app/actions/auth.ts from veltrixcode/Iot: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/spaces/veltrixcode/Iot/resolve/main/src/app/actions/auth.ts
- Command line
-
hf download hf://spaces/veltrixcode/Iot/src/app/actions/auth.ts
-
curl -L -o auth.ts https://huggingface.co/spaces/veltrixcode/Iot/resolve/main/src/app/actions/auth.ts
1.42 kB
| "use server" | |
| import { prisma } from "@/lib/prisma" | |
| import bcrypt from "bcryptjs" | |
| import { z } from "zod" | |
| const signupSchema = z.object({ | |
| name: z.string().min(2, "Name must be at least 2 characters"), | |
| username: z.string().min(3, "Username must be at least 3 characters"), | |
| email: z.string().email("Invalid email address"), | |
| password: z.string().min(6, "Password must be at least 6 characters"), | |
| }) | |
| export async function signup(data: z.infer<typeof signupSchema>) { | |
| const result = signupSchema.safeParse(data) | |
| if (!result.success) { | |
| return { error: "Invalid input" } | |
| } | |
| const { name, username, email, password } = result.data | |
| try { | |
| const existingUser = await prisma.user.findFirst({ | |
| where: { | |
| OR: [{ email }, { username }], | |
| }, | |
| }) | |
| if (existingUser) { | |
| return { error: "User with this email or username already exists" } | |
| } | |
| const hashedPassword = await bcrypt.hash(password, 10) | |
| await prisma.user.create({ | |
| data: { | |
| name, | |
| username, | |
| email, | |
| password: hashedPassword, | |
| credits: 50, | |
| }, | |
| }) | |
| return { success: true } | |
| } catch (error) { | |
| console.error("Signup error:", error) | |
| return { error: "Something went wrong. Please try again." } | |
| } | |
| } | |