saleor-dashboard/src/auth/views/Login.tsx
Dawid Tarasiuk 4880093f63
Use Auth SDK (#1474)
* Use Auth SDK

* Update auth provider hook

* Update sdk module mapping

* Update setting password

* Fix no user details on first login

* Update auth tests

* Cleanups

* Update SDK

Update SDK

Update SDK

Update test recordings

Update SDK

* Implement SDK External Auth

Update new password view

Hnalde external logout

Update SDK

Fix logout external redirect

* Fix login page style

* Update SDK

* Auth Provider cleanups

Update and refactor auth

Auth types cleanups and refactor

* Update channel context provider

* Fix login error handling

* Logout immidiatelly non-staff user

* Update test snapshots

* Trigger CI

* Update to SDK v0.4, remove duplicated UserContext hook

* Handle server errors during login

* Fix wrong login page form submition handling

* Update login error messages

Co-authored-by: Jakub Majorek <majorek.jakub@gmail.com>
2021-12-17 12:10:54 +01:00

101 lines
3 KiB
TypeScript

import { APP_DEFAULT_URI, APP_MOUNT_URI } from "@saleor/config";
import useLocalStorage from "@saleor/hooks/useLocalStorage";
import useNavigator from "@saleor/hooks/useNavigator";
import React, { useEffect } from "react";
import { useQuery } from "react-apollo";
import urlJoin from "url-join";
import useRouter from "use-react-router";
import { useUser } from "..";
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";
interface LoginViewProps {
params: LoginUrlQueryParams;
}
const LoginView: React.FC<LoginViewProps> = ({ params }) => {
const navigate = useNavigator();
const { location } = useRouter();
const {
login,
requestLoginByExternalPlugin,
loginByExternalPlugin,
authenticating,
error
} = useUser();
const {
data: externalAuthentications,
loading: externalAuthenticationsLoading
} = useQuery<AvailableExternalAuthentications>(
availableExternalAuthentications
);
const [
requestedExternalPluginId,
setRequestedExternalPluginId
] = useLocalStorage("requestedExternalPluginId", null);
const handleSubmit = async (data: LoginFormData) => {
const result = await login(data.email, data.password);
const errors = result?.errors || [];
return errors;
};
const handleRequestExternalAuthentication = async (pluginId: string) => {
const result = await requestLoginByExternalPlugin(pluginId, {
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;
}
};
const handleExternalAuthentication = async (code: string, state: string) => {
const result = await loginByExternalPlugin(requestedExternalPluginId, {
code,
state
});
if (result && !result?.errors?.length) {
navigate(APP_DEFAULT_URI);
}
};
useEffect(() => {
const { code, state } = params;
const isCallbackPath = location.pathname.includes(loginCallbackPath);
if (code && state && isCallbackPath) {
handleExternalAuthentication(code, state);
}
}, []);
return (
<LoginPage
error={error}
disabled={authenticating}
externalAuthentications={
externalAuthentications?.shop?.availableExternalAuthentications
}
loading={externalAuthenticationsLoading || authenticating}
onExternalAuthentication={handleRequestExternalAuthentication}
onPasswordRecovery={() => navigate(passwordResetUrl)}
onSubmit={handleSubmit}
/>
);
};
LoginView.displayName = "LoginView";
export default LoginView;