2020-07-07 10:14:12 +00:00
|
|
|
import { User } from "@saleor/fragments/types/User";
|
2021-01-26 22:04:54 +00:00
|
|
|
import { parse as parseQs } from "qs";
|
|
|
|
import React, { MutableRefObject } from "react";
|
|
|
|
import { Route, RouteComponentProps, 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-09-02 19:23:37 +00:00
|
|
|
import {
|
2021-01-26 22:04:54 +00:00
|
|
|
ExternalLoginInput,
|
|
|
|
RequestExternalLoginInput
|
|
|
|
} from "./hooks/useExternalAuthProvider";
|
|
|
|
import { ExternalObtainAccessTokens_externalObtainAccessTokens } from "./types/ExternalObtainAccessTokens";
|
|
|
|
import { TokenAuth_tokenCreate } from "./types/TokenAuth";
|
|
|
|
import {
|
|
|
|
LoginUrlQueryParams,
|
2019-09-02 19:23:37 +00:00
|
|
|
newPasswordPath,
|
|
|
|
passwordResetPath,
|
|
|
|
passwordResetSuccessPath
|
|
|
|
} from "./urls";
|
2021-01-26 22:04:54 +00:00
|
|
|
import LoginViewComponent from "./views/Login";
|
2019-09-02 19:23:37 +00:00
|
|
|
import NewPassword from "./views/NewPassword";
|
|
|
|
import ResetPassword from "./views/ResetPassword";
|
|
|
|
import ResetPasswordSuccess from "./views/ResetPasswordSuccess";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2021-01-26 22:04:54 +00:00
|
|
|
const LoginView: React.FC<RouteComponentProps<any>> = () => {
|
|
|
|
const qs = parseQs(location.search.substr(1));
|
|
|
|
const params: LoginUrlQueryParams = qs;
|
|
|
|
|
|
|
|
return <LoginViewComponent params={params} />;
|
|
|
|
};
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
interface UserContext {
|
2021-01-26 22:04:54 +00:00
|
|
|
login: (username: string, password: string) => Promise<TokenAuth_tokenCreate>;
|
|
|
|
loginByExternalPlugin: (
|
|
|
|
input: ExternalLoginInput
|
|
|
|
) => Promise<ExternalObtainAccessTokens_externalObtainAccessTokens>;
|
2020-07-23 13:37:39 +00:00
|
|
|
loginByToken: (auth: string, csrf: string, user: User) => void;
|
2019-06-19 14:40:52 +00:00
|
|
|
logout: () => void;
|
2021-01-26 22:04:54 +00:00
|
|
|
requestLoginByExternalPlugin: (
|
|
|
|
pluginId: string,
|
|
|
|
input: RequestExternalLoginInput
|
|
|
|
) => Promise<void>;
|
2019-09-02 19:23:37 +00:00
|
|
|
tokenAuthLoading: boolean;
|
2020-07-23 13:37:39 +00:00
|
|
|
tokenRefresh: () => Promise<boolean>;
|
2019-09-02 19:23:37 +00:00
|
|
|
tokenVerifyLoading: boolean;
|
2019-06-19 14:40:52 +00:00
|
|
|
user?: User;
|
2021-01-26 22:04:54 +00:00
|
|
|
autologinPromise?: MutableRefObject<Promise<any>>;
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const UserContext = React.createContext<UserContext>({
|
|
|
|
login: undefined,
|
2021-01-26 22:04:54 +00:00
|
|
|
loginByExternalPlugin: undefined,
|
2019-09-03 13:42:15 +00:00
|
|
|
loginByToken: undefined,
|
2019-09-02 19:23:37 +00:00
|
|
|
logout: undefined,
|
2021-01-26 22:04:54 +00:00
|
|
|
requestLoginByExternalPlugin: undefined,
|
2019-09-02 19:23:37 +00:00
|
|
|
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-07-21 16:49:20 +00:00
|
|
|
const AuthRouter: React.FC = () => (
|
2019-09-03 13:42:15 +00:00
|
|
|
<Layout>
|
|
|
|
<Switch>
|
|
|
|
<Route path={passwordResetSuccessPath} component={ResetPasswordSuccess} />
|
|
|
|
<Route path={passwordResetPath} component={ResetPassword} />
|
2020-07-21 16:49:20 +00:00
|
|
|
<Route path={newPasswordPath} component={NewPassword} />
|
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";
|