Skip to content

Commit

Permalink
Involuntary logout issue resolved.
Browse files Browse the repository at this point in the history
  • Loading branch information
UO287687 committed Apr 7, 2024
1 parent fc9024f commit e33819b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
21 changes: 21 additions & 0 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ app.post('/login', async (req, res) => {
res.status(500).json({ error: 'Service down' });
}
});
app.get('/verify', async (req, res) => {
try {
if (req.headers.authorization) {
try{
const authResponse = await axios.get(authServiceUrl+'/verify', {
headers: {
Authorization: req.headers.authorization
}
});
res.json(authResponse.data);
} catch (error) {
res.status(error.response.status).json(error.response.data);
}
} else {
res.status(401).json({ error: 'Unauthorized' });
}
} catch (error) {
res.status(500).json({ error: 'Service down' });
}
});


app.post('/adduser', async (req, res) => {
try {
Expand Down
21 changes: 20 additions & 1 deletion webapp/src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SessionContext } from '../SessionContext';

const Login = ({ goTo }) => {

const { saveSessionData } = useContext(SessionContext);
const { saveSessionData, sessionData } = useContext(SessionContext);

const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
Expand Down Expand Up @@ -36,10 +36,29 @@ const Login = ({ goTo }) => {
setError(error.response.data.error);
}
};
const autologin = async () => {
try {
const response = await axios.get(`${apiEndpoint}/verify`, {
headers: {
Authorization: 'Bearer '+sessionData.token
}
});
if(response.status && response.status===200){
goTo(1);
}
} catch (error) {
}
};

const handleCloseSnackbar = () => {
setOpenSnackbar(false);
};
useEffect(() => {
if (sessionData && sessionData.token) {
autologin();
}
//eslint-disable-next-line
}, []);

useEffect(() => {
if (loginSuccess) {
Expand Down
5 changes: 3 additions & 2 deletions webapp/src/components/Nav.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const gatewayUrl = process.env.REACT_APP_API_ENDPOINT || "http://localhost:8000"

function Nav({ goTo }) {

const { sessionData } = useContext(SessionContext);
const { sessionData, clearSessionData } = useContext(SessionContext);
const username = sessionData ? sessionData.username : 'noUser';
const profileImgSrc = sessionData && sessionData.profileImage ?
require(`../assets/${sessionData.profileImage}`) : defaultProfileImg;
Expand Down Expand Up @@ -49,7 +49,8 @@ function Nav({ goTo }) {
handleCloseUserMenu();
}

const logoutClic = () => {
const logoutClic = async() => {
clearSessionData();
goTo(0);
handleCloseUserMenu();
}
Expand Down

0 comments on commit e33819b

Please sign in to comment.