Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[PWA-147] Sign Out should revoke customer token using graphql mutation #2012

Merged
merged 6 commits into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import actions from '../actions';
import { getUserDetails, resetPassword } from '../asyncActions';
import { getUserDetails, resetPassword, signOut } from '../asyncActions';

jest.mock('../../../../RestApi');
jest.mock('../../../../util/simplePersistence');
jest.mock('../../../../util/router-helpers', () => ({
refresh: jest.fn()
}));

const dispatch = jest.fn();
const getState = jest.fn(() => ({
Expand All @@ -12,6 +15,7 @@ const thunkArgs = [dispatch, getState];
const fetchUserDetails = jest
.fn()
.mockResolvedValue({ data: { customer: {} } });
const revokeToken = jest.fn().mockResolvedValue({});

describe('getUserDetails', () => {
test('it returns a thunk', () => {
Expand Down Expand Up @@ -94,3 +98,18 @@ describe('resetPassword', () => {
);
});
});

describe('signOut', () => {
const history = {};

test('signOut returns a thunk', () => {
expect(signOut({ history, revokeToken })).toBeInstanceOf(Function);
});

test('signOut thunk invokes revokeToken', async () => {
await signOut({ history, revokeToken })(dispatch);

expect(revokeToken).toHaveBeenCalled();
expect(revokeToken).toHaveReturned();
});
});
7 changes: 5 additions & 2 deletions packages/peregrine/lib/store/actions/user/asyncActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ export const signIn = credentials =>
}
};

export const signOut = ({ history }) => async dispatch => {
// Sign the user out in local storage and Redux.
export const signOut = ({ history, revokeToken }) => async dispatch => {
// Send mutation to revoke token.
await revokeToken();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this will return an error if called without a bearer token. Do we care? Probably not, but I would possibly catch and do something with the error just incase.

{
  "errors": [
    {
      "message": "The current customer isn't authorized.",
      "category": "graphql-authorization",
      "locations": [
        {
          "line": 2,
          "column": 5
        }
      ],
      "path": [
        "revokeCustomerToken"
      ]
    }
  ],
  "data": {
    "revokeCustomerToken": null
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1f680c4


// Remove token from local storage and Redux.
await dispatch(clearToken());
await dispatch(actions.reset());
await clearCheckoutDataFromStorage();
Expand Down
11 changes: 8 additions & 3 deletions packages/peregrine/lib/talons/AuthModal/useAuthModal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useCallback, useEffect, useState } from 'react';
import { useHistory } from 'react-router-dom';

import { useUserContext } from '../../context/user';
import { useMutation } from '@apollo/react-hooks';

const UNAUTHED_ONLY = ['CREATE_ACCOUNT', 'FORGOT_PASSWORD', 'SIGN_IN'];

Expand Down Expand Up @@ -30,11 +33,14 @@ export const useAuthModal = props => {
showForgotPassword,
showMainMenu,
showMyAccount,
signOutMutation,
view
} = props;

const [username, setUsername] = useState('');
const [{ currentUser }, { signOut }] = useUserContext();
const [revokeToken] = useMutation(signOutMutation);
const history = useHistory();

// If the user is authed, the only valid view is "MY_ACCOUNT".
// view an also be `MENU` but in that case we don't want to act.
Expand All @@ -54,9 +60,8 @@ export const useAuthModal = props => {
}, [showMyAccount]);

const handleSignOut = useCallback(() => {
// TODO: Get history from router context when implemented.
signOut({ history: window.history });
}, [signOut]);
signOut({ history, revokeToken });
}, [history, revokeToken, signOut]);

return {
handleClose,
Expand Down
6 changes: 5 additions & 1 deletion packages/venia-ui/lib/components/AuthModal/authModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import MyAccount from '../MyAccount';
import SignIn from '../SignIn';
import defaultClasses from './authModal.css';
import { useAuthModal } from '@magento/peregrine/lib/talons/AuthModal/useAuthModal';
import SIGN_OUT_MUTATION from '../../queries/signOut.graphql';

const AuthModal = props => {
const {
Expand All @@ -19,7 +20,10 @@ const AuthModal = props => {
showForgotPassword,
showMyAccount,
username
} = useAuthModal(props);
} = useAuthModal({
...props,
signOutMutation: SIGN_OUT_MUTATION
});

let child = null;
switch (props.view) {
Expand Down
5 changes: 5 additions & 0 deletions packages/venia-ui/lib/queries/signOut.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mutation signOut {
revokeCustomerToken {
result
}
}