React.js is a JavaScript library for building user interfaces, primarily for Single Page Applications (SPA). It allows you to create reusable UI components and manage dynamic data.
React uses:
Components - Reusable UI blocks.
State & Props - Manage dynamic data.
Virtual DOM - Efficient updates without reloading the page.
React Router - Navigation without refreshing the browser.
Project Setup:
npx create-react-app react-auth
cd react-auth
npm install
npm start
Install Required Packages:
npm install react-router-dom axios
react-router-dom → Enables navigation between pages.
axios → Used for API requests.
Step 1: App.js (Main Entry Point)
src/App.js
Explanation
Imports React and React Router for navigation.
Defines routes (Routes & Route):
/register → Shows Register page.
/login → Shows Login page.
/dashboard → Shows Dashboard page.
Wraps everything inside <Router> to manage navigation.
Step 2: Register Component
src/components/Register.js
Explanation
useState() → Manages form data.
handleChange() → Updates state when user types.
handleRegister() → Sends form data to FastAPI backend.
Displays success/error message.
If registration is successful, shows "Go to Login" link.
Step 3: Login Component
src/components/Login.js
Explanation
useState() → Stores email/password.
useNavigate() → Redirects user after login.
handleLogin() → Sends credentials to the backend.
Redirects user to /dashboard if login is successful.
Step 4: Dashboard Component
src/components/Dashboard.js
Explanation
Simple UI to show success message.
Only accessible after login.
Step 5: Add CSS Styling
src/styles/auth.css
Step 1 : Setting Up the Backend (FastAPI + MongoDB)
1. Install Dependencies
First, create a virtual environment and install dependencies: