2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-09-02 19:23:37 +00:00
|
|
|
import { Route, Switch } from "react-router-dom";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-09-03 13:42:15 +00:00
|
|
|
import Layout from "./components/Layout";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { User } from "./types/User";
|
2019-09-02 19:23:37 +00:00
|
|
|
import {
|
|
|
|
newPasswordPath,
|
|
|
|
passwordResetPath,
|
|
|
|
passwordResetSuccessPath
|
|
|
|
} from "./urls";
|
|
|
|
import LoginView from "./views/Login";
|
|
|
|
import NewPassword from "./views/NewPassword";
|
|
|
|
import ResetPassword from "./views/ResetPassword";
|
|
|
|
import ResetPasswordSuccess from "./views/ResetPasswordSuccess";
|
2020-01-20 15:42:23 +00:00
|
|
|
import LoginLoading from "./components/LoginLoading";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
interface UserContext {
|
2019-09-05 14:05:56 +00:00
|
|
|
login: (username: string, password: string) => void;
|
2019-09-03 13:42:15 +00:00
|
|
|
loginByToken: (token: string, user: User) => void;
|
2019-06-19 14:40:52 +00:00
|
|
|
logout: () => void;
|
2019-09-02 19:23:37 +00:00
|
|
|
tokenAuthLoading: boolean;
|
2020-05-07 11:04:15 +00:00
|
|
|
tokenRefresh: () => Promise<void>;
|
2019-09-02 19:23:37 +00:00
|
|
|
tokenVerifyLoading: boolean;
|
2019-06-19 14:40:52 +00:00
|
|
|
user?: User;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const UserContext = React.createContext<UserContext>({
|
|
|
|
login: undefined,
|
2019-09-03 13:42:15 +00:00
|
|
|
loginByToken: undefined,
|
2019-09-02 19:23:37 +00:00
|
|
|
logout: undefined,
|
|
|
|
tokenAuthLoading: false,
|
2020-05-07 11:04:15 +00:00
|
|
|
tokenRefresh: undefined,
|
2019-09-02 19:23:37 +00:00
|
|
|
tokenVerifyLoading: false
|
2019-06-19 14:40:52 +00:00
|
|
|
});
|
|
|
|
|
2020-01-20 15:42:23 +00:00
|
|
|
interface AuthRouterProps {
|
|
|
|
hasToken: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AuthRouter: React.FC<AuthRouterProps> = ({ hasToken }) => (
|
2019-09-03 13:42:15 +00:00
|
|
|
<Layout>
|
|
|
|
<Switch>
|
|
|
|
<Route path={passwordResetSuccessPath} component={ResetPasswordSuccess} />
|
|
|
|
<Route path={passwordResetPath} component={ResetPassword} />
|
2020-01-20 15:42:23 +00:00
|
|
|
{!hasToken ? (
|
|
|
|
<Route path={newPasswordPath} component={NewPassword} />
|
|
|
|
) : (
|
|
|
|
<LoginLoading />
|
|
|
|
)}
|
2019-09-03 13:42:15 +00:00
|
|
|
<Route component={LoginView} />
|
|
|
|
</Switch>
|
|
|
|
</Layout>
|
2019-09-02 19:23:37 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
AuthRouter.displayName = "AuthRouter";
|
|
|
|
export default AuthRouter;
|
2019-09-03 13:42:15 +00:00
|
|
|
|
|
|
|
export * from "./utils";
|