saleor-dashboard/src/auth/views/Login.tsx

98 lines
2.9 KiB
TypeScript
Raw Normal View History

2021-01-26 22:04:54 +00:00
import { APP_DEFAULT_URI, APP_MOUNT_URI } from "@saleor/config";
2019-09-02 19:23:37 +00:00
import useNavigator from "@saleor/hooks/useNavigator";
import useUser from "@saleor/hooks/useUser";
2021-01-26 22:04:54 +00:00
import React, { useEffect, useState } from "react";
import { useQuery } from "react-apollo";
import urlJoin from "url-join";
import useRouter from "use-react-router";
2021-01-26 22:04:54 +00:00
import LoginPage from "../components/LoginPage";
import { LoginFormData } from "../components/LoginPage/form";
import { availableExternalAuthentications } from "../queries";
import { AvailableExternalAuthentications } from "../types/AvailableExternalAuthentications";
import {
loginCallbackPath,
LoginUrlQueryParams,
passwordResetUrl
} from "../urls";
2019-06-19 14:40:52 +00:00
2021-01-26 22:04:54 +00:00
interface LoginViewProps {
params: LoginUrlQueryParams;
}
const LoginView: React.FC<LoginViewProps> = ({ params }) => {
2019-09-02 19:23:37 +00:00
const navigate = useNavigator();
2021-01-26 22:04:54 +00:00
const { location } = useRouter();
const {
login,
requestLoginByExternalPlugin,
loginByExternalPlugin,
tokenAuthLoading
} = useUser();
const [isError, setIsError] = useState(false);
const [isExternalError, setIsExternalError] = useState(false);
const {
data: externalAuthentications,
loading: externalAuthenticationsLoading
} = useQuery<AvailableExternalAuthentications>(
availableExternalAuthentications
);
const handleSubmit = async (data: LoginFormData) => {
const result = await login(data.email, data.password);
const errors = result?.errors || [];
setIsExternalError(false);
setIsError(!result || errors?.length > 0);
return errors;
};
const handleRequestExternalAuthentication = (pluginId: string) =>
requestLoginByExternalPlugin(pluginId, {
redirectUri: urlJoin(
window.location.origin,
APP_MOUNT_URI === APP_DEFAULT_URI ? "" : APP_MOUNT_URI,
loginCallbackPath
)
});
const handleExternalAuthentication = async (code: string, state: string) => {
const result = await loginByExternalPlugin({ code, state });
const errors = result?.errors || [];
setIsError(false);
if (!result || errors?.length > 0) {
setIsExternalError(true);
} else {
navigate(APP_DEFAULT_URI);
}
return errors;
};
useEffect(() => {
const { code, state } = params;
const isCallbackPath = location.pathname.includes(loginCallbackPath);
2019-06-19 14:40:52 +00:00
2021-01-26 22:04:54 +00:00
if (code && state && isCallbackPath) {
handleExternalAuthentication(code, state);
}
}, []);
2019-09-02 19:23:37 +00:00
return (
<LoginPage
2021-01-26 22:04:54 +00:00
error={isError}
externalError={isExternalError}
disabled={tokenAuthLoading}
externalAuthentications={
externalAuthentications?.shop?.availableExternalAuthentications
}
loading={externalAuthenticationsLoading || tokenAuthLoading}
2021-01-26 22:04:54 +00:00
onExternalAuthentication={handleRequestExternalAuthentication}
2019-09-02 19:23:37 +00:00
onPasswordRecovery={() => navigate(passwordResetUrl)}
onSubmit={handleSubmit}
/>
);
};
2019-06-19 14:40:52 +00:00
LoginView.displayName = "LoginView";
export default LoginView;