Skip to content

Commit d34ee6e

Browse files
committed
feat: add error when no sub in idtoken and getting user
1 parent 4ccd0bb commit d34ee6e

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

lib/utils/token/getUserProfile.test.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it, beforeEach } from "vitest";
1+
import { describe, expect, it, beforeEach, vi } from "vitest";
22
import { MemoryStorage, StorageKeys } from "../../sessionManager";
33
import { getUserProfile, setActiveStorage } from ".";
44
import { createMockAccessToken } from "./testUtils";
@@ -53,4 +53,32 @@ describe("getUserProfile", () => {
5353
picture: "https://kinde.com/",
5454
});
5555
});
56+
57+
it("when no sub, return null", async () => {
58+
await storage.setSessionItem(
59+
StorageKeys.idToken,
60+
createMockAccessToken({
61+
sub: null,
62+
}),
63+
);
64+
const idToken = await getUserProfile();
65+
66+
expect(idToken).toStrictEqual(null);
67+
});
68+
69+
it("when no sub, return null", async () => {
70+
const consoleMock = vi
71+
.spyOn(console, "error")
72+
.mockImplementation(() => undefined);
73+
74+
await storage.setSessionItem(
75+
StorageKeys.idToken,
76+
createMockAccessToken({
77+
sub: null,
78+
}),
79+
);
80+
await getUserProfile();
81+
82+
expect(consoleMock).toHaveBeenCalledWith("No sub in idToken");
83+
});
5684
});

lib/utils/token/getUserProfile.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { getDecodedToken } from ".";
22

33
export type UserProfile = {
44
id: string;
5-
givenName: string;
6-
familyName: string;
7-
email: string;
8-
picture: string;
5+
givenName?: string;
6+
familyName?: string;
7+
email?: string;
8+
picture?: string;
99
};
1010

1111
export const getUserProfile = async <T>(): Promise<
@@ -21,6 +21,11 @@ export const getUserProfile = async <T>(): Promise<
2121
if (!idToken) {
2222
return null;
2323
}
24+
const { sub } = idToken;
25+
if (!sub) {
26+
console.error("No sub in idToken");
27+
return null;
28+
}
2429
return {
2530
id: idToken.sub,
2631
givenName: idToken.given_name,

0 commit comments

Comments
 (0)