fix: fixed external navigation bug

Signed-off-by: Raul Kele <raulkeleblk@gmail.com>
This commit is contained in:
Raul Kele 2023-01-12 12:41:44 +02:00
parent 2066151329
commit 9eeb00e801
4 changed files with 15 additions and 37 deletions

View File

@ -2,6 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<base href="/">
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

View File

@ -16,7 +16,6 @@ function App() {
return localStorageToken ? true : false;
};
const [isAuthEnabled, setIsAuthEnabled] = useState(true);
const [isLoggedIn, setIsLoggedIn] = useState(isToken());
return (
@ -29,21 +28,13 @@ function App() {
<Route path="/explore" element={<ExplorePage />} />
<Route path="/image/:name" element={<RepoPage />} />
<Route path="/image/:reponame/tag/:tag" element={<TagPage />} />
<Route path="*" element={<Navigate to="/home" />} />
</Route>
<Route element={<AuthWrapper isLoggedIn={!isLoggedIn} redirect="/" />}>
<Route
path="/login"
element={
<LoginPage
isAuthEnabled={isAuthEnabled}
setIsAuthEnabled={setIsAuthEnabled}
isLoggedIn={isLoggedIn}
setIsLoggedIn={setIsLoggedIn}
/>
}
/>
<Route path="/login" element={<LoginPage isLoggedIn={isLoggedIn} setIsLoggedIn={setIsLoggedIn} />} />
<Route path="*" element={<Navigate to="/login" />} />
</Route>
<Route path="*" element={<Navigate to="/home" />} />
</Routes>
</Router>
</div>

View File

@ -99,13 +99,7 @@ const useStyles = makeStyles(() => ({
}
}));
export default function SignIn({
isAuthEnabled,
setIsAuthEnabled,
isLoggedIn,
setIsLoggedIn,
wrapperSetLoading = () => {}
}) {
export default function SignIn({ isLoggedIn, setIsLoggedIn, wrapperSetLoading = () => {} }) {
const [usernameError, setUsernameError] = useState(null);
const [passwordError, setPasswordError] = useState(null);
const [username, setUsername] = useState(null);
@ -119,7 +113,7 @@ export default function SignIn({
useEffect(() => {
setIsLoading(true);
if (isAuthEnabled && isLoggedIn) {
if (isLoggedIn) {
setIsLoading(false);
wrapperSetLoading(false);
navigate('/home');
@ -128,7 +122,7 @@ export default function SignIn({
.get(`${host()}/v2/`, abortController.signal)
.then((response) => {
if (response.status === 200) {
setIsAuthEnabled(false);
localStorage.setItem('token', '-');
setIsLoggedIn(true);
setIsLoading(false);
wrapperSetLoading(false);
@ -136,7 +130,7 @@ export default function SignIn({
}
})
.catch(() => {
setIsAuthEnabled(true);
localStorage.setItem('token', '-');
setIsLoading(false);
wrapperSetLoading(false);
});
@ -150,7 +144,7 @@ export default function SignIn({
event.preventDefault();
setRequestProcessing(true);
let cfg = {};
if (isAuthEnabled) {
if (isLoggedIn) {
const token = btoa(username + ':' + password);
cfg = {
headers: {
@ -162,12 +156,10 @@ export default function SignIn({
.get(`${host()}${endpoints.repoList}`, abortController.signal, cfg)
.then((response) => {
if (response.data && response.data.data) {
if (isAuthEnabled) {
const token = btoa(username + ':' + password);
localStorage.setItem('token', token);
setRequestProcessing(false);
setRequestError(false);
}
const token = btoa(username + ':' + password);
localStorage.setItem('token', token);
setRequestProcessing(false);
setRequestError(false);
setIsLoggedIn(true);
navigate('/home');
}

View File

@ -20,7 +20,7 @@ const useStyles = makeStyles(() => ({
}
}));
function LoginPage({ isAuthEnabled, setIsAuthEnabled, isLoggedIn, setIsLoggedIn }) {
function LoginPage({ isLoggedIn, setIsLoggedIn }) {
const [isLoading, setIsLoading] = useState(true);
const classes = useStyles();
@ -31,13 +31,7 @@ function LoginPage({ isAuthEnabled, setIsAuthEnabled, isLoggedIn, setIsLoggedIn
<SigninPresentation />
</Grid>
<Grid item xs={6} className={isLoading ? classes.loadingHidden : ''}>
<SignIn
isAuthEnabled={isAuthEnabled}
setIsAuthEnabled={setIsAuthEnabled}
isLoggedIn={isLoggedIn}
setIsLoggedIn={setIsLoggedIn}
wrapperSetLoading={setIsLoading}
/>
<SignIn isLoggedIn={isLoggedIn} setIsLoggedIn={setIsLoggedIn} wrapperSetLoading={setIsLoading} />
</Grid>
</Grid>
);