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

#8205 Makes conversion of 'true' string to boolean for user attributes case insensitive #8206

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
42 changes: 41 additions & 1 deletion packages/auth/__tests__/auth-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3089,7 +3089,7 @@ describe('auth unit test', () => {
spyon.mockClear();
});

test('happy case with unverified', async () => {
test('happy case with verified', async () => {
const spyon = jest
.spyOn(Auth.prototype, 'userAttributes')
.mockImplementationOnce(() => {
Expand Down Expand Up @@ -3128,6 +3128,46 @@ describe('auth unit test', () => {

spyon.mockClear();
});

test('happy case with verified as strings', async () => {
const spyon = jest
.spyOn(Auth.prototype, 'userAttributes')
.mockImplementationOnce(() => {
return new Promise((res: any, rej) => {
res([
{
Name: 'email',
Value: '[email protected]',
},
{
Name: 'phone_number',
Value: '+12345678901',
},
{
Name: 'email_verified',
Value: 'true',
},
{
Name: 'phone_number_verified',
Value: 'True',
},
]);
});
});

const auth = new Auth(authOptions);
const user = new CognitoUser({
Username: 'username',
Pool: userPool,
});

expect(await auth.verifiedContact(user)).toEqual({
unverified: {},
verified: { email: '[email protected]', phone_number: '+12345678901' },
});

spyon.mockClear();
});
});

describe('currentUserPoolUser test', () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/auth/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2137,8 +2137,7 @@ export class AuthClass {
attribute.Name === 'email_verified' ||
attribute.Name === 'phone_number_verified'
) {
obj[attribute.Name] =
attribute.Value === 'true' || attribute.Value === true;
obj[attribute.Name] = this.isTruthyString(attribute.Value) || attribute.Value === true;
} else {
obj[attribute.Name] = attribute.Value;
}
Expand All @@ -2147,6 +2146,10 @@ export class AuthClass {
return obj;
}

private isTruthyString(value: any): boolean {
return typeof value.toLowerCase === 'function' && value.toLowerCase() === 'true';
}

private createCognitoUser(username: string): CognitoUser {
const userData: ICognitoUserData = {
Username: username,
Expand Down