commit
6ec5e6e1e2
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 (
|
||||||
|
Loading…
Reference in New Issue
Block a user