Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add wrappers for token mutations

Marcin Gębalac45be8feb4e0
diff --git a/saleor/static/dashboard-next/auth/AuthProvider.tsx b/saleor/static/dashboard-next/auth/AuthProvider.tsx
index 92475d970a..004de2dc55 100644
--- a/saleor/static/dashboard-next/auth/AuthProvider.tsx
+++ b/saleor/static/dashboard-next/auth/AuthProvider.tsx
@@ -1,32 +1,41 @@
import * as React from "react";
-import { MutationFn } from "../../../../node_modules/react-apollo";
-import {
- TokenAuthMutation,
- TokenAuthMutationVariables,
- UserFragment,
- VerifyTokenMutation,
- VerifyTokenMutationVariables
-} from "../gql-types";
+import { UserFragment } from "../gql-types";
import {
getAuthToken,
removeAuthToken,
setAuthToken,
UserContext
} from "./index";
-import {
- tokenAuthMutation,
- tokenVerifyMutation,
- TypedTokenAuthMutation,
- TypedVerifyTokenMutation
-} from "./mutations";
+
+import TokenAuthProvider from "./containers/TokenAuth";
+import TokenVerifyProvider from "./containers/TokenVerify";
+
+const AuthProviderOperations: React.StatelessComponent<any> = ({
+ children,
+ onError
+}) => {
+ return (
+ <TokenAuthProvider onError={onError}>
+ {tokenAuth => (
+ <TokenVerifyProvider onError={onError}>
+ {tokenVerify => (
+ <AuthProvider tokenAuth={tokenAuth} tokenVerify={tokenVerify}>
+ {({ isAuthenticated }) => {
+ return children({ isAuthenticated });
+ }}
+ </AuthProvider>
+ )}
+ </TokenVerifyProvider>
+ )}
+ </TokenAuthProvider>
+ );
+};
interface AuthProviderProps {
- authenticate: MutationFn<TokenAuthMutation, TokenAuthMutationVariables>;
- authResult: any;
children: any;
- verifyToken: MutationFn<VerifyTokenMutation, VerifyTokenMutationVariables>;
- verifyResult: any;
+ tokenAuth: any;
+ tokenVerify: any;
}
interface AuthProviderState {
@@ -42,32 +51,32 @@ class AuthProvider extends React.Component<
this.state = { user: undefined };
}
- componentWillReceiveProps(props) {
- if (props.authResult.called && !props.authResult.loading) {
- this.setState({ user: props.authResult.data.tokenCreate.user });
- setAuthToken(props.authResult.data.tokenCreate.token);
+ componentWillReceiveProps(props: AuthProviderProps) {
+ const { tokenAuth, tokenVerify } = props;
+ if (tokenAuth.error || tokenVerify.error) {
+ this.logout();
+ }
+ if (tokenAuth.data) {
+ this.setState({ user: tokenAuth.data.tokenCreate.user });
+ setAuthToken(tokenAuth.data.tokenCreate.token);
+ }
+ if (tokenVerify.data) {
+ this.setState({ user: tokenVerify.data.tokenVerify.user });
}
}
componentDidMount() {
const { user } = this.state;
+ const { tokenVerify } = this.props;
const token = getAuthToken();
if (!!token && !user) {
- this.props
- .verifyToken({ variables: { token } })
- .then(response => {
- if (response) {
- this.setState({ user: response.data.tokenVerify.user });
- }
- })
- .catch(error => {
- this.logout();
- });
+ tokenVerify.mutate({ variables: { token } });
}
}
login = (email: string, password: string) => {
- this.props.authenticate({ variables: { email, password } });
+ const { tokenAuth } = this.props;
+ tokenAuth.mutate({ variables: { email, password } });
};
logout = () => {
@@ -75,51 +84,20 @@ class AuthProvider extends React.Component<
removeAuthToken();
};
- setUser = (user: UserFragment) => {
- this.setState({ user });
- };
-
render() {
- const { authResult, verifyResult } = this.props;
+ const { children, tokenAuth, tokenVerify } = this.props;
const { user } = this.state;
const isAuthenticated = !!user;
- const loading = authResult.loading || verifyResult.loading;
+ const loading = tokenAuth.loading || tokenVerify.loading;
return (
<UserContext.Provider
value={{ user, login: this.login, logout: this.logout }}
>
- {loading ? (
- // FIXME: render loading state
- <div>Loading</div>
- ) : (
- this.props.children({ isAuthenticated, logout: this.logout })
- )}
+ {/* FIXME: render lodaing state properly */}
+ { loading ? <div>Loading...</div> : children({ isAuthenticated }) }
</UserContext.Provider>
);
}
}
-const AuthProviderOperations: React.StatelessComponent<any> = ({
- children
-}) => (
- <TypedTokenAuthMutation mutation={tokenAuthMutation}>
- {(authenticate, tokenAuthResult) => (
- <TypedVerifyTokenMutation mutation={tokenVerifyMutation}>
- {(verifyToken, verifyTokenResult) => (
- <AuthProvider
- authenticate={authenticate}
- authResult={tokenAuthResult}
- verifyToken={verifyToken}
- verifyResult={verifyTokenResult}
- >
- {({ isAuthenticated }) => {
- return children({ isAuthenticated });
- }}
- </AuthProvider>
- )}
- </TypedVerifyTokenMutation>
- )}
- </TypedTokenAuthMutation>
-);
-
export default AuthProviderOperations;
diff --git a/saleor/static/dashboard-next/auth/containers/TokenAuth.tsx b/saleor/static/dashboard-next/auth/containers/TokenAuth.tsx
new file mode 100644
index 0000000000..0e46047cde
--- /dev/null
+++ b/saleor/static/dashboard-next/auth/containers/TokenAuth.tsx
@@ -0,0 +1,34 @@
+import * as React from "react";
+
+import {
+ PartialMutationProviderProps,
+ PartialMutationProviderRenderProps
+} from "../..";
+import { TokenAuthMutation, TokenAuthMutationVariables } from "../../gql-types";
+import { tokenAuthMutation, TypedTokenAuthMutation } from "../mutations";
+
+interface TokenAuthProviderProps
+ extends PartialMutationProviderProps<TokenAuthMutation> {
+ children: PartialMutationProviderRenderProps<
+ TokenAuthMutation,
+ TokenAuthMutationVariables
+ >;
+}
+
+const TokenAuthProvider: React.StatelessComponent<TokenAuthProviderProps> = ({
+ children,
+ onError,
+ onSuccess
+}) => (
+ <TypedTokenAuthMutation
+ mutation={tokenAuthMutation}
+ onCompleted={onSuccess}
+ onError={onError}
+ >
+ {(mutate, { called, data, error, loading }) =>
+ children({ called, data, error, loading, mutate })
+ }
+ </TypedTokenAuthMutation>
+);
+
+export default TokenAuthProvider;
diff --git a/saleor/static/dashboard-next/auth/containers/TokenVerify.tsx b/saleor/static/dashboard-next/auth/containers/TokenVerify.tsx
new file mode 100644
index 0000000000..f81727b176
--- /dev/null
+++ b/saleor/static/dashboard-next/auth/containers/TokenVerify.tsx
@@ -0,0 +1,35 @@
+import * as React from "react";
+
+import {
+ PartialMutationProviderProps,
+ PartialMutationProviderRenderProps
+} from "../..";
+import {
+ VerifyTokenMutation,
+ VerifyTokenMutationVariables
+} from "../../gql-types";
+import { tokenVerifyMutation, TypedVerifyTokenMutation } from "../mutations";
+
+interface TokenVerifyProviderProps
+ extends PartialMutationProviderProps<VerifyTokenMutation> {
+ children: PartialMutationProviderRenderProps<
+ VerifyTokenMutation,
+ VerifyTokenMutationVariables
+ >;
+}
+
+const TokenVerifyProvider: React.StatelessComponent<
+ TokenVerifyProviderProps
+> = ({ children, onError, onSuccess }) => (
+ <TypedVerifyTokenMutation
+ mutation={tokenVerifyMutation}
+ onCompleted={onSuccess}
+ onError={onError}
+ >
+ {(mutate, { called, data, error, loading }) =>
+ children({ called, data, error, loading, mutate })
+ }
+ </TypedVerifyTokenMutation>
+);
+
+export default TokenVerifyProvider;
diff --git a/saleor/static/dashboard-next/auth/containers/index.ts b/saleor/static/dashboard-next/auth/containers/index.ts
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/static/dashboard-next/auth/index.tsx b/saleor/static/dashboard-next/auth/index.tsx
index 3168385ff5..49f79e789c 100644
--- a/saleor/static/dashboard-next/auth/index.tsx
+++ b/saleor/static/dashboard-next/auth/index.tsx
@@ -11,9 +11,9 @@ const Component: React.StatelessComponent<RouteComponentProps<any>> = ({
};
interface UserContext {
- user?: any;
login?: (username: string, password: string) => void;
logout?: () => void;
+ user?: any;
}
export const UserContext = React.createContext<UserContext>({});
diff --git a/saleor/static/dashboard-next/index.tsx b/saleor/static/dashboard-next/index.tsx
index 5309fc5e6c..856eaf8ad9 100644
--- a/saleor/static/dashboard-next/index.tsx
+++ b/saleor/static/dashboard-next/index.tsx
@@ -67,20 +67,20 @@ render(
<MessageManager>
<CssBaseline />
<AuthProvider>
- {({ isAuthenticated }) => (
- isAuthenticated ? (
- <AppRoot>
- <Switch>
- <Route path="/categories" component={CategorySection} />
- <Route path="/pages" component={PageSection} />
- <Route path="/products" component={ProductSection} />
- </Switch>
- </AppRoot>
- ) : (
- <Route component={Auth} />
- )
- )}
- </AuthProvider>
+ {({ isAuthenticated }) => (
+ isAuthenticated ? (
+ <AppRoot>
+ <Switch>
+ <Route path="/categories" component={CategorySection} />
+ <Route path="/pages" component={PageSection} />
+ <Route path="/products" component={ProductSection} />
+ </Switch>
+ </AppRoot>
+ ) : (
+ <Route component={Auth} />
+ )
+ )}
+ </AuthProvider>
</MessageManager>
</MuiThemeProvider>
</BrowserRouter>
@@ -126,6 +126,7 @@ export type PartialMutationProviderRenderProps<
TVariables extends {} = {}
> = (
props: {
+ called?: boolean;
data: TData;
loading: boolean;
error?: ApolloError;