Skip to content

Commit

Permalink
fix(api): users can be queried by their sub
Browse files Browse the repository at this point in the history
The whole username vs sub vs email vs phone number thing is confusing,
and in an earlier change when I allowed usernames to be supplied by the
client it broke the ability to lookup by the sub when you use an email.

This is made more confusing by how the userName parameter supplied to
various calls to Cognito can be either the sub or the username or one of
the other username attributes 🤷

This change allows you to _always_ be able to lookup a user by their sub
regardless of what username they've registered with.
  • Loading branch information
jagregory committed Oct 5, 2021
1 parent aa9350f commit ae555b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/services/userPoolClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ describe("User Pool Client", () => {
expect(user?.Username).toEqual("user-supplied");
});

it("returns existing user by their sub", async () => {
const user = await userPool.getUserByUsername("uuid-1234");

expect(user).not.toBeNull();
expect(user?.Username).toEqual("user-supplied");
});

if (find_by_email) {
it("returns existing user by their email", async () => {
const user = await userPool.getUserByUsername(
Expand Down
4 changes: 4 additions & 0 deletions src/services/userPoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ export class UserPoolClientService implements UserPoolClient {
const users = await this.dataStore.get<Record<string, User>>("Users", {});

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

if (
aliasEmailEnabled &&
attributesIncludeMatch("email", username, user.Attributes)
Expand Down

0 comments on commit ae555b8

Please sign in to comment.