Skip to content

Commit

Permalink
fix(userpool): force save sub as attribute, fix sub check logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jagregory committed Apr 12, 2020
1 parent 03c5712 commit e5ed247
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
19 changes: 14 additions & 5 deletions integration-tests/userPool.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createUserPool } from "../src/services/userPool";
import { createUserPool, UserPool } from "../src/services/userPool";
import fs from "fs";
import { promisify } from "util";

Expand Down Expand Up @@ -26,7 +26,7 @@ describe("User Pool", () => {
});

describe("saveUser", () => {
it("saves a user", async () => {
it("saves a user with their username as an additional attribute", async () => {
const now = new Date().getTime();
const dataStore = await createUserPool({ UsernameAttributes: [] }, path);

Expand All @@ -49,7 +49,10 @@ describe("User Pool", () => {
Username: "1",
Password: "hunter3",
UserStatus: "UNCONFIRMED",
Attributes: [{ Name: "email", Value: "[email protected]" }],
Attributes: [
{ Name: "sub", Value: "1" },
{ Name: "email", Value: "[email protected]" },
],
UserLastModifiedDate: now,
UserCreateDate: now,
Enabled: true,
Expand Down Expand Up @@ -83,7 +86,10 @@ describe("User Pool", () => {
Password: "hunter3",
UserStatus: "UNCONFIRMED",
ConfirmationCode: "1234",
Attributes: [{ Name: "email", Value: "[email protected]" }],
Attributes: [
{ Name: "sub", Value: "1" },
{ Name: "email", Value: "[email protected]" },
],
UserLastModifiedDate: now,
UserCreateDate: now,
Enabled: true,
Expand All @@ -110,7 +116,10 @@ describe("User Pool", () => {
Username: "1",
Password: "hunter3",
UserStatus: "CONFIRMED",
Attributes: [{ Name: "email", Value: "[email protected]" }],
Attributes: [
{ Name: "sub", Value: "1" },
{ Name: "email", Value: "[email protected]" },
],
UserLastModifiedDate: now,
UserCreateDate: now,
Enabled: true,
Expand Down
24 changes: 18 additions & 6 deletions src/services/userPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ export const createUserPool = async (

db.default({ Users: {}, Options: options });

const hasAttribute = (
const attributeEquals = (
attributeName: string,
attributeValue: string,
user: User
) =>
user.Attributes.filter(
!!user.Attributes.find(
(x) => x.Name === attributeName && x.Value === attributeValue
);
const hasAttribute = (attributeName: string, user: User) =>
!!user.Attributes.find((x) => x.Name === attributeName);

return {
async getUserByUsername(username) {
Expand All @@ -68,17 +70,17 @@ export const createUserPool = async (
const users = (await db.get("Users").value()) as { [key: string]: User };

for (const user of Object.values(users)) {
if (hasAttribute("sub", username, user)) {
if (attributeEquals("sub", username, user)) {
return user;
}

if (aliasEmailEnabled && hasAttribute("email", username, user)) {
if (aliasEmailEnabled && attributeEquals("email", username, user)) {
return user;
}

if (
aliasPhoneNumberEnabled &&
hasAttribute("phone_number", username, user)
attributeEquals("phone_number", username, user)
) {
return user;
}
Expand All @@ -90,7 +92,17 @@ export const createUserPool = async (
async saveUser(user) {
console.log("saveUser", user);

await db.get("Users").set(user.Username, user).save();
const attributes = hasAttribute("sub", user)
? user.Attributes
: [{ Name: "sub", Value: user.Username }, ...user.Attributes];

await db
.get("Users")
.set(user.Username, {
...user,
Attributes: attributes,
})
.save();
},
};
};

0 comments on commit e5ed247

Please sign in to comment.