Skip to content

Commit

Permalink
fix(auth): replace window history with current browser session's state (
Browse files Browse the repository at this point in the history
#12782)

* chore: replace history with current browser session's state

* fix lint issues

* fix unit test
  • Loading branch information
israx authored Jan 4, 2024
1 parent 848651f commit c393316
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 66 deletions.
126 changes: 64 additions & 62 deletions packages/auth/__tests__/auth-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ const createMockLocalStorage = () =>
removeItem(key: string) {
delete this._items[key];
},
} as unknown as Storage);
}) as unknown as Storage;

import { AuthOptions, SignUpParams, AwsCognitoOAuthOpts } from '../src/types';
import { AuthClass as Auth } from '../src/Auth';
Expand Down Expand Up @@ -1407,38 +1407,38 @@ describe('auth unit test', () => {
spyon.mockClear();
});

test('currentUserPoolUser fails but hub event still dispatches', async () => {
const auth = new Auth(authOptions);
const spyon = jest
.spyOn(CognitoUser.prototype, 'sendMFACode')
.mockImplementationOnce((code, callback) => {
callback.onSuccess(session);
});

const spyon2 = jest
.spyOn(auth, 'currentUserPoolUser')
.mockImplementationOnce(() => {
return Promise.reject('Could not get current user.');
});
const hubSpy = jest.spyOn(Hub, 'dispatch');
const user = new CognitoUser({
Username: 'username',
Pool: userPool,
});
const result = await auth.confirmSignIn(user, 'code', null);
expect(result).toEqual(user);
expect(hubSpy).toHaveBeenCalledWith(
'auth',
{
data: user,
event: 'signIn',
message: 'A user username has been signed in',
},
'Auth',
Symbol.for('amplify_default')
);
spyon.mockClear();
});
test('currentUserPoolUser fails but hub event still dispatches', async () => {
const auth = new Auth(authOptions);
const spyon = jest
.spyOn(CognitoUser.prototype, 'sendMFACode')
.mockImplementationOnce((code, callback) => {
callback.onSuccess(session);
});

const spyon2 = jest
.spyOn(auth, 'currentUserPoolUser')
.mockImplementationOnce(() => {
return Promise.reject('Could not get current user.');
});
const hubSpy = jest.spyOn(Hub, 'dispatch');
const user = new CognitoUser({
Username: 'username',
Pool: userPool,
});
const result = await auth.confirmSignIn(user, 'code', null);
expect(result).toEqual(user);
expect(hubSpy).toHaveBeenCalledWith(
'auth',
{
data: user,
event: 'signIn',
message: 'A user username has been signed in',
},
'Auth',
Symbol.for('amplify_default')
);
spyon.mockClear();
});

test('onFailure', async () => {
const spyon = jest
Expand Down Expand Up @@ -3058,12 +3058,13 @@ describe('auth unit test', () => {
spyon.mockClear();
});

test('error hub event', async (done) => {
test('error hub event', async done => {
expect.assertions(3);
const spyon = jest.spyOn(CognitoUser.prototype, 'updateAttributes')
const spyon = jest
.spyOn(CognitoUser.prototype, 'updateAttributes')
.mockImplementationOnce((attrs, callback: any) => {
callback(new Error('Error'), null, null);
});
});

const auth = new Auth(authOptions);

Expand Down Expand Up @@ -3097,19 +3098,20 @@ describe('auth unit test', () => {
spyon.mockClear();
});

test('happy case code delivery details hub event', async (done) => {
test('happy case code delivery details hub event', async done => {
expect.assertions(2);

const codeDeliverDetailsResult: any = {
'CodeDeliveryDetailsList': [
{
'AttributeName': 'email',
'DeliveryMedium': 'EMAIL',
'Destination': 'e***@e***'
}
]
CodeDeliveryDetailsList: [
{
AttributeName: 'email',
DeliveryMedium: 'EMAIL',
Destination: 'e***@e***',
},
],
};
const spyon = jest.spyOn(CognitoUser.prototype, 'updateAttributes')
const spyon = jest
.spyOn(CognitoUser.prototype, 'updateAttributes')
.mockImplementationOnce((attrs, callback: any) => {
callback(null, 'SUCCESS', codeDeliverDetailsResult);
});
Expand All @@ -3126,20 +3128,20 @@ describe('auth unit test', () => {
sub: 'sub',
};
const payloadData = {
'email': {
email: {
isUpdated: false,
codeDeliveryDetails: {
AttributeName: 'email',
DeliveryMedium: 'EMAIL',
Destination: 'e***@e***'
}
Destination: 'e***@e***',
},
},
'phone_number': {
isUpdated: true
phone_number: {
isUpdated: true,
},
sub: {
isUpdated: true,
},
'sub': {
isUpdated: true
}
};
const listenToHub = Hub.listen('auth', ({ payload }) => {
const { event } = payload;
Expand Down Expand Up @@ -3588,8 +3590,8 @@ describe('auth unit test', () => {

expect(handleAuthResponseSpy).toHaveBeenCalledWith(url);
expect(replaceStateSpy).toHaveBeenCalledWith(
{},
null,
window.history.state,
'',
(options.oauth as AwsCognitoOAuthOpts).redirectSignIn
);

Expand All @@ -3601,7 +3603,7 @@ describe('auth unit test', () => {
"Hub.dispatch('auth', { data: ..., event: 'parsingCallbackUrl' })",
],
Array [
"window.history.replaceState({}, null, 'http://localhost:3000/')",
"window.history.replaceState(null, "", 'http://localhost:3000/')",
],
Array [
"Hub.dispatch('auth', { data: ..., event: 'signIn' })",
Expand Down Expand Up @@ -3653,8 +3655,8 @@ describe('auth unit test', () => {

expect(handleAuthResponseSpy).toHaveBeenCalledWith(url);
expect(replaceStateSpy).toHaveBeenCalledWith(
{},
null,
window.history.state,
'',
(options.oauth as AwsCognitoOAuthOpts).redirectSignIn
);
});
Expand All @@ -3678,7 +3680,7 @@ describe('auth unit test', () => {
);
});

test('User Pools and Identity Pools', async () => {
test.only('User Pools and Identity Pools', async () => {
const options: AuthOptions = {
region: 'region',
userPoolId: 'userPoolId',
Expand Down Expand Up @@ -3717,8 +3719,8 @@ describe('auth unit test', () => {

expect(handleAuthResponseSpy).toHaveBeenCalledWith(url);
expect(replaceStateSpy).toHaveBeenCalledWith(
{},
null,
window.history.state,
'',
(options.oauth as AwsCognitoOAuthOpts).redirectSignIn
);
});
Expand Down
8 changes: 4 additions & 4 deletions packages/auth/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2552,8 +2552,8 @@ export class AuthClass {

if (window && typeof window.history !== 'undefined') {
window.history.replaceState(
{},
null,
window.history.state,
'',
(this._config.oauth as AwsCognitoOAuthOpts).redirectSignIn
);
}
Expand Down Expand Up @@ -2588,8 +2588,8 @@ export class AuthClass {
// Otherwise, reloading the page will throw errors as the `code` has already been spent.
if (window && typeof window.history !== 'undefined') {
window.history.replaceState(
{},
null,
window.history.state,
'',
(this._config.oauth as AwsCognitoOAuthOpts).redirectSignIn
);
}
Expand Down

0 comments on commit c393316

Please sign in to comment.