Skip to content

Commit 5f4e0a5

Browse files
VIA-630 Fix updating token and session with age details from login profile
1 parent dc05c4b commit 5f4e0a5

File tree

6 files changed

+30
-31
lines changed

6 files changed

+30
-31
lines changed

src/utils/auth/callbacks/get-token.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe("getToken", () => {
5050

5151
const profile: Profile = {
5252
nhs_number: "test_nhs_number",
53+
birthdate: "1994-08-04",
5354
};
5455

5556
const account = {
@@ -101,15 +102,14 @@ describe("getToken", () => {
101102
expectResultToMatchTokenWith(
102103
result,
103104
profile.nhs_number,
104-
"",
105+
profile.birthdate,
105106
"newIdToken",
106107
"new-apim-access-token",
107108
nowInSeconds + 1111,
108109
maxAgeInSeconds,
109110
);
110111
});
111112

112-
// todo: check the apim assertion still holds
113113
it("should return token with empty values on initial login if account and profile are undefined", async () => {
114114
const undefinedToken = {} as JWT;
115115
const undefinedAccount = {} as Account;
@@ -136,6 +136,7 @@ describe("getToken", () => {
136136
expect(result).toMatchObject({
137137
user: {
138138
nhs_number: "",
139+
birthdate: "",
139140
},
140141
nhs_login: {
141142
id_token: "",
@@ -167,7 +168,7 @@ describe("getToken", () => {
167168

168169
const result = await getToken(token, account, profile, maxAgeInSeconds);
169170

170-
expectResultToMatchTokenWith(result, profile.nhs_number, "","newIdToken", "", 0, maxAgeInSeconds);
171+
expectResultToMatchTokenWith(result, profile.nhs_number, profile.birthdate, "newIdToken", "", 0, maxAgeInSeconds);
171172
});
172173
});
173174

@@ -183,14 +184,14 @@ describe("getToken", () => {
183184

184185
const result = await getToken(token, account, profile, maxAgeInSeconds);
185186

186-
expectResultToMatchTokenWith(result, profile.nhs_number, "", "newIdToken", "", 0, maxAgeInSeconds);
187+
expectResultToMatchTokenWith(result, profile.nhs_number, profile.birthdate, "newIdToken", "", 0, maxAgeInSeconds);
187188
});
188189
});
189190

190191
const expectResultToMatchTokenWith = (
191192
result: JWT | null,
192193
nhsNumber: string,
193-
birthdate: string,
194+
birthdate: string | null | undefined,
194195
idToken: string,
195196
apimToken: string,
196197
apimExpiresAt: number,

src/utils/auth/callbacks/get-token.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,6 @@ import { JWT } from "next-auth/jwt";
1010
import { headers } from "next/headers";
1111
import { Logger } from "pino";
1212

13-
14-
15-
16-
17-
18-
19-
20-
21-
22-
23-
24-
25-
26-
27-
28-
2913
const log: Logger = logger.child({ module: "utils-auth-callbacks-get-token" });
3014

3115
/* from Next Auth documentation:
@@ -102,11 +86,11 @@ const fillMissingFieldsInTokenWithDefaultValues = (token: JWT, apimAccessCredent
10286
return {
10387
...token,
10488
user: {
105-
nhs_number: token.user?.nhs_number ?? "" as NhsNumber,
106-
birthdate: token.user?.birthdate ?? "" as BirthDate,
89+
nhs_number: token.user?.nhs_number ?? ("" as NhsNumber),
90+
birthdate: token.user?.birthdate ?? ("" as BirthDate),
10791
},
10892
nhs_login: {
109-
id_token: token.nhs_login?.id_token ?? "" as IdToken,
93+
id_token: token.nhs_login?.id_token ?? ("" as IdToken),
11094
},
11195
apim: {
11296
access_token: (apimAccessCredentials ? apimAccessCredentials.accessToken : token.apim?.access_token) ?? "",
@@ -130,7 +114,7 @@ const updateTokenWithValuesFromAccountAndProfile = (
130114
...token,
131115
user: {
132116
nhs_number: (profile.nhs_number ?? "") as NhsNumber,
133-
birthdate: token.user?.birthdate ?? "" as BirthDate,
117+
birthdate: (profile.birthdate ?? "") as BirthDate,
134118
},
135119
nhs_login: {
136120
id_token: (account.id_token ?? "") as IdToken,

src/utils/auth/callbacks/get-updated-session.test.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import { NhsNumber } from "@src/models/vaccine";
22
import { getUpdatedSession } from "@src/utils/auth/callbacks/get-updated-session";
3-
import { AccessToken, Age, ExpiresAt, IdToken } from "@src/utils/auth/types";
3+
import { AccessToken, Age, BirthDate, ExpiresAt, IdToken } from "@src/utils/auth/types";
4+
import { calculateAge } from "@src/utils/date";
45
import { Session } from "next-auth";
56
import { JWT } from "next-auth/jwt";
67

78
jest.mock("sanitize-data", () => ({ sanitize: jest.fn() }));
9+
jest.mock("@src/utils/date", () => ({ calculateAge: jest.fn() }));
10+
11+
const mockBirthdate = "1995-01-01";
12+
const mockCalculatedAge = 30;
813

914
describe("getSession", () => {
15+
beforeEach(() => {
16+
(calculateAge as jest.Mock).mockImplementation(() => mockCalculatedAge);
17+
});
18+
1019
it("updates session user fields from token when both defined", () => {
1120
const session: Session = {
1221
user: {
@@ -19,7 +28,7 @@ describe("getSession", () => {
1928
const token = {
2029
user: {
2130
nhs_number: "test-nhs-number" as NhsNumber,
22-
birthdate: "1995-01-01",
31+
birthdate: mockBirthdate as BirthDate,
2332
},
2433
nhs_login: {
2534
id_token: "test-id-token" as IdToken,
@@ -33,7 +42,7 @@ describe("getSession", () => {
3342
const result: Session = getUpdatedSession(session, token);
3443

3544
expect(result.user.nhs_number).toBe("test-nhs-number");
36-
expect(result.user.age).toBe(30);
45+
expect(result.user.age).toBe(mockCalculatedAge);
3746
});
3847

3948
it("does not update session if token.user is missing", () => {
@@ -61,6 +70,7 @@ describe("getSession", () => {
6170
const token = {
6271
user: {
6372
nhs_number: "test-nhs-number",
73+
birthdate: mockBirthdate,
6474
},
6575
} as JWT;
6676

src/utils/auth/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ declare module "next-auth" {
5151

5252
interface Profile {
5353
nhs_number: string;
54+
birthdate: string;
5455
}
5556
}
5657

src/utils/date.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Age } from "@src/utils/auth/types";
22
import { differenceInYears } from "date-fns";
33
import { z } from "zod";
44

5-
export const UtcDateFromStringSchema = z
5+
const UtcDateFromStringSchema = z
66
.string()
77
.regex(/^\d{4}-\d{2}-\d{2}$/, "Date must be in YYYY-MM-DD format")
88
.transform((str, ctx) => {
@@ -18,7 +18,7 @@ export const UtcDateFromStringSchema = z
1818
return date;
1919
});
2020

21-
export const UtcDateTimeFromStringSchema = z.iso
21+
const UtcDateTimeFromStringSchema = z.iso
2222
.datetime({ offset: true, message: "Invalid ISO 8601 datetime format" })
2323
.transform((str, ctx) => {
2424
const date = new Date(str);
@@ -31,9 +31,11 @@ export const UtcDateTimeFromStringSchema = z.iso
3131
return date;
3232
});
3333

34-
export const calculateAge = (date: string): Age => {
34+
const calculateAge = (date: string): Age => {
3535
const today: Date = new Date();
3636
const birthDate: Date = UtcDateFromStringSchema.parse(date);
3737

3838
return differenceInYears(today, birthDate) as Age;
3939
};
40+
41+
export { UtcDateFromStringSchema, UtcDateTimeFromStringSchema, calculateAge };

src/utils/logger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const REDACT_KEYS: string[] = [
1313
"ELIGIBILITY_API_KEY",
1414
"accessToken",
1515
"access_token",
16+
"age",
1617
"apiKey",
1718
"apikey",
1819
"api_key",

0 commit comments

Comments
 (0)