Overview
Mis Compras implements a dual authentication system using both PHP session-based authentication and client-side localStorage for maintaining user state across the application.Authentication Flow
Registration Flow
Users can register through either the Node.js or PHP backend:User submits registration form
The client sends user credentials (name, email, password) to the backend
Login Flow
Registration Endpoints
PHP Registration
| Parameter | Type | Required | Description |
|---|---|---|---|
nombre | string | Yes | User’s full name |
email | string | Yes | User’s email address |
contrasena | string | Yes | User’s password (will be hashed) |
Missing Fields
Duplicate Email
Database Error
Node.js Registration
| Field | Type | Required | Description |
|---|---|---|---|
nombre | string | Yes | User’s full name |
correo | string | Yes | User’s email address |
contraseña | string | Yes | User’s password |
Note the field name differences: PHP uses
email and contrasena, while Node.js uses correo and contraseña.Login Endpoint
PHP Login (Primary Method)
| Parameter | Type | Required | Description |
|---|---|---|---|
email | string | Yes | User’s registered email |
contrasena | string | Yes | User’s password |
Missing Fields
Invalid Password
User Not Found
Session Management
Server-Side Sessions (PHP)
When a user successfully logs in, PHP creates a server-side session:id_usuario: User’s database IDnombre: User’s display name
Client-Side Storage (localStorage)
The frontend should store user data in localStorage for persistence:| Key | Description | Example Value |
|---|---|---|
userId | User’s database ID | "1" |
userName | User’s display name | "John Doe" |
isLoggedIn | Login status flag | "true" |
Password Security
PHP Password Hashing
The PHP backend uses PHP’s native password hashing:- Algorithm: bcrypt (via PASSWORD_DEFAULT)
- Cost: Default (currently 10)
- Salt: Automatically generated
Node.js Password Hashing
The Node.js backend uses bcrypt:- Algorithm: bcrypt
- Salt Rounds: 10
- Library:
bcryptnpm package
Both backends use bcrypt for password hashing, ensuring consistent security across the platform.
Authentication Best Practices
For Frontend Developers
- Always use HTTPS in production to prevent credential interception
- Clear localStorage on logout to prevent unauthorized access
- Validate session before sensitive operations by checking
isLoggedInflag - Handle session expiration gracefully with appropriate error messages
For Backend Developers
- Never log passwords in console or error messages
- Use prepared statements for all database queries (already implemented)
- Implement rate limiting on login endpoints to prevent brute force attacks
- Consider adding email verification for new registrations
- Add CSRF protection for form submissions
Logout Implementation
While there’s no dedicated logout endpoint, implement logout as follows:Frontend Logout
Backend Session Cleanup (PHP)
If you need to implement a PHP logout endpoint:Protected Routes
To protect routes that require authentication:Frontend Route Protection
Backend Route Protection (Node.js Example)
Security Considerations
Recommended Improvements
- Implement JWT tokens for stateless authentication
- Restrict CORS to specific frontend domains
- Add session timeouts (e.g., 30 minutes of inactivity)
- Rate limit login attempts (e.g., 5 attempts per 15 minutes)
- Use httpOnly cookies instead of localStorage for sensitive data
- Implement 2FA for enhanced security
Troubleshooting
Common Issues
Issue: “Faltan campos” error even with all fields provided- Verify you’re using
application/x-www-form-urlencodedfor PHP endpoints - Check field names match exactly (case-sensitive)
- Ensure the request method is POST
- Verify cookies are enabled in the browser
- Check that
session_start()is called at the beginning of PHP scripts - Ensure the session cookie domain is correctly set
- Confirm the password field name matches (
contrasenavscontraseña) - Verify the stored hash is valid in the database
- Check that the password wasn’t accidentally hashed twice
Next Steps
Products API
Fetch and display product catalogs
Orders API
Process authenticated checkout
Users API
Manage user profiles
Configuration
Configure authentication settings