Skip to content

Latest commit

 

History

History
452 lines (336 loc) · 15.6 KB

AUTHENTICATION.md

File metadata and controls

452 lines (336 loc) · 15.6 KB

Authentication PR Summary

This pull request adds a complete browser-side authentication experience for ChaatBazaar. It keeps the existing orange theme and responsive layout, uses localStorage for users, login state, and theme preference, and updates the navbar so it behaves correctly on login, register/signup, and homepage pages.

Main results:

  • Login and registration now work with validation and redirects.
  • Logged-in users see a profile icon and login/signup buttons.
  • Theme preference persists across refreshes.
  • Profile and Dashboard pages are protected and only open for authenticated users.

Testing Checklist

Use this checklist to verify the authentication features manually:

  • Open the login page and register/signup page confirm the navbar shows only Home, Menu, About, Cart, and Theme Toggle.
  • Submit the login form with invalid email or short password and confirm validation messages appear.
  • Log in with a valid saved user and confirm loggedInUser is stored in localStorage.
  • After login, confirm the homepage shows the profile icon instead of Login/Sign Up buttons.
  • Open the profile dropdown and confirm it contains Profile, Dashboard, and Logout.
  • Click outside the dropdown and confirm it closes.
  • Press Escape while the dropdown is open and confirm it closes.
  • Use Logout and confirm loggedInUser is removed and the navbar returns to the logged-out state.
  • Register a new user and confirm the user is saved in localStorage.users and auto-logged in.
  • Refresh the page and confirm the theme choice is remembered.
  • Open Profile and Dashboard while logged out and confirm the app redirects to Login.
  • Open Profile and Dashboard while logged in and confirm the user data loads correctly.

1. Overview of the Authentication System

The authentication system is fully client-side. It does not use a server or database. Instead, it stores user data in the browser using localStorage.

The system now supports:

  • Registration with validation.
  • Login with email and password checking.
  • Automatic login after registration.
  • Logout.
  • Session-aware navbar rendering.
  • A profile dropdown menu for logged-in users.
  • Theme toggle persistence.
  • Protected pages for profile and dashboard views.

The goal of this update was to make the auth experience easy to use, consistent across pages, and safe for a frontend-only project.

2. New Files Created

js/auth.js

Why it was created: This file centralizes all authentication, theme, session, dropdown, and protected-page logic.

What code was added:

  • Login and registration handling.
  • localStorage read/write helpers.
  • Session storage helpers.
  • Theme toggle setup and persistence.
  • Navbar rendering for logged-in and logged-out states.
  • Profile dropdown behavior.
  • Page protection and redirect logic.
  • Profile and dashboard population logic.

What problem it solves:

  • It avoids repeating auth logic in multiple pages.
  • It keeps login, signup, theme, and navbar behavior synchronized.

profile.html

Why it was created: This page gives logged-in users a place to view account details.

What code was added:

  • A protected page shell.
  • A centered profile card.
  • Placeholders for user name, email, phone, and location.
  • Shared navbar and theme toggle.

What problem it solves:

  • It gives the profile dropdown a real destination.
  • It shows the current user’s session data in a simple UI.

dashboard.html

Why it was created: This page acts as a protected dashboard destination from the profile dropdown.

What code was added:

  • A protected page shell.
  • A dashboard card with a friendly placeholder layout.
  • A greeting that uses the logged-in user’s name.
  • Shared navbar and theme toggle.

What problem it solves:

  • It gives the dashboard link a real page.
  • It proves route protection is working.

login.html

Why it was created: The navbar needed to be better for the login page.

What code was added:

  • Shared navbar order with the theme toggle kept in place.
  • data-page-type="login" so the shared auth script can detect the page.

What problem it solves:

  • The login page now keeps focus on the login form.
  • It prevents duplicate or confusing auth links in the header.

signup.html

Why it was created: This file serves as the register page and needed the same navbar cleanup.

What code was added:

  • Shared navbar order with only the required site links.
  • data-page-type="signup" so the shared auth script can detect the page.

What problem it solves:

  • The register page now matches the simplified navbar requirement.
  • It keeps the form as the main path for account creation.

index.html

Why it was modified: The homepage needed to show the correct logged-in or logged-out state.

What code was added:

  • The shared auth mount point #authNavItem.
  • The theme toggle positioned before the auth slot.
  • Support for dynamic profile icon rendering after login.

What problem it solves:

  • The homepage now changes based on whether a user is logged in.
  • It hides Login/Sign Up buttons after authentication.

menu.html

Why it was modified: It uses the same shared header as the homepage.

What code was added:

  • The reordered header layout.
  • The shared auth slot and theme toggle.

