User Registration (api/register.ts)¶
Endpoint Overview¶
Handles new user registration by creating a user account with hashed password. Validates email uniqueness and creates a new user record in the database.
Request Handlers¶
POST¶
- Body:
- Response:
- Status: 200 OK, 405 Method Not Allowed, 422 Unprocessable Entity, 400 Bad Request
Error Handling¶
try {
const existingUser = await prismadb.users.findUnique({
where: { UserEmail: email }
});
if (existingUser) {
return res.status(422).json({ error: 'Email taken' });
}
// ... user creation
} catch (error) {
return res.status(400).json({ error: `Something went wrong: ${error}` });
}
Common errors: - 405: Non-POST request method - 422: Email already registered - 400: Invalid input data - 400: Database error
Usage Example¶
const response = await fetch("/api/register", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
firstName: "John",
lastName: "Doe",
email: "john@example.com",
password: "securePassword123"
})
});
const newUser = await response.json();
Implementation Details¶
- Uses bcrypt for password hashing (12 rounds)
- Validates email uniqueness
- Creates user record in database
- Combines first and last name for UserName
- Performs input validation
- Uses Prisma for database operations
Pages/Components Referenced By¶
- Sign In Component - Registration link
- Register Page - Main registration form
- Email API - Welcome email sending
- Current API - Post-registration session
Notes¶
- Rate limited to prevent abuse
- Password hashed before storage
- Email must be unique
- Name fields sanitized
- No password complexity requirements in API (handled by frontend)
- Returns created user object
- Used in registration flow