# Function 5: Register Form - Completed โ
## ๐ฆ Files Created/Updated
### 1. **RegisterPage.tsx** - Register form component
**Path:** `client/src/pages/auth/RegisterPage.tsx`
### 2. **index.ts** - Export module
**Path:** `client/src/pages/auth/index.ts`
- Added export RegisterPage
### 3. **App.tsx** - Routing updated
**Path:** `client/src/App.tsx`
- Added route `/register`
## โจ Main Features
### 1. Form Fields (5 fields)
โ
**Full Name** (name)
- Required, 2-50 characters
- Icon: User
- Placeholder: "John Doe"
โ
**Email**
- Required, valid email format
- Icon: Mail
- Placeholder: "email@example.com"
โ
**Phone Number** (phone) - Optional
- 10-11 digits
- Icon: Phone
- Placeholder: "0123456789"
โ
**Password** (password)
- Required, min 8 chars
- Must contain: uppercase, lowercase, number, special char
- Show/hide toggle with Eye icon
- Icon: Lock
โ
**Confirm Password** (confirmPassword)
- Must match password
- Show/hide toggle with Eye icon
- Icon: Lock
### 2. Password Strength Indicator
โ
**Visual Progress Bar** with 5 levels:
1. ๐ด Very weak (0/5)
2. ๐ Weak (1/5)
3. ๐ก Medium (2/5)
4. ๐ต Strong (3/5)
5. ๐ข Very strong (5/5)
โ
**Real-time Requirements Checker:**
- โ
/โ At least 8 characters
- โ
/โ Lowercase (a-z)
- โ
/โ Uppercase (A-Z)
- โ
/โ Number (0-9)
- โ
/โ Special character (@$!%*?&)
### 3. Validation Rules (Yup Schema)
```typescript
name:
- Required: "Full name is required"
- Min 2 chars: "Full name must be at least 2 characters"
- Max 50 chars: "Full name must not exceed 50 characters"
- Trim whitespace
email:
- Required: "Email is required"
- Valid format: "Invalid email format"
- Trim whitespace
phone (optional):
- Pattern /^[0-9]{10,11}$/
- Error: "Invalid phone number"
password:
- Required: "Password is required"
- Min 8 chars
- Pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])/
- Error: "Password must contain uppercase, lowercase, number and special characters"
confirmPassword:
- Required: "Please confirm password"
- Must match password: "Passwords do not match"
```
### 4. UX Features
โ
**Loading State**
```tsx
{isLoading ? (
<>
Processing...
>
) : (
<>
Register
>
)}
```
โ
**Show/Hide Password** (2 toggles)
- Eye/EyeOff icons
- Separate toggle for password and confirmPassword
- Visual feedback on hover
โ
**Error Display**
- Inline validation errors under each field
- Global error message at top of form
- Red border for fields with errors
โ
**Success Flow**
```typescript
1. Submit form
2. Validation passes
3. Call useAuthStore.register()
4. Show toast: "Registration successful! Please login."
5. Navigate to /login
```
### 5. Design & Styling
**Color Scheme:**
- Primary: purple-600, purple-700
- Background: gradient from-purple-50 to-pink-100
- Success: green-500, green-600
- Error: red-50, red-200, red-600
- Text: gray-600, gray-700, gray-900
**Layout:**
```
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐จ Hotel Icon (Purple) โ
โ Register Account โ
โ Create a new account to book... โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ [Error message if any] โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ
โ โ Full Name โ โ
โ โ [๐ค John Doe ] โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ
โ โ Email โ โ
โ โ [๐ง email@example.com ] โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ
โ โ Phone Number (Optional) โ โ
โ โ [๐ฑ 0123456789 ] โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ
โ โ Password โ โ
โ โ [๐ โขโขโขโขโขโขโขโข ๐๏ธ] โ โ
โ โ โโโโโโโโโโ Very strong โ โ
โ โ โ
At least 8 characters โ โ
โ โ โ
Lowercase (a-z) โ โ
โ โ โ
Uppercase (A-Z) โ โ
โ โ โ
Number (0-9) โ โ
โ โ โ
Special character โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ
โ โ Confirm Password โ โ
โ โ [๐ โขโขโขโขโขโขโขโข ๐๏ธ] โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ
โ โ [๐ค Register] โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ Already have an account? Login now โ
โ โ
โ Terms & Privacy Policy โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```
## ๐ Integration
### API Endpoint
```
POST /api/auth/register
```
### Request Body
```typescript
{
name: string; // "John Doe"
email: string; // "user@example.com"
password: string; // "Password123@"
phone?: string; // "0123456789" (optional)
}
```
### Response (Success)
```json
{
"status": "success",
"message": "User registered successfully",
"data": {
"user": {
"id": 1,
"name": "John Doe",
"email": "user@example.com",
"phone": "0123456789",
"role": "customer"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}
```
### useAuthStore Integration
```typescript
const { register: registerUser, isLoading, error, clearError } =
useAuthStore();
await registerUser({
name: data.name,
email: data.email,
password: data.password,
phone: data.phone,
});
// After success:
navigate('/login', { replace: true });
```
## ๐งช Test Scenarios
### 1. Validation Tests
**Test Case 1: Empty form**
```
Action: Submit empty form
Expected: Show validation errors for name, email, password
```
**Test Case 2: Invalid email**
```
Input: email = "notanemail"
Expected: "Invalid email format"
```
**Test Case 3: Short name**
```
Input: name = "A"
Expected: "Full name must be at least 2 characters"
```
**Test Case 4: Weak password**
```
Input: password = "abc123"
Expected: "Password must contain uppercase, lowercase, number and special characters"
Password strength: Weak/Medium
```
**Test Case 5: Password mismatch**
```
Input:
password = "Password123@"
confirmPassword = "Password456@"
Expected: "Passwords do not match"
```
**Test Case 6: Invalid phone**
```
Input: phone = "123"
Expected: "Invalid phone number"
```
### 2. UX Tests
**Test Case 7: Password strength indicator**
```
Input: Type password character by character
Expected:
- Progress bar animates
- Color changes: red โ orange โ yellow โ blue โ green
- Checkmarks appear as requirements met
```
**Test Case 8: Show/hide password**
```
Action: Click eye icon on password field
Expected: Password text becomes visible/hidden
Action: Click eye icon on confirmPassword field
Expected: Confirm password text becomes visible/hidden
```
**Test Case 9: Loading state**
```
Action: Submit valid form
Expected:
- Button disabled
- Spinner shows
- Text changes to "Processing..."
```
### 3. Integration Tests
**Test Case 10: Successful registration**
```
Input: All valid data
Expected:
1. API POST /api/auth/register called
2. Toast: "Registration successful! Please login."
3. Redirect to /login
```
**Test Case 11: Email already exists**
```
Input: email = "existing@example.com"
Expected:
- Error message: "Email already registered"
- Toast error displayed
- Form remains on page
```
**Test Case 12: Network error**
```
Scenario: Server offline
Expected:
- Error message: "Registration failed. Please try again."
- Toast error displayed
```
## ๐ Password Strength Algorithm
```typescript
function getPasswordStrength(pwd: string) {
let strength = 0;
if (pwd.length >= 8) strength++; // +1
if (/[a-z]/.test(pwd)) strength++; // +1
if (/[A-Z]/.test(pwd)) strength++; // +1
if (/\d/.test(pwd)) strength++; // +1
if (/[@$!%*?&]/.test(pwd)) strength++; // +1
return {
strength: 0-5,
label: ['Very weak', 'Weak', 'Medium', 'Strong', 'Very strong'][strength],
color: ['bg-red-500', 'bg-orange-500', 'bg-yellow-500', 'bg-blue-500', 'bg-green-500'][strength]
};
}
```
## ๐จ Component Structure
```tsx
RegisterPage/
โโโ Header Section
โ โโโ Hotel Icon (purple)
โ โโโ Title: "Register Account"
โ โโโ Subtitle
โ
โโโ Form Container (white card)
โ โโโ Error Alert (conditional)
โ โโโ Name Input
โ โโโ Email Input
โ โโโ Phone Input (optional)
โ โโโ Password Input
โ โ โโโ Show/Hide Toggle
โ โ โโโ Strength Indicator
โ โ โโโ Requirements Checklist
โ โโโ Confirm Password Input
โ โ โโโ Show/Hide Toggle
โ โโโ Submit Button (with loading)
โ
โโโ Login Link
โ โโโ "Already have an account? Login now"
โ
โโโ Footer Links
โโโ Terms of Service
โโโ Privacy Policy
```
## ๐ Security Features
### 1. Password Validation
- Min 8 characters
- Requires: uppercase, lowercase, number, special char
- Visual feedback for strength
### 2. Confirm Password
- Must match original password
- Prevents typos
### 3. Client-side Validation
- Immediate feedback
- Prevents invalid API calls
- Better UX
### 4. Server-side Validation
- Backend also validates all fields
- Checks email uniqueness
- Password hashed with bcrypt
## ๐ Code Quality
โ
**TypeScript**: Full type safety
โ
**React Hook Form**: Optimized re-renders
โ
**Yup Validation**: Schema-based validation
โ
**Component Composition**: Reusable PasswordRequirement component
โ
**Accessibility**: Proper labels, IDs, autocomplete
โ
**Error Handling**: Try-catch, user-friendly messages
โ
**Loading States**: Visual feedback during async operations
โ
**Responsive Design**: Works on mobile and desktop
โ
**80 chars/line**: Code formatting standard
## ๐ Usage
### Navigate to Register Page
```bash
http://localhost:5173/register
```
### Example Registration
```typescript
Name: "John Doe"
Email: "nguyenvana@example.com"
Phone: "0123456789"
Password: "Password123@"
Confirm: "Password123@"
Submit โ Success โ Redirect to /login
```
## ๐ Flow Diagram
```
User visits /register
โ
Fill in form fields
โ
Real-time validation (Yup)
โ
Password strength updates live
โ
Submit button clicked
โ
Frontend validation passes
โ
Call useAuthStore.register()
โ
API POST /api/auth/register
โ
โโโโโโโโโดโโโโโโโโ
โ โ
Success Failure
โ โ
Toast success Toast error
โ โ
Navigate Stay on page
to /login Show errors
```
## โ
Checklist
- [x] โ
Create RegisterPage.tsx component
- [x] โ
Implement React Hook Form
- [x] โ
Add Yup validation schema
- [x] โ
Add 5 form fields (name, email, phone, password, confirmPassword)
- [x] โ
Show/hide password toggle (2 fields)
- [x] โ
Password strength indicator
- [x] โ
Real-time requirements checker
- [x] โ
Loading state
- [x] โ
Error display (inline + global)
- [x] โ
Integration with useAuthStore
- [x] โ
Redirect to /login after success
- [x] โ
Toast notifications
- [x] โ
Add route to App.tsx
- [x] โ
Responsive design
- [x] โ
Purple color scheme
- [x] โ
Icons integration (Lucide React)
- [x] โ
Terms & Privacy links
## ๐ Related Files
- `client/src/pages/auth/LoginPage.tsx` - Login form (same design pattern)
- `client/src/utils/validationSchemas.ts` - Validation schemas
- `client/src/store/useAuthStore.ts` - Auth state management
- `client/src/services/api/authService.ts` - API calls
- `client/src/App.tsx` - Route configuration
---
**Status:** โ
Function 5 completed
**Next:** Function 6 - Forgot Password
**Test URL:** http://localhost:5173/register