Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Showing friendly error message when log in fails #272

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 76 additions & 66 deletions frontend/src/components/Login/Authenticate.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,80 @@
import { Alert, LinearProgress, Stack } from "@mui/material";
import React, { useContext, useEffect, useState } from "react";
import { useMutation } from "react-query";
import { useLocation, useNavigate, useParams } from "react-router-dom";
import RefreshTwoTone from "@mui/icons-material/RefreshTwoTone";
import axios from "../../axios";
import AuthContext from "../../Context/AuthContext";
const Authenticate = (props) => {
const { authenticate } = useContext(AuthContext);
const [error, setError] = useState(false);
let location = useLocation();
const navigate = useNavigate();

import { LinearProgress, Stack } from '@mui/material';
import React, { useContext, useEffect, useState } from 'react'
import { useMutation } from 'react-query';
import { useLocation, useNavigate, useParams } from 'react-router-dom';
import axios from '../../axios'
import AuthContext from '../../Context/AuthContext';
const Authenticate = props => {
const callback = async () => {
try {
// const params = `?${location.search.split("&")[1]}&${location.search.split("&")[2]}`
const params = location.search;
console.log("calling auth/callback");
const res = await axios.get(`/auth/callback/${params}`);

const {authenticate} = useContext(AuthContext);
const [error, setError] = useState(false)
let location = useLocation();
const navigate = useNavigate()

const callback = async () => {
try {

// const params = `?${location.search.split("&")[1]}&${location.search.split("&")[2]}`
const params =location.search;
console.log('calling auth/callback')
const res = await axios.get(`/auth/callback/${params}`);

if (!res)
setError("OSM Authentication API is not avialble, please contact HOT tech team [email protected]");

if (res.error)
setError(res.error.response.statusText);


authenticate(res.data.access_token)


navigate(`${localStorage.getItem("redirect") ? localStorage.getItem("redirect") : "/"}`)
return res.data;
} catch (e) {
console.log("isError");
setError(e);
} finally {

}
};
const { mutate, isLoading} = useMutation(callback);
if (!res)
setError(
"OSM Authentication API is not avialble, please contact HOT tech team [email protected]"
);
console.log("res", res);
if (res.error) setError("Error");

useEffect(() => {

// console.log(location)
mutate()
return () => {

}
}, [])

return <>
{isLoading &&
<Stack sx={{ width: '100%', color: 'grey.500' }} spacing={2}>
<LinearProgress color="hot" />
<LinearProgress color="hot" />
<LinearProgress color="hot" />
<LinearProgress color="hot" />
<LinearProgress color="hot" />
<LinearProgress color="hot" />
<LinearProgress color="hot" />
<LinearProgress color="hot" />
<LinearProgress color="hot" />
</Stack>}
{error && error}
</>
}
authenticate(res.data.access_token);

export default Authenticate;
navigate(
`${
localStorage.getItem("redirect")
? localStorage.getItem("redirect")
: "/"
}`
);
return res.data;
} catch (e) {
console.log("isError");
setError("Error");
} finally {
}
};
const { mutate, isLoading } = useMutation(callback);

useEffect(() => {
// console.log(location)
mutate();
return () => {};
}, []);

return (
<>
{isLoading && (
<Stack sx={{ width: "100%", color: "grey.500" }} spacing={2}>
<LinearProgress color="hot" />
<LinearProgress color="hot" />
<LinearProgress color="hot" />
<LinearProgress color="hot" />
<LinearProgress color="hot" />
<LinearProgress color="hot" />
<LinearProgress color="hot" />
<LinearProgress color="hot" />
<LinearProgress color="hot" />
</Stack>
)}
{error && (
<p>
<Alert severity="error">
Please press refresh button{" "}
<RefreshTwoTone color="error"></RefreshTwoTone> in you browser if
you see this error <br></br>
</Alert>
</p>
)}
</>
);
};

export default Authenticate;
Loading