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-07-20 09:46:59 +00:00
|
|
|
import { getMutationStatus } 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-07-14 14:31:41 +00:00
|
|
|
import { useMutation } from "react-apollo";
|
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-14 14:31:41 +00:00
|
|
|
tokenAuthMutation,
|
|
|
|
tokenRefreshMutation,
|
|
|
|
tokenVerifyMutation
|
2020-05-07 11:04:15 +00:00
|
|
|
} from "./mutations";
|
2020-07-14 14:31:41 +00:00
|
|
|
import { RefreshToken, RefreshTokenVariables } from "./types/RefreshToken";
|
|
|
|
import { TokenAuth, TokenAuthVariables } from "./types/TokenAuth";
|
|
|
|
import { VerifyToken, VerifyTokenVariables } from "./types/VerifyToken";
|
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-20 09:46:59 +00:00
|
|
|
const persistToken = false;
|
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
interface AuthProviderProps {
|
|
|
|
children: React.ReactNode;
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
2020-07-14 08:11:43 +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-20 09:46:59 +00:00
|
|
|
const [userContext, setUserContext] = useState<undefined | User>(undefined);
|
|
|
|
|
|
|
|
const logout = () => {
|
|
|
|
setUserContext(undefined);
|
|
|
|
if (isCredentialsManagementAPISupported) {
|
|
|
|
navigator.credentials.preventSilentAccess();
|
|
|
|
}
|
|
|
|
removeAuthToken();
|
|
|
|
};
|
|
|
|
|
2020-07-14 14:31:41 +00:00
|
|
|
const [tokenAuth, tokenAuthResult] = useMutation<
|
|
|
|
TokenAuth,
|
|
|
|
TokenAuthVariables
|
2020-07-20 09:46:59 +00:00
|
|
|
>(tokenAuthMutation, {
|
|
|
|
onCompleted: result => {
|
2020-07-20 09:58:39 +00:00
|
|
|
if (result.tokenCreate.errors.length > 0) {
|
|
|
|
logout();
|
|
|
|
}
|
|
|
|
|
2020-07-20 09:46:59 +00:00
|
|
|
const user = result.tokenCreate.user;
|
|
|
|
|
|
|
|
// FIXME: Now we set state also when auth fails and returned user is
|
|
|
|
// `null`, because the LoginView uses this `null` to display error.
|
|
|
|
setUserContext(user);
|
|
|
|
if (user) {
|
|
|
|
setAuthToken(result.tokenCreate.token, persistToken);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onError: logout
|
|
|
|
});
|
2020-07-14 14:31:41 +00:00
|
|
|
const [tokenRefresh] = useMutation<RefreshToken, RefreshTokenVariables>(
|
2020-07-20 09:46:59 +00:00
|
|
|
tokenRefreshMutation,
|
|
|
|
{
|
|
|
|
onError: logout
|
|
|
|
}
|
2020-07-14 14:31:41 +00:00
|
|
|
);
|
|
|
|
const [tokenVerify, tokenVerifyResult] = useMutation<
|
|
|
|
VerifyToken,
|
|
|
|
VerifyTokenVariables
|
2020-07-20 09:46:59 +00:00
|
|
|
>(tokenVerifyMutation, {
|
|
|
|
onCompleted: result => {
|
|
|
|
if (result.tokenVerify === null) {
|
|
|
|
logout();
|
|
|
|
} else {
|
|
|
|
const user = result.tokenVerify?.user;
|
|
|
|
|
|
|
|
if (!!user) {
|
|
|
|
setUserContext(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onError: logout
|
|
|
|
});
|
2020-07-14 14:31:41 +00:00
|
|
|
|
|
|
|
const tokenAuthOpts = {
|
|
|
|
...tokenAuthResult,
|
|
|
|
status: getMutationStatus(tokenAuthResult)
|
|
|
|
};
|
|
|
|
const tokenVerifyOpts = {
|
|
|
|
...tokenVerifyResult,
|
|
|
|
status: getMutationStatus(tokenVerifyResult)
|
|
|
|
};
|
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-15 14:09:12 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const token = getAuthToken();
|
|
|
|
if (!!token && !userContext) {
|
|
|
|
verifyToken(token);
|
|
|
|
} else {
|
|
|
|
loginWithCredentialsManagementAPI(login);
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
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 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);
|
2020-07-14 14:31:41 +00:00
|
|
|
const isAuthenticated = !!user.user;
|
2020-07-13 15:44:02 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
hasToken: !!getAuthToken(),
|
|
|
|
isAuthenticated,
|
|
|
|
tokenAuthLoading: user.tokenAuthLoading,
|
|
|
|
tokenVerifyLoading: user.tokenVerifyLoading,
|
2020-07-14 08:11:43 +00:00
|
|
|
user: user.user
|
2020-07-13 15:44:02 +00:00
|
|
|
};
|
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
export default AuthProvider;
|