Merge pull request #879 from colouring-cities/fix/842

Fix/842
This commit is contained in:
Ed Chalstrey 2022-07-25 09:34:38 +01:00 committed by GitHub
commit 6ec5e6e1e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 7 deletions

View File

@ -1,10 +1,11 @@
import React from 'react' import React, { Fragment } from 'react'
import { Route, RouteProps, Redirect } from 'react-router-dom'; import { Route, RouteProps, Redirect } from 'react-router-dom';
import { SpinnerIcon } from './components/icons';
import { useAuth } from './auth-context'; import { useAuth } from './auth-context';
export const PrivateRoute: React.FC<RouteProps> = ({component: Component, children, ...rest}) => { export const PrivateRoute: React.FC<RouteProps> = ({component: Component, children, ...rest}) => {
const { isAuthenticated } = useAuth(); const { isAuthenticated, isLoading } = useAuth();
return <Route return <Route
{...rest} {...rest}
@ -17,6 +18,9 @@ export const PrivateRoute: React.FC<RouteProps> = ({component: Component, childr
} }
} else { } else {
if (isLoading) {
return <Fragment><SpinnerIcon spin={true} /> Loading user info...</Fragment>
}
return <Redirect to={{ return <Redirect to={{
pathname: "/login.html", pathname: "/login.html",
state: { from: props.location.pathname } state: { from: props.location.pathname }
@ -35,7 +39,7 @@ export const AuthRoute: React.FC<RouteProps> = ({ component: Component, children
if(isAuthenticated) { if(isAuthenticated) {
let state = props.location.state as any; let state = props.location.state as any;
let from = '/my-account.html'; let from = '/my-account.html';
if ('from' in state){ if (typeof state == 'object' && 'from' in state){
from = state.from; from = state.from;
} }

View File

@ -20,7 +20,7 @@ export const Login: React.FC = () => {
e.preventDefault(); e.preventDefault();
setError(undefined); setError(undefined);
login({ username, password }); login({ username, password }, setError);
}, [username, password]); }, [username, password]);
return ( return (