React JS UI Page
What is React.js?
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 startInstall Required Packages:
npm install react-router-dom axiosreact-router-dom → Enables navigation between pages.
axios → Used for API requests.
Step 1: App.js (Main Entry Point)
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
src/components/Register.jsExplanation
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
/dashboardif 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:
Explanation:
fastapi→ API frameworkuvicorn→ Server to run FastAPIpymongo→ Connects FastAPI to MongoDBpasslib[bcrypt]→ Hash passwords securelypython-dotenv→ Load environment variablespydantic[email]→ Email validation
2. Connect FastAPI to MongoDB
Create a .env file to store MongoDB credentials:
3. Create FastAPI App
Create a file main.py:
Final Steps
Start backend API:
Start frontend:
Last updated