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

110 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-01-26 22:04:54 +00:00
import { APP_DEFAULT_URI, APP_MOUNT_URI } from "@saleor/config";
import useLocalStorage from "@saleor/hooks/useLocalStorage";
2019-09-02 19:23:37 +00:00
import useNavigator from "@saleor/hooks/useNavigator";
import React, { useEffect } from "react";
2021-01-26 22:04:54 +00:00
import { useQuery } from "react-apollo";
import urlJoin from "url-join";
import useRouter from "use-react-router";
import { useUser } from "..";
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,
authenticating,
error
2021-01-26 22:04:54 +00:00
} = useUser();
const {
data: externalAuthentications,
loading: externalAuthenticationsLoading
} = useQuery<AvailableExternalAuthentications>(
availableExternalAuthentications
);
const [
requestedExternalPluginId,
setRequestedExternalPluginId
] = useLocalStorage("requestedExternalPluginId", null);
2021-01-26 22:04:54 +00:00
const [fallbackUri, setFallbackUri] = useLocalStorage(
"externalLoginFallbackUri",
null
);
2021-01-26 22:04:54 +00:00
const handleSubmit = async (data: LoginFormData) => {
const result = await login(data.email, data.password);
const errors = result?.errors || [];
return errors;
};
const handleRequestExternalAuthentication = async (pluginId: string) => {
setFallbackUri(location.pathname);
const result = await requestLoginByExternalPlugin(pluginId, {
2021-01-26 22:04:54 +00:00
redirectUri: urlJoin(
window.location.origin,
APP_MOUNT_URI === APP_DEFAULT_URI ? "" : APP_MOUNT_URI,
loginCallbackPath
)
});
const data = JSON.parse(result?.authenticationData || "");
if (data && !result?.errors?.length) {
setRequestedExternalPluginId(pluginId);
window.location.href = data.authorizationUrl;
}
};
2021-01-26 22:04:54 +00:00
const handleExternalAuthentication = async (code: string, state: string) => {
const result = await loginByExternalPlugin(requestedExternalPluginId, {
code,
state
});
if (result && !result?.errors?.length) {
navigate(fallbackUri ?? "/");
setFallbackUri(null);
2021-01-26 22:04:54 +00:00
}
};
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
error={error}
disabled={authenticating}
2021-01-26 22:04:54 +00:00
externalAuthentications={
externalAuthentications?.shop?.availableExternalAuthentications
}
loading={externalAuthenticationsLoading || authenticating}
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;