Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

PWA-3236: Change user time to logout #4260

Merged
merged 5 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions packages/peregrine/lib/store/actions/user/asyncActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ export const resetPassword = ({ email }) =>
dispatch(actions.resetPassword.receive());
};

export const setToken = token =>
export const setToken = (token, customer_token_lifetime = 3600) =>
async function thunk(...args) {
const [dispatch] = args;

// Store token in local storage.
// TODO: Get correct token expire time from API
storage.setItem('signin_token', token, 3600);
storage.setItem('signin_token', token, customer_token_lifetime);

// Persist in store
dispatch(actions.setToken(token));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,19 @@ const signInVariables = {
password: '123456'
};
const authToken = 'auth-token-123';
const customerTokenLifetime = 3600;
const signInMock = {
request: {
query: defaultOperations.signInMutation,
variables: signInVariables
},
result: {
data: { generateCustomerToken: { token: authToken } }
data: {
generateCustomerToken: {
token: authToken,
customer_token_lifetime: customerTokenLifetime
}
}
}
};

Expand Down Expand Up @@ -202,7 +208,7 @@ describe('handle submit event', () => {
await result.current.handleSubmit(formValues);
});

expect(mockSetToken).toHaveBeenCalledWith('auth-token-123');
expect(mockSetToken).toHaveBeenCalledWith('auth-token-123', 3600);
expect(createAccount).toHaveBeenCalled();
expect(mockRemoveCart).toHaveBeenCalled();
expect(mockCreateCart).toHaveBeenCalledWith({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const SIGN_IN = gql`
mutation SignInAfterCheckout($email: String!, $password: String!) {
generateCustomerToken(email: $email, password: $password) {
token
customer_token_lifetime
}
}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ export const useCreateAccount = props => {
...recaptchaDataForSignIn
});
const token = signInResponse.data.generateCustomerToken.token;
await setToken(token);
const customerTokenLifetime =
signInResponse.data.generateCustomerToken
.customer_token_lifetime;

await (customerTokenLifetime
? setToken(token, customerTokenLifetime)
: setToken(token));

// Clear guest cart from redux.
await removeCart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ const signInMutationFn = jest.fn().mockReturnValue([
jest.fn().mockReturnValue({
data: {
generateCustomerToken: {
token: 'customer token'
token: 'customer token',
customer_token_lifetime: 3600
}
}
}),
Expand Down Expand Up @@ -272,10 +273,12 @@ describe('handleSubmit', () => {

test('should signin after account creation', async () => {
const token = 'customertoken';
const customer_token_lifetime = 3600;
const signIn = jest.fn().mockReturnValue({
data: {
generateCustomerToken: {
token
token,
customer_token_lifetime
}
}
});
Expand All @@ -298,7 +301,7 @@ describe('handleSubmit', () => {
password: defaultFormValues.password
}
});
expect(setToken).toHaveBeenCalledWith(token);
expect(setToken).toHaveBeenCalledWith(token, customer_token_lifetime);
});

test('should clear cart data from cache', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const SIGN_IN = gql`
mutation SignInAfterCreate($email: String!, $password: String!) {
generateCustomerToken(email: $email, password: $password) {
token
customer_token_lifetime
}
}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,12 @@ export const useCreateAccount = props => {
...recaptchaDataForSignIn
});
const token = signInResponse.data.generateCustomerToken.token;
await setToken(token);

const customerTokenLifetime =
signInResponse.data.generateCustomerToken
.customer_token_lifetime;
await (customerTokenLifetime
? setToken(token, customerTokenLifetime)
: setToken(token));
// Clear all cart/customer data from cache and redux.
await apolloClient.clearCacheData(apolloClient, 'cart');
await apolloClient.clearCacheData(apolloClient, 'customer');
Expand Down
10 changes: 8 additions & 2 deletions packages/peregrine/lib/talons/SignIn/__tests__/useSignIn.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,20 @@ const signInVariables = {
password: 'slurm is the best'
};
const authToken = 'auth-token-123';
const customerTokenLifetime = 3600;

const signInMock = {
request: {
query: defaultOperations.signInMutation,
variables: signInVariables
},
result: {
data: { generateCustomerToken: { token: authToken } }
data: {
generateCustomerToken: {
token: authToken,
customer_token_lifetime: customerTokenLifetime
}
}
}
};

Expand Down Expand Up @@ -156,7 +162,7 @@ test('handleSubmit triggers waterfall of operations and actions', async () => {
await act(() => result.current.handleSubmit(signInVariables));

expect(result.current.isBusy).toBe(true);
expect(setToken).toHaveBeenCalledWith(authToken);
expect(setToken).toHaveBeenCalledWith(authToken, customerTokenLifetime);
expect(getCartDetails).toHaveBeenCalled();
expect(getUserDetails).toHaveBeenCalled();

Expand Down
1 change: 1 addition & 0 deletions packages/peregrine/lib/talons/SignIn/signIn.gql.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const SIGN_IN = gql`
mutation SignIn($email: String!, $password: String!) {
generateCustomerToken(email: $email, password: $password) {
token
customer_token_lifetime
}
}
`;
Expand Down
8 changes: 7 additions & 1 deletion packages/peregrine/lib/talons/SignIn/useSignIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ export const useSignIn = props => {
});

const token = signInResponse.data.generateCustomerToken.token;
await setToken(token);
const customerTokenLifetime =
signInResponse.data.generateCustomerToken
.customer_token_lifetime;

await (customerTokenLifetime
? setToken(token, customerTokenLifetime)
: setToken(token));

// Clear all cart/customer data from cache and redux.
await apolloClient.clearCacheData(apolloClient, 'cart');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const thisDep = {
intercept
};

const WEBPACK_BUILD_TIMEOUT = 30000;
const WEBPACK_BUILD_TIMEOUT = 800000;

const mockComponent = name => `function ${name}(props) { return <div className={name}>{props.children}</div>;
`;
Expand Down