Skip to content

Commit cb889de

Browse files
authored
Merge pull request #142 from mcode/login-bug
Login bug
2 parents ee6433f + 7001b3b commit cb889de

5 files changed

Lines changed: 101 additions & 35 deletions

File tree

frontend/src/App.tsx

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { BrowserRouter as Router, Link, Route, Routes } from 'react-router-dom';
66
import './App.css';
77
import DoctorOrders from './views/DoctorOrders/DoctorOrders';
88
import Login from './views/Login/Login';
9+
import ProtectedRoute from './components/ProtectedRoute';
10+
import { AuthProvider } from './contexts/AuthContext';
911
import axios from 'axios';
1012

1113
axios.defaults.baseURL = process.env.REACT_APP_PIMS_BACKEND_URL
@@ -17,36 +19,44 @@ const basename = process.env.REACT_APP_VITE_BASE?.replace(/\/$/, '') || '';
1719

1820
function App() {
1921
return (
20-
<Box>
21-
<Router basename={basename}>
22-
<div className="App">
23-
<Container className="NavContainer" maxWidth="xl">
24-
<div className="containerg">
25-
<div className="logo">
26-
<LocalPharmacyIcon
27-
sx={{ color: 'white', fontSize: 40, paddingTop: 2.5, paddingRight: 2.5 }}
28-
/>
29-
<h1>Pharmacy</h1>
22+
<AuthProvider>
23+
<Box>
24+
<Router basename={basename}>
25+
<div className="App">
26+
<Container className="NavContainer" maxWidth="xl">
27+
<div className="containerg">
28+
<div className="logo">
29+
<LocalPharmacyIcon
30+
sx={{ color: 'white', fontSize: 40, paddingTop: 2.5, paddingRight: 2.5 }}
31+
/>
32+
<h1>Pharmacy</h1>
33+
</div>
34+
<div className="links">
35+
<Link className="NavButtons" to="/DoctorOrders">
36+
<Button variant="contained">Doctor Orders</Button>
37+
</Link>
38+
<Link className="NavButtons" to="/Login">
39+
<Button variant="contained">Login</Button>
40+
</Link>
41+
</div>
3042
</div>
31-
<div className="links">
32-
<Link className="NavButtons" to="/DoctorOrders">
33-
<Button variant="contained">Doctor Orders</Button>
34-
</Link>
35-
<Link className="NavButtons" to="/Login">
36-
<Button variant="contained">Login</Button>
37-
</Link>
38-
</div>
39-
</div>
40-
</Container>
41-
</div>
42-
<Routes>
43-
{/* Initial load to login page, will need to change to check for user authentication to load to correct page */}
44-
<Route path="/" element={<Login />} />
45-
<Route path="/Login" element={<Login />}></Route>
46-
<Route path="/DoctorOrders" element={<DoctorOrders />}></Route>
47-
</Routes>
48-
</Router>
49-
</Box>
43+
</Container>
44+
</div>
45+
<Routes>
46+
<Route path="/" element={<Login />} />
47+
<Route path="/Login" element={<Login />} />
48+
<Route
49+
path="/DoctorOrders"
50+
element={
51+
<ProtectedRoute>
52+
<DoctorOrders />
53+
</ProtectedRoute>
54+
}
55+
/>
56+
</Routes>
57+
</Router>
58+
</Box>
59+
</AuthProvider>
5060
);
5161
}
5262

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Navigate } from 'react-router-dom';
2+
import { useAuth } from '../contexts/AuthContext';
3+
4+
interface ProtectedRouteProps {
5+
children: React.ReactNode;
6+
}
7+
8+
export default function ProtectedRoute({ children }: ProtectedRouteProps) {
9+
const { isAuthenticated } = useAuth();
10+
11+
if (!isAuthenticated) {
12+
return <Navigate to="/Login" replace />;
13+
}
14+
15+
return <>{children}</>;
16+
}

frontend/src/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"realm": "ClientFhirServer",
33
"client": "pims-login",
4-
"auth": "http://localhost:8180/auth",
4+
"auth": "http://localhost:8180",
55
"scopeId": "pims"
66
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React, { createContext, useContext, useState } from 'react';
2+
3+
interface AuthContextType {
4+
token: string | null;
5+
login: (token: string) => void;
6+
logout: () => void;
7+
isAuthenticated: boolean;
8+
}
9+
10+
const AuthContext = createContext<AuthContextType | undefined>(undefined);
11+
12+
export function AuthProvider({ children }: { children: React.ReactNode }) {
13+
const [token, setToken] = useState<string | null>(null);
14+
15+
const login = (newToken: string) => {
16+
setToken(newToken);
17+
};
18+
19+
const logout = () => {
20+
setToken(null);
21+
};
22+
23+
return (
24+
<AuthContext.Provider value={{ token, login, logout, isAuthenticated: Boolean(token) }}>
25+
{children}
26+
</AuthContext.Provider>
27+
);
28+
}
29+
30+
export function useAuth() {
31+
const context = useContext(AuthContext);
32+
if (!context) {
33+
throw new Error('useAuth must be used within AuthProvider');
34+
}
35+
return context;
36+
}

frontend/src/views/Login/Login.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ import LockOutlinedIcon from '@mui/icons-material/LockOutlined';
33
import { Avatar, Box, Button, Container, CssBaseline, TextField, Typography } from '@mui/material';
44
import axios from 'axios';
55
import * as React from 'react';
6+
import { useNavigate } from 'react-router-dom';
7+
import { useAuth } from '../../contexts/AuthContext';
68
import config from '../../config.json';
79

810
export default function Login() {
9-
const [token, setToken] = React.useState<string | null>(null);
11+
const { login, isAuthenticated } = useAuth();
12+
const navigate = useNavigate();
13+
1014
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
1115
event.preventDefault();
1216
const data = new FormData(event.currentTarget);
@@ -23,16 +27,16 @@ export default function Login() {
2327
withCredentials: true
2428
})
2529
.then(result => {
26-
// do something with the token
2730
const scope = result.data.scope.split(' ').includes(config.scopeId);
2831
if (scope) {
29-
setToken(result.data.access_token);
32+
login(result.data.access_token);
33+
navigate('/DoctorOrders');
3034
} else {
3135
console.error('Unauthorized User');
3236
}
3337
})
3438
.catch(err => {
35-
if (err.response.status === 401) {
39+
if (err.response?.status === 401) {
3640
console.error('Unknown user');
3741
} else {
3842
console.error(err);
@@ -52,7 +56,7 @@ export default function Login() {
5256
alignItems: 'center'
5357
}}
5458
>
55-
{token ? (
59+
{isAuthenticated ? (
5660
<Avatar sx={{ m: 1, bgcolor: 'secondary.success' }}>
5761
<LockOpenOutlinedIcon />
5862
</Avatar>

0 commit comments

Comments
 (0)