2020-05-25 23:38:52 +00:00
|
|
|
import { DEMO_MODE } from "@saleor/config";
|
2020-07-07 10:14:12 +00:00
|
|
|
import { User } from "@saleor/fragments/types/User";
|
2019-10-25 12:35:48 +00:00
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
2020-05-25 23:38:52 +00:00
|
|
|
import { maybe } from "@saleor/misc";
|
2019-09-06 15:30:33 +00:00
|
|
|
import {
|
|
|
|
isSupported as isCredentialsManagementAPISupported,
|
|
|
|
login as loginWithCredentialsManagementAPI,
|
|
|
|
saveCredentials
|
|
|
|
} from "@saleor/utils/credentialsManagement";
|
2020-07-13 15:44:02 +00:00
|
|
|
import React, { useContext, useEffect, useState } from "react";
|
2020-05-25 23:38:52 +00:00
|
|
|
import { useIntl } from "react-intl";
|
2020-05-14 09:30:32 +00:00
|
|
|
|
|
|
|
import { UserContext } from "./";
|
2020-05-07 11:04:15 +00:00
|
|
|
import {
|
2020-07-13 14:53:48 +00:00
|
|
|
useTokenAuthMutation,
|
|
|
|
useTokenRefreshMutation,
|
|
|
|
useTokenVerifyMutation
|
2020-05-07 11:04:15 +00:00
|
|
|
} from "./mutations";
|
2020-05-25 23:38:52 +00:00
|
|
|
import {
|
|
|
|
displayDemoMessage,
|
|
|
|
getAuthToken,
|
|
|
|
removeAuthToken,
|
|
|
|
setAuthToken
|
|
|
|
} from "./utils";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
interface AuthProviderProps {
|
|
|
|
children: React.ReactNode;
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
2020-07-13 15:44:02 +00:00
|
|
|
const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
2019-10-25 12:35:48 +00:00
|
|
|
const intl = useIntl();
|
|
|
|
const notify = useNotifier();
|
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
const [tokenAuth, tokenAuthOpts] = useTokenAuthMutation({});
|
|
|
|
const [tokenRefresh] = useTokenRefreshMutation({});
|
|
|
|
const [tokenVerify, tokenVerifyOpts] = useTokenVerifyMutation({});
|
|
|
|
|
|
|
|
const [userContext, setUserContext] = useState<undefined | User>(undefined);
|
|
|
|
const [persistToken] = useState<boolean>(false);
|
2020-07-13 14:53:48 +00:00
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
const onLogin = () => {
|
2020-05-25 23:38:52 +00:00
|
|
|
if (DEMO_MODE) {
|
|
|
|
displayDemoMessage(intl, notify);
|
|
|
|
}
|
|
|
|
};
|
2019-10-25 12:35:48 +00:00
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const token = getAuthToken();
|
|
|
|
if (!!token && !userContext) {
|
|
|
|
verifyToken(token);
|
|
|
|
} else {
|
|
|
|
loginWithCredentialsManagementAPI(login);
|
|
|
|
}
|
|
|
|
}, []);
|
2019-09-03 13:42:15 +00:00
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
useEffect(() => {
|
2019-09-03 13:42:15 +00:00
|
|
|
if (tokenAuthOpts.error || tokenVerifyOpts.error) {
|
2020-07-13 15:44:02 +00:00
|
|
|
logout();
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
2019-09-03 13:42:15 +00:00
|
|
|
if (tokenAuthOpts.data) {
|
|
|
|
const user = tokenAuthOpts.data.tokenCreate.user;
|
2019-06-19 14:40:52 +00:00
|
|
|
// FIXME: Now we set state also when auth fails and returned user is
|
|
|
|
// `null`, because the LoginView uses this `null` to display error.
|
2020-07-13 15:44:02 +00:00
|
|
|
setUserContext(user);
|
2019-06-19 14:40:52 +00:00
|
|
|
if (user) {
|
2020-07-13 15:44:02 +00:00
|
|
|
setAuthToken(tokenAuthOpts.data.tokenCreate.token, persistToken);
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-02-13 15:32:55 +00:00
|
|
|
if (maybe(() => tokenVerifyOpts.data.tokenVerify === null)) {
|
2020-07-13 15:44:02 +00:00
|
|
|
logout();
|
2020-02-11 16:36:53 +00:00
|
|
|
} else {
|
2020-02-13 15:32:55 +00:00
|
|
|
const user = maybe(() => tokenVerifyOpts.data.tokenVerify.user);
|
|
|
|
if (!!user) {
|
2020-07-13 15:44:02 +00:00
|
|
|
setUserContext(user);
|
2020-02-11 16:36:53 +00:00
|
|
|
}
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-13 15:44:02 +00:00
|
|
|
}, [tokenAuthOpts, tokenVerifyOpts]);
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
const login = async (email: string, password: string) => {
|
|
|
|
tokenAuth({ variables: { email, password } }).then(result => {
|
2019-09-05 14:05:56 +00:00
|
|
|
if (result && !result.data.tokenCreate.errors.length) {
|
2020-05-25 23:38:52 +00:00
|
|
|
if (!!onLogin) {
|
|
|
|
onLogin();
|
|
|
|
}
|
2019-09-11 14:04:41 +00:00
|
|
|
saveCredentials(result.data.tokenCreate.user, password);
|
2019-09-05 14:05:56 +00:00
|
|
|
}
|
|
|
|
});
|
2019-09-03 13:42:15 +00:00
|
|
|
};
|
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
const loginByToken = (token: string, user: User) => {
|
|
|
|
setUserContext(user);
|
|
|
|
setAuthToken(token, persistToken);
|
2019-06-19 14:40:52 +00:00
|
|
|
};
|
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
const logout = () => {
|
|
|
|
setUserContext(undefined);
|
2019-09-06 15:30:33 +00:00
|
|
|
if (isCredentialsManagementAPISupported) {
|
2019-09-05 14:05:56 +00:00
|
|
|
navigator.credentials.preventSilentAccess();
|
|
|
|
}
|
2019-06-19 14:40:52 +00:00
|
|
|
removeAuthToken();
|
|
|
|
};
|
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
const verifyToken = (token: string) => tokenVerify({ variables: { token } });
|
2019-09-03 13:42:15 +00:00
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
const refreshToken = async () => {
|
2020-05-07 11:04:15 +00:00
|
|
|
const token = getAuthToken();
|
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
const refreshData = await tokenRefresh({ variables: { token } });
|
2020-05-07 11:04:15 +00:00
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
setAuthToken(refreshData.data.tokenRefresh.token, persistToken);
|
2020-05-07 11:04:15 +00:00
|
|
|
};
|
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
return (
|
|
|
|
<UserContext.Provider
|
|
|
|
value={{
|
|
|
|
login,
|
|
|
|
loginByToken,
|
|
|
|
logout,
|
|
|
|
tokenAuthLoading: tokenAuthOpts.loading,
|
|
|
|
tokenRefresh: refreshToken,
|
|
|
|
tokenVerifyLoading: tokenVerifyOpts.loading,
|
|
|
|
user: userContext
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</UserContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useAuth = () => {
|
|
|
|
const user = useContext(UserContext);
|
|
|
|
const isAuthenticated = !!user;
|
|
|
|
|
|
|
|
return {
|
|
|
|
hasToken: !!getAuthToken(),
|
|
|
|
isAuthenticated,
|
|
|
|
tokenAuthLoading: user.tokenAuthLoading,
|
|
|
|
tokenVerifyLoading: user.tokenVerifyLoading,
|
|
|
|
user
|
|
|
|
};
|
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
export default AuthProvider;
|