2019-10-25 12:35:48 +00:00
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
2021-01-26 22:04:54 +00:00
|
|
|
import React, { useContext } from "react";
|
|
|
|
import { useApolloClient } from "react-apollo";
|
|
|
|
import { useIntl } from "react-intl";
|
2020-05-14 09:30:32 +00:00
|
|
|
|
|
|
|
import { UserContext } from "./";
|
2021-01-26 22:04:54 +00:00
|
|
|
import { useAuthProvider } from "./hooks/useAuthProvider";
|
|
|
|
import { getTokens } from "./utils";
|
2020-07-20 10:12:14 +00:00
|
|
|
|
|
|
|
interface AuthProviderProps {
|
|
|
|
children: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
|
|
|
const apolloClient = useApolloClient();
|
|
|
|
const intl = useIntl();
|
|
|
|
const notify = useNotifier();
|
|
|
|
|
2021-01-26 22:04:54 +00:00
|
|
|
const authProvider = useAuthProvider({ apolloClient, intl, notify });
|
2020-07-20 10:12:14 +00:00
|
|
|
|
2020-07-13 15:44:02 +00:00
|
|
|
return (
|
2021-01-26 22:04:54 +00:00
|
|
|
<UserContext.Provider value={authProvider}>{children}</UserContext.Provider>
|
2020-07-13 15:44:02 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
2020-07-23 13:37:39 +00:00
|
|
|
hasToken: !!getTokens(),
|
2020-07-13 15:44:02 +00:00
|
|
|
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;
|