RegisterPage Component
TheRegisterPage component provides the user interface for creating new user accounts in the Karma Ecommerce application. It handles user registration by calling the AuthService and includes comprehensive error handling.
Location
Component Code
- TypeScript
- HTML Template
@Component Decorator
The component is configured with the following metadata:| Property | Value | Description |
|---|---|---|
selector | 'app-register-page' | HTML selector for the component |
imports | [] | Standalone component with no additional imports |
templateUrl | './register-page.html' | Path to the HTML template |
styleUrl | './register-page.css' | Path to the component styles |
Properties
authService
Methods
registrar()
usuario(string) - Username for the new accountcontrasena(string) - Password for the new account
registrarUsuario()- Calls the service method that returns an Observablepipe()- Chains RxJS operators for data transformation and error handlingtap()- Side effect operator that:- Logs the user object with label “usuario” to console
- Displays success alert if registration succeeds
- Displays the user object as alert if registration fails
catchError()- Error handling operator that:- Extracts error message from error object
- Displays formatted error alert
- Re-throws the error using
throwError()
subscribe()- Activates the observable stream
- On success: Displays “usuario registrado correctamente”
- On failure: Displays error message in format “Error al registrar: ”
- Logs: User object to console on successful registration
- Checks if
err.errorexists, otherwise defaults to “Error desconocido” - Displays user-friendly error message
- Re-throws error for potential upstream handling
Dependency Injection
The component uses Angular’s dependency injection to inject theAuthService:
Routing Configuration
The RegisterPage is configured as the/registro route in app.routes.ts:
- Path:
/registro - Component: RegisterPage
- Access: Navigate to
/registroto access registration page
RxJS Operators Used
The component demonstrates several RxJS operators:tap()
Used for side effects without modifying the stream:- Logging to console
- Displaying alerts
- Does not transform the observable data
catchError()
Handles errors in the observable stream:- Intercepts errors from the registration service
- Displays user-friendly error messages
- Can transform or re-throw errors
throwError()
Creates an error observable:- Re-throws the caught error
- Allows error propagation to subscribers
- Useful for maintaining error flow
Related Components
- LoginPage - User login page
- AuthService - Authentication and registration service
- UsuarioRepository - User repository interface
Usage Notes
- The component currently uses hardcoded credentials in the template for testing
- Registration results are displayed using browser alerts
- Unlike LoginPage, this component does NOT implement
OnInit - Comprehensive error handling is implemented using RxJS
catchError - The component follows Angular standalone component architecture
- Error messages are extracted from the
err.errorproperty or default to “Error desconocido”
