Skip to content

feat(signUp_verification_status): Sign up email/phone verification status fix #386

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/services/tokenGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class JwtTokenGenerator implements TokenGenerator {
auth_time: authTime,
email: attributeValue("email", user.Attributes),
email_verified: Boolean(
attributeValue("email_verified", user.Attributes) ?? false
"true" == attributeValue("email_verified", user.Attributes) ?? false
),
event_id: eventId,
iat: authTime,
Expand Down
50 changes: 48 additions & 2 deletions src/targets/signUp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("SignUp target", () => {
).rejects.toBeInstanceOf(UsernameExistsError);
});

it("saves a new user", async () => {
it("saves a new user with email", async () => {
mockUserPoolService.getUserByUsername.mockResolvedValue(null);

await signUp(TestContext, {
Expand All @@ -74,6 +74,41 @@ describe("SignUp target", () => {
Value: expect.stringMatching(UUID),
},
{ Name: "email", Value: "[email protected]" },
{ Name: "email_verified", Value: "false" },
],
Enabled: true,
Password: "pwd",
UserCreateDate: now,
UserLastModifiedDate: now,
UserStatus: "UNCONFIRMED",
Username: "user-supplied",
RefreshTokens: [],
});
});

it("saves a new user with email and phone_number", async () => {
mockUserPoolService.getUserByUsername.mockResolvedValue(null);

await signUp(TestContext, {
ClientId: "clientId",
Password: "pwd",
Username: "user-supplied",
UserAttributes: [
{ Name: "email", Value: "[email protected]" },
{ Name: "phone_number", Value: "0400000000" },
],
});

expect(mockUserPoolService.saveUser).toHaveBeenCalledWith(TestContext, {
Attributes: [
{
Name: "sub",
Value: expect.stringMatching(UUID),
},
{ Name: "email", Value: "[email protected]" },
{ Name: "phone_number", Value: "0400000000" },
{ Name: "email_verified", Value: "false" },
{ Name: "phone_number_verified", Value: "false" },
],
Enabled: true,
Password: "pwd",
Expand Down Expand Up @@ -120,6 +155,7 @@ describe("SignUp target", () => {
userAttributes: [
{ Name: "sub", Value: expect.stringMatching(UUID) },
{ Name: "email", Value: "[email protected]" },
{ Name: "email_verified", Value: "false" },
],
userPoolId: "test",
username: "user-supplied",
Expand Down Expand Up @@ -206,6 +242,7 @@ describe("SignUp target", () => {
userAttributes: [
{ Name: "sub", Value: expect.stringMatching(UUID) },
{ Name: "email", Value: "[email protected]" },
{ Name: "email_verified", Value: "false" },
{ Name: "cognito:user_status", Value: "CONFIRMED" },
],
userPoolId: "test",
Expand All @@ -229,7 +266,10 @@ describe("SignUp target", () => {
},
Password: "pwd",
Username: "user-supplied",
UserAttributes: [{ Name: "email", Value: "[email protected]" }],
UserAttributes: [
{ Name: "email", Value: "[email protected]" },
{ Name: "email_verified", Value: "false" },
],
ValidationData: [{ Name: "another", Value: "attribute" }],
})
).rejects.toBeInstanceOf(UserLambdaValidationError);
Expand Down Expand Up @@ -466,6 +506,7 @@ describe("SignUp target", () => {
Attributes: [
{ Name: "sub", Value: expect.stringMatching(UUID) },
{ Name: "email", Value: "[email protected]" },
{ Name: "email_verified", Value: "false" },
],
Enabled: true,
Password: "pwd",
Expand Down Expand Up @@ -535,6 +576,7 @@ describe("SignUp target", () => {
Attributes: [
{ Name: "sub", Value: expect.stringMatching(UUID) },
{ Name: "phone_number", Value: "0400000000" },
{ Name: "phone_number_verified", Value: "false" },
],
Enabled: true,
Password: "pwd",
Expand Down Expand Up @@ -611,6 +653,8 @@ describe("SignUp target", () => {
{ Name: "sub", Value: expect.stringMatching(UUID) },
{ Name: "email", Value: "[email protected]" },
{ Name: "phone_number", Value: "0400000000" },
{ Name: "email_verified", Value: "false" },
{ Name: "phone_number_verified", Value: "false" },
],
Enabled: true,
Password: "pwd",
Expand Down Expand Up @@ -657,6 +701,7 @@ describe("SignUp target", () => {
Attributes: [
{ Name: "sub", Value: expect.stringMatching(UUID) },
{ Name: "email", Value: "[email protected]" },
{ Name: "email_verified", Value: "false" },
],
Enabled: true,
Password: "pwd",
Expand Down Expand Up @@ -719,6 +764,7 @@ describe("SignUp target", () => {
Attributes: [
{ Name: "sub", Value: expect.stringMatching(UUID) },
{ Name: "email", Value: "[email protected]" },
{ Name: "email_verified", Value: "false" },
],
ConfirmationCode: "123456",
Enabled: true,
Expand Down
30 changes: 23 additions & 7 deletions src/targets/signUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export const SignUp =
: [{ Name: "sub", Value: uuid.v4() }, ...(req.UserAttributes ?? [])];
let userStatus: UserStatusType = "UNCONFIRMED";

const isEmailUsername =
config.UserPoolDefaults.UsernameAttributes?.includes("email");
const hasEmailAttribute = attributesInclude("email", attributes);

if (triggers.enabled("PreSignUp")) {
const { autoConfirmUser, autoVerifyEmail, autoVerifyPhone } =
await triggers.preSignUp(ctx, {
Expand All @@ -102,18 +106,30 @@ export const SignUp =
if (autoConfirmUser) {
userStatus = "CONFIRMED";
}
const isEmailUsername =
config.UserPoolDefaults.UsernameAttributes?.includes("email");
const hasEmailAttribute = attributesInclude("email", attributes);

if (isEmailUsername && !hasEmailAttribute) {
attributes.push({ Name: "email", Value: req.Username });
}
if ((isEmailUsername || hasEmailAttribute) && autoVerifyEmail) {
attributes.push({ Name: "email_verified", Value: "true" });
if (isEmailUsername || hasEmailAttribute) {
if (autoVerifyEmail) {
attributes.push({ Name: "email_verified", Value: "true" });
} else {
attributes.push({ Name: "email_verified", Value: "false" });
}
}
if (attributesInclude("phone_number", attributes)) {
if (autoVerifyPhone) {
attributes.push({ Name: "phone_number_verified", Value: "true" });
} else {
attributes.push({ Name: "phone_number_verified", Value: "false" });
}
}
} else {
if (isEmailUsername || hasEmailAttribute) {
attributes.push({ Name: "email_verified", Value: "false" });
}
if (attributesInclude("phone_number", attributes) && autoVerifyPhone) {
attributes.push({ Name: "phone_number_verified", Value: "true" });
if (attributesInclude("phone_number", attributes)) {
attributes.push({ Name: "phone_number_verified", Value: "false" });
}
}

Expand Down