Implement a login feature that authenticates users using either username or email and password. If the entered credentials match an existing user in the database, log the user in and redirect them to the home page. Additionally, enhance the signup functionality to display a message if the entered username already exists, prompting the user to choose a different one.
Acceptance Criteria:
Backend API for Login:
Create a new API route to handle login requests.
Accept username or email and password as inputs.
Query the database to find a user with the given username or email.
Validate the entered password against the stored hashed password using bcrypt.
If the credentials are correct, generate a JWT (JSON Web Token) for authentication and return it along with the user details.
If credentials are incorrect, return a descriptive error message ("Invalid username or password").
Frontend Login Page Enhancements:
Modify the existing Login page to accept either a username or email and a password.
On form submission:
Send a POST request to the login API with the entered data.
If the login is successful, save the JWT to local storage or cookies and redirect the user to their profile or home page.
If the login fails, display an appropriate error message.
Signup Page Error Handling:
During signup, check if the entered username or email already exists in the database.
If a match is found, display a clear error message:
For Username: "Username already exists, please choose another."
For Email: "Email is already registered, please use another."
Prevent the signup process if there are any errors.
The signup form should remain filled with the previously entered data except for the fields causing the error.
Testing and Validation:
Successful login using both username and email.
Incorrect login attempts with descriptive error messages.
Signup form validation for existing usernames and emails.
Perform frontend tests to verify correct error display and form handling.
Implement a login feature that authenticates users using either username or email and password. If the entered credentials match an existing user in the database, log the user in and redirect them to the home page. Additionally, enhance the signup functionality to display a message if the entered username already exists, prompting the user to choose a different one.
Acceptance Criteria: Backend API for Login:
Frontend Login Page Enhancements:
Signup Page Error Handling:
Testing and Validation: