Create mutation hooks for auth provider

This commit is contained in:
Dawid Tarasiuk 2020-07-13 16:53:48 +02:00 committed by dominik-zeglen
parent 02156e5d44
commit b633e2f470
3 changed files with 34 additions and 44 deletions

View file

@ -13,9 +13,9 @@ import { useIntl } from "react-intl";
import { UserContext } from "./"; import { UserContext } from "./";
import { import {
TokenRefreshMutation, useTokenAuthMutation,
TypedTokenAuthMutation, useTokenRefreshMutation,
TypedVerifyTokenMutation useTokenVerifyMutation
} from "./mutations"; } from "./mutations";
import { RefreshToken, RefreshTokenVariables } from "./types/RefreshToken"; import { RefreshToken, RefreshTokenVariables } from "./types/RefreshToken";
import { TokenAuth, TokenAuthVariables } from "./types/TokenAuth"; import { TokenAuth, TokenAuthVariables } from "./types/TokenAuth";
@ -42,6 +42,10 @@ const AuthProviderOperations: React.FC<AuthProviderOperationsProps> = ({
const intl = useIntl(); const intl = useIntl();
const notify = useNotifier(); const notify = useNotifier();
const tokenAuth = useTokenAuthMutation({});
const tokenRefresh = useTokenRefreshMutation({});
const tokenVerify = useTokenVerifyMutation({});
const handleLogin = () => { const handleLogin = () => {
if (DEMO_MODE) { if (DEMO_MODE) {
displayDemoMessage(intl, notify); displayDemoMessage(intl, notify);
@ -49,26 +53,14 @@ const AuthProviderOperations: React.FC<AuthProviderOperationsProps> = ({
}; };
return ( return (
<TypedTokenAuthMutation> <AuthProvider
{(...tokenAuth) => ( tokenAuth={tokenAuth}
<TypedVerifyTokenMutation> tokenVerify={tokenVerify}
{(...tokenVerify) => ( tokenRefresh={tokenRefresh}
<TokenRefreshMutation> onLogin={handleLogin}
{(...tokenRefresh) => ( >
<AuthProvider {children}
tokenAuth={tokenAuth} </AuthProvider>
tokenVerify={tokenVerify}
tokenRefresh={tokenRefresh}
onLogin={handleLogin}
>
{children}
</AuthProvider>
)}
</TokenRefreshMutation>
)}
</TypedVerifyTokenMutation>
)}
</TypedTokenAuthMutation>
); );
}; };

View file

@ -1,5 +1,6 @@
import { fragmentUser } from "@saleor/fragments/auth"; import { fragmentUser } from "@saleor/fragments/auth";
import { accountErrorFragment } from "@saleor/fragments/errors"; import { accountErrorFragment } from "@saleor/fragments/errors";
import makeMutation from "@saleor/hooks/makeMutation";
import gql from "graphql-tag"; import gql from "graphql-tag";
import { TypedMutation } from "../mutations"; import { TypedMutation } from "../mutations";
@ -17,7 +18,7 @@ export const tokenAuthMutation = gql`
mutation TokenAuth($email: String!, $password: String!) { mutation TokenAuth($email: String!, $password: String!) {
tokenCreate(email: $email, password: $password) { tokenCreate(email: $email, password: $password) {
token token
errors { errors: accountErrors {
field field
message message
} }
@ -27,11 +28,9 @@ export const tokenAuthMutation = gql`
} }
} }
`; `;
export const useTokenAuthMutation = makeMutation<TokenAuth, TokenAuthVariables>(
export const TypedTokenAuthMutation = TypedMutation< tokenAuthMutation
TokenAuth, );
TokenAuthVariables
>(tokenAuthMutation);
export const tokenVerifyMutation = gql` export const tokenVerifyMutation = gql`
${fragmentUser} ${fragmentUser}
@ -44,12 +43,23 @@ export const tokenVerifyMutation = gql`
} }
} }
`; `;
export const useTokenVerifyMutation = makeMutation<
export const TypedVerifyTokenMutation = TypedMutation<
VerifyToken, VerifyToken,
VerifyTokenVariables VerifyTokenVariables
>(tokenVerifyMutation); >(tokenVerifyMutation);
const refreshToken = gql`
mutation RefreshToken($token: String!) {
tokenRefresh(csrfToken: $token) {
token
}
}
`;
export const useTokenRefreshMutation = makeMutation<
RefreshToken,
RefreshTokenVariables
>(refreshToken);
export const requestPasswordReset = gql` export const requestPasswordReset = gql`
${accountErrorFragment} ${accountErrorFragment}
mutation RequestPasswordReset($email: String!, $redirectUrl: String!) { mutation RequestPasswordReset($email: String!, $redirectUrl: String!) {
@ -84,15 +94,3 @@ export const SetPasswordMutation = TypedMutation<
SetPassword, SetPassword,
SetPasswordVariables SetPasswordVariables
>(setPassword); >(setPassword);
const refreshToken = gql`
mutation RefreshToken($token: String!) {
tokenRefresh(csrfToken: $token) {
token
}
}
`;
export const TokenRefreshMutation = TypedMutation<
RefreshToken,
RefreshTokenVariables
>(refreshToken);

View file

@ -9,7 +9,7 @@ import { PermissionEnum } from "./../../types/globalTypes";
// ==================================================== // ====================================================
export interface TokenAuth_tokenCreate_errors { export interface TokenAuth_tokenCreate_errors {
__typename: "Error"; __typename: "AccountError";
field: string | null; field: string | null;
message: string | null; message: string | null;
} }