Skip to content

Commit

Permalink
feat(@aws-amplify/auth): Auto sign in after sign up (aws-amplify#10126)
Browse files Browse the repository at this point in the history
Fixes: aws-amplify#6320 aws-amplify#3882 aws-amplify#3631 aws-amplify#6018

Co-authored-by: Balashova <helgabalashova>
Co-authored-by: Francisco Rodriguez <frodriguez.cs@gmail.com>
  • Loading branch information
helgabalashova and elorzafe authored Jul 28, 2022
1 parent 366c32e commit e54617f
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,13 @@ jobs:
sample_name: with-authenticator
spec: ui-amplify-authenticator
browser: << parameters.browser >>
- integ_test_js:
test_name: 'Sign In after Sign Up'
framework: react
category: auth
sample_name: auto-signin-after-signup
spec: auto-signin-after-signup
browser: << parameters.browser >>
integ_angular_auth:
parameters:
browser:
Expand Down
133 changes: 133 additions & 0 deletions packages/auth/__tests__/auth-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CognitoIdToken,
CognitoAccessToken,
NodeCallback,
ISignUpResult,
} from 'amazon-cognito-identity-js';

const MAX_DEVICES: number = 60;
Expand Down Expand Up @@ -319,6 +320,14 @@ const authOptionsWithHostedUIConfig: AuthOptions = {
responseType: 'code',
},
};
const authOptionConfirmationLink: AuthOptions = {
userPoolId: 'awsUserPoolsId',
userPoolWebClientId: 'awsUserPoolsWebClientId',
region: 'region',
identityPoolId: 'awsCognitoIdentityPoolId',
mandatorySignIn: false,
signUpVerificationMethod: 'link',
};

const authOptionsWithClientMetadata: AuthOptions = {
userPoolId: 'awsUserPoolsId',
Expand All @@ -343,6 +352,13 @@ const userPool = new CognitoUserPool({
ClientId: authOptions.userPoolWebClientId,
});

const signUpResult: ISignUpResult = {
user: null,
userConfirmed: true,
userSub: 'userSub',
codeDeliveryDetails: null,
};

const idToken = new CognitoIdToken({ IdToken: 'idToken' });
const accessToken = new CognitoAccessToken({ AccessToken: 'accessToken' });

Expand Down Expand Up @@ -558,6 +574,122 @@ describe('auth unit test', () => {
});
});

describe('autoSignInAfterSignUp', () => {
test('happy case auto confirm', async () => {
const spyon = jest
.spyOn(CognitoUserPool.prototype, 'signUp')
.mockImplementationOnce(
(
username,
password,
signUpAttributeList,
validationData,
callback,
clientMetadata
) => {
callback(null, signUpResult);
}
);
const signInSpyon = jest.spyOn(CognitoUser.prototype, 'authenticateUser');
const auth = new Auth(authOptions);
const attrs = {
username: 'username',
password: 'password',
attributes: {
email: 'email',
phone_number: 'phone_number',
otherAttrs: 'otherAttrs',
},
autoSignIn: { enabled: true },
};
expect(await auth.signUp(attrs)).toBe(signUpResult);
expect(signInSpyon).toHaveBeenCalledTimes(1);
spyon.mockClear();
signInSpyon.mockClear();
});

test('happy case confirmation code', async () => {
const spyon = jest.spyOn(CognitoUserPool.prototype, 'signUp');
const confirmSpyon = jest.spyOn(
CognitoUser.prototype,
'confirmRegistration'
);
const signInSpyon = jest.spyOn(CognitoUser.prototype, 'authenticateUser');
const auth = new Auth(authOptions);
const attrs = {
username: 'username',
password: 'password',
attributes: {
email: 'email',
phone_number: 'phone_number',
otherAttrs: 'otherAttrs',
},
autoSignIn: { enabled: true },
};
expect(await auth.signUp(attrs)).toBe('signUpResult');
expect(await auth.confirmSignUp('username', 'code')).toBe('Success');
expect(signInSpyon).toHaveBeenCalledTimes(1);
spyon.mockClear();
confirmSpyon.mockClear();
signInSpyon.mockClear();
});

test('happy case confirmation link', async () => {
jest.useFakeTimers();
const spyon = jest.spyOn(CognitoUserPool.prototype, 'signUp');
const signInSpyon = jest.spyOn(CognitoUser.prototype, 'authenticateUser');
const auth = new Auth(authOptionConfirmationLink);
const attrs = {
username: 'username',
password: 'password',
attributes: {
email: 'email',
phone_number: 'phone_number',
otherAttrs: 'otherAttrs',
},
autoSignIn: { enabled: true },
};
expect(await auth.signUp(attrs)).toBe('signUpResult');
jest.advanceTimersByTime(11000);
expect(signInSpyon).toHaveBeenCalledTimes(2);
spyon.mockClear();
signInSpyon.mockClear();
});

test('fail confirmation code', async () => {
const spyon = jest.spyOn(CognitoUserPool.prototype, 'signUp');
const confirmSpyon = jest
.spyOn(CognitoUser.prototype, 'confirmRegistration')
.mockImplementationOnce(
(confirmationCode, forceAliasCreation, callback) => {
callback('err', null);
}
);
const signInSpyon = jest.spyOn(CognitoUser.prototype, 'authenticateUser');
const auth = new Auth(authOptions);
const attrs = {
username: 'username',
password: 'password',
attributes: {
email: 'email',
phone_number: 'phone_number',
otherAttrs: 'otherAttrs',
},
autoSignIn: { enabled: true },
};
expect(await auth.signUp(attrs)).toBe('signUpResult');
try {
await auth.confirmSignUp('username', 'code');
} catch (e) {
expect(e).toBe('err');
}
expect(signInSpyon).toHaveBeenCalledTimes(0);
spyon.mockClear();
confirmSpyon.mockClear();
signInSpyon.mockClear();
});
});

describe('confirmSignUp', () => {
test('happy case', async () => {
const spyon = jest.spyOn(CognitoUser.prototype, 'confirmRegistration');
Expand Down Expand Up @@ -1517,6 +1649,7 @@ describe('auth unit test', () => {
describe('currentSession', () => {
afterEach(() => {
jest.clearAllMocks();
jest.useRealTimers();
});
test('happy case', async () => {
const auth = new Auth(authOptions);
Expand Down
Loading

0 comments on commit e54617f

Please sign in to comment.