Content ITV PRO
This is Itvedant Content department
Business Scenario
In this lab, we will integrate the Medicare React frontend with the existing Spring Boot authentication system by implementing user registration, login, and JWT-based authentication.
Pre-Lab Preparation
Module:
1) React Basics & Backend Integration (Spring Boot)
Register → Name + Email + Phone + Password Role → PATIENT automatically Login → Email + Password ↓ JWT
Phase 12B — Authentication, JWT & Protected Routes
↓ Patient / Doctor Dashboard
POST /api/auth/register POST /api/auth/login
Configure CORS in Spring Boot
1
C:\MEDICAREPROJECT\medicare-backend
http://localhost:8080
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;npm i react-router
and createBrowserRouter.
First Run
4
npm run dev
Local: http://localhost:5173/
React + Viteyour frontend setup is working.
Delete Default Vite Files
5
assets/ App.css App.jsx index.css main.jsx
assets/ App.css App.jsx
Create .env
6
VITE_API_BASE_URL=http://localhost:8080/api
medicare-frontend/ ├── src/ ├── public/ ├── .env ├── package.json └── vite.config.js
"http://localhost:8080/api"
Development Backend http://localhost:8080/api
Create Central Axios Configuration
7
src/api/api.js
import axios from "axios";
const api = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL,
headers: {
"Content-Type": "application/json"
}
});
export default api;axios.get( "http://localhost:8080/api/doctors" );
Why We Have api.js
doctorService ↓ api ↓ Spring Boot
import api from "../api/api";
api.get("/doctors");api.post("/auth/login", data);Configure Bootstrap
8
import React from "react";
import ReactDOM from "react-dom/client";
import { RouterProvider } from "react-router/dom";
import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap/dist/js/bootstrap.bundle.min.js";
import "./index.css";
import router from "./routes/router";
ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
);Minimal CSS
9
body {
margin: 0;
min-height: 100vh;
background-color: #f8f9fa;
}
#root {
min-height: 100vh;
}Create Home Page
10
import { Link } from "react-router";
function Home() {
return (
<div className="container py-5">
<div className="text-center py-5">
<h1 className="display-5 fw-bold"> Medicare Healthcare </h1>
<p className="lead text-secondary mt-3">
Book doctor appointments and manage your healthcare easily.
</p>
<div className="d-flex justify-content-center gap-3 mt-4">
<Link to="/login" className="btn btn-primary" > Login </Link>
<Link to="/register" className="btn btn-outline-primary">
Register </Link>
</div>
</div>
</div>
);
}
export default Home;Create Login Placeholder
11
function Login() {
return (
<div className="container py-5">
<div className="row justify-content-center">
<div className="col-md-6 col-lg-5">
<div className="card shadow-sm">
<div className="card-body p-4">
<h2 className="text-center mb-4">Login</h2>
<p className="text-center text-secondary">
Login form will be implemented
in the authentication phase.
</p>
</div>
</div>
</div>
</div>
</div>
);}
export default Login;Register Placeholder
12
function Register() {
return (
<div className="container py-5">
<div className="row justify-content-center">
<div className="col-md-7 col-lg-6">
<div className="card shadow-sm">
<div className="card-body p-4">
<h2 className="text-center mb-4">Create Account</h2>
<p className="text-center text-secondary">
Patient registration form will be implemented next. </p>
</div>
</div>
</div>
</div>
</div>
);
}
export default Register;Patient Dashboard Placeholder
13
function PatientDashboard() {
return (
<div className="container py-5">
<h2> Patient Dashboard </h2> <p className="text-secondary">
Welcome to your Medicare dashboard.
</p>
</div>
);
}
export default PatientDashboard;Patient Dashboard ├── Find Doctor ├── My Appointments ├── My Prescriptions └── My Profile
which follows the patient modules in your original specification.
Doctor Dashboard Placeholder
14
function DoctorDashboard() {
return (
<div className="container py-5">
<h2> Doctor Dashboard </h2>
<p className="text-secondary">
View appointments and manage
patient consultations.
</p>
</div>
);
}
export default DoctorDashboard;Unauthorized Page
15
import { Link } from "react-router";
function Unauthorized() {
return (
<div className="container py-5 text-center">
<h1 className="display-5">
403
</h1>
<h3>
Access Denied
</h3>
<p className="text-secondary">
You do not have permission to access this page.
</p> <Link to="/" className="btn btn-primary" >
Go Home
</Link>
</div>
);
}
export default Unauthorized;Not Found Page
16
import { Link } from "react-router";
function NotFound() {
return (
<div className="container py-5 text-center"> <h1 className="display-5">
404
</h1>
<h3>
Page Not Found
</h3>
<p className="text-secondary">
The page you are looking for
does not exist.
</p>
<Link to="/" className="btn btn-primary">
Go Home
</Link>
</div>
);
}
export default NotFound;Create Router
17
import { createBrowserRouter } from "react-router";
import Home from "../pages/common/Home";
import Login from "../pages/auth/Login";
import Register from "../pages/auth/Register";
import PatientDashboard from "../pages/patient/PatientDashboard";
import DoctorDashboard from "../pages/doctor/DoctorDashboard";
import Unauthorized from "../pages/common/Unauthorized";
import NotFound from "../pages/common/NotFound";
const router = createBrowserRouter([
{
path: "/",
element: <Home />
},
{
path: "/login",
element: <Login />
},
{
path: "/register",
element: <Register />
}, {
path: "/patient/dashboard",
element: <PatientDashboard />
},
{
path: "/doctor/dashboard",
element: <DoctorDashboard />
},
{
path: "/unauthorized",
element: <Unauthorized />
},
{
path: "*",
element: <NotFound />
}
]);
export default router;Start Application
18
npm run dev
Test Every Route
19
Medicare Healthcare
Login
Create Account
Doctor Dashboard
Patient Dashboard
404 Page Not Found
We are done with this lab. The latest source code has been uploaded to GitHub. You can access the latest commit using the link below:
Great job!
You have successfully set up the Medicare React frontend using Vite, Bootstrap, Axios, and React Router.
Checkpoint
Git Push
git push origin branchNameNext-Lab Preparation
Module:
1) React Basics & Backend Integration (Spring Boot)
By Content ITV