What problem it solves:

  • The navbar stays consistent across the site.
  • Theme and auth behavior work the same way on the menu page.

orders.html

Why it was modified: It uses the same shared header pattern and needed the updated auth placement.

What code was added:

  • The reordered navbar structure.
  • The shared auth slot.

What problem it solves:

  • The page matches the new navbar pattern.
  • Logged-in users see the same account controls everywhere.

cart.html

Why it was modified: It shares the global header and needed the same auth and theme layout.

What code was added:

  • The theme toggle before the auth slot.
  • The shared auth mount point.

What problem it solves:

  • Cart users get the same auth experience as on the homepage.
  • The header remains consistent and responsive.

favorites.html

Why it was modified: It uses the shared navigation and needed the same auth placement updates.

What code was added:

  • The reordered navbar structure.
  • The shared auth slot and theme toggle.

What problem it solves:

  • The page stays aligned with the rest of the site.
  • Logged-in users can access profile actions from here too.

faq.html

Why it was modified: It also uses the shared header and needed the same auth layout.

What code was added:

  • The reordered navbar structure.
  • The shared auth slot and theme toggle.

What problem it solves:

  • The FAQ page behaves the same as the other shared-navbar pages.
  • Theme and auth state stay consistent.

css/style.css

Why it was modified: The global stylesheet needed new styles for the auth profile menu and state-aware navbar.

What code was added:

  • Styles for the auth nav slot.
  • Styles for the login button in the navbar.
  • Styles for the circular profile button.
  • Styles for the dropdown menu and its items.
  • Dark mode overrides.

What problem it solves:

  • It makes the profile dropdown look correct and responsive.
  • It keeps the orange theme working in both light and dark mode.

css/auth.css

Why it was created: The auth stylesheet needed support for the profile and dashboard pages.

What code was added:

  • Card styles for profile and dashboard pages.
  • Styles for profile avatar, profile details, and action links.
  • Layout tweaks to keep the pages centered and mobile-friendly.

What problem it solves:

  • It gives the new protected pages a polished, consistent look.
  • It keeps the auth pages readable and responsive.

js/main.js

Why it was modified: Theme handling was moved out so one shared script controls it.

What code was added:

  • Removal of the old theme-toggle behavior from the main site script.

What problem it solves:

  • It prevents duplicate theme logic.
  • It avoids conflicts between the homepage script and the auth script.

4. Detailed Explanation of Major Code Changes

Shared auth controller

The biggest change is the new shared auth controller in js/auth.js. It acts like the brain for all authentication-related behavior.

It now handles:

  • Reading and writing user data.
  • Logging users in and out.
  • Saving the current session.
  • Showing the right navbar controls.
  • Updating the theme.
  • Protecting profile and dashboard pages.
  • Filling in profile and dashboard content after login.

This keeps the implementation easier to understand and maintain.

Navbar rendering

The navbar now changes based on the session state.

  • If the user is logged out, the navbar shows a Login button.
  • If the user is logged in, the Login button is replaced by a circular profile icon.
  • Clicking the profile icon opens a dropdown with Profile, Dashboard, and Logout.

This is done with JavaScript so the same behavior works on every shared-navbar page.

Theme toggle persistence

Theme state is now saved in localStorage.

  • Light theme is the default.
  • When the user switches to dark mode, the choice is saved.
  • On refresh or page change, the saved theme is restored.

This makes the theme toggle feel consistent and user-friendly.

Protected pages

The Profile and Dashboard pages are protected.

  • If a logged-out user opens them, they are redirected to Login.
  • If a logged-in user opens them, the pages load normally.

This keeps account pages from being opened without a session.

Sanitized profile output

Stored user values are escaped before being rendered in the profile dropdown.

This prevents unsafe HTML from being inserted into the page by mistake.

5. How localStorage Is Used

This project uses browser storage instead of a backend database.

users

Stores all registered users as an array of objects. Each user object contains:

  • name
  • email
  • password
  • phone
  • location
  • createdAt

Used for:

  • Checking duplicate emails.
  • Validating login credentials.
  • Keeping registration data after page reload.

loggedInUser

Stores the currently logged-in user as an object. It usually contains:

  • name
  • email
  • phone
  • location
  • loginAt

Used for:

  • Showing the profile icon.
  • Filling the profile page.
  • Filling the dashboard greeting.
  • Checking whether protected pages should redirect.

theme

Stores the current visual theme. Possible values:

  • light
  • dark

Used for:

  • Restoring the selected theme after refresh.
  • Keeping theme behavior the same across pages.

6. Login Flow

The login flow works like this:

  1. The user opens the login page.
  2. The form checks that the email format is valid.
  3. The password must be at least 6 characters long.
  4. The script looks for a matching user in localStorage.users.
  5. If the user is found, loggedInUser is saved in localStorage.
  6. The user is redirected to index.html.
  7. The homepage then shows the profile icon instead of Login/Sign Up.

If validation fails, the form shows a helpful message and keeps the user on the page.

7. Registration Flow

The registration flow works like this:

  1. The user fills in name, email, password, phone, and location.
  2. The script checks that all fields are valid.
  3. The email must have a valid format.
  4. The password must be at least 6 characters.
  5. The phone number must contain exactly 10 digits.
  6. The email must not already exist in localStorage.users.
  7. If everything is valid, the new user is added to users.
  8. The new user is also auto-logged in by saving loggedInUser.
  9. The user is redirected to index.html.

This makes sign up feel fast because the user does not need to log in again after registration.

8. Logout Flow

The logout flow is simple:

  1. The user opens the profile dropdown.
  2. The user clicks Logout.
  3. loggedInUser is removed from localStorage.
  4. The user is redirected to index.html.
  5. The homepage returns to the logged-out navbar state.

This keeps logout predictable and easy to test.

9. Profile Icon and Dropdown Functionality

When a user is logged in, the navbar shows a circular profile icon instead of auth buttons.

The dropdown:

  • Opens below the icon.
  • Closes when the user clicks outside it.
  • Closes when the user presses Escape.
  • Contains the current user name and email.
  • Includes links for Profile and Dashboard.
  • Includes a Logout button.

This gives logged-in users quick access to account pages without adding extra clutter to the header.

10. Theme Toggle Functionality

The theme toggle still works on all shared pages.

What it does:

  • Switches between light and dark modes.
  • Saves the selected theme in localStorage.
  • Restores the same theme after refresh.
  • Updates the toggle accessibility label and state.

This keeps the UI consistent and makes the user’s choice persistent.

11. Route/Page Protection for Profile and Dashboard

The Profile and Dashboard pages are protected by session checks.

Behavior:

  • Logged-out users are sent to Login.
  • Logged-in users can view the pages normally.
  • The page content is filled from loggedInUser.

This makes the account pages behave like private pages even though the project is frontend-only.

12. Validation Rules Implemented

Login validation

  • Email must be valid.
  • Password must be at least 6 characters.
  • The email and password must match a saved user.

Registration validation

  • Name is required.
  • Email must be valid.
  • Password must be at least 6 characters.
  • Phone must be exactly 10 digits.
  • Location is required.
  • Email must not already be registered.

These checks prevent bad or duplicate data from being saved.

13. UI/UX Improvements Made

  • Simplified the login and register page navbars.
  • Kept the main actions focused on the form.
  • Added a profile dropdown for logged-in users.
  • Kept the orange theme and responsive layout intact.
  • Added clear validation feedback messages.
  • Kept the theme toggle available across pages.
  • Added centered profile and dashboard cards for a cleaner account view.

These changes make the auth flow easier to use and easier to understand.

14. Bugs Fixed During Implementation

  • Removed the extra Login and Sign Up links from the login and register page navbars.
  • Prevented the homepage from showing logged-out auth buttons after login.
  • Moved theme control into one shared script to avoid conflicts.
  • Added safe HTML escaping for profile dropdown values.
  • Added redirect protection for Profile and Dashboard pages.

These fixes made the navigation and account state behave consistently.

15. Future Improvements That Could Be Added

  • Add a real backend so users are not stored only in the browser.
  • Hash passwords before saving them.
  • Add password reset or forgot-password flow.
  • Add a richer dashboard with order history and saved addresses.
  • Add avatar upload for the profile page.
  • Add server-side validation for stronger security.
  • Add remember-me behavior with a longer session model.

These are optional next steps if the project grows beyond a frontend demo.

16. File-by-File Summary

  • js/auth.js: Central auth, theme, dropdown, and page-protection logic.
  • js/main.js: Existing site behavior; theme handling removed to avoid duplication.
  • login.html: Login form page with a simplified navbar.
  • signup.html: Register page with a simplified navbar.
  • index.html: Homepage that changes based on session state.
  • menu.html: Shared-navbar page updated to match the new layout.
  • orders.html: Shared-navbar page updated to match the new layout.
  • cart.html: Shared-navbar page updated to match the new layout.
  • favorites.html: Shared-navbar page updated to match the new layout.
  • faq.html: Shared-navbar page updated to match the new layout.
  • css/style.css: Global navbar, profile dropdown, and dark-mode styles.
  • css/auth.css: Auth card, profile card, and dashboard styles.
  • profile.html: Protected profile page.
  • dashboard.html: Protected dashboard page.