Skip to content

Commit a32afa2

Browse files
VIA-630 MD: bring the birthdate back in and update session with calculated age
1 parent a3b3c1c commit a32afa2

File tree

7 files changed

+45
-10
lines changed

7 files changed

+45
-10
lines changed

src/app/_components/inactivity/InactivityDialog.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import useInactivityTimer from "@src/utils/auth/inactivity-timer";
55
import { userLogout } from "@src/utils/auth/user-logout";
66
import { render, screen } from "@testing-library/react";
77
import { Session } from "next-auth";
8+
import { Age } from "@src/utils/auth/types";
89

910
const mockSessionValue: Partial<Session> = {
1011
expires: new Date(Date.now() + 60000).toISOString(),
1112
user: {
1213
nhs_number: "" as NhsNumber,
14+
age: 20 as Age,
1315
},
1416
};
1517
let mockSession = { data: mockSessionValue, status: "authenticated" };

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe("getToken", () => {
4848

4949
const nowInSeconds = 1749052001;
5050

51-
const profile = {
51+
const profile: Profile = {
5252
nhs_number: "test_nhs_number",
5353
};
5454

@@ -101,6 +101,7 @@ describe("getToken", () => {
101101
expectResultToMatchTokenWith(
102102
result,
103103
profile.nhs_number,
104+
"",
104105
"newIdToken",
105106
"new-apim-access-token",
106107
nowInSeconds + 1111,
@@ -118,7 +119,7 @@ describe("getToken", () => {
118119

119120
const result = await getToken(undefinedToken, undefinedAccount, undefinedProfile, maxAgeInSeconds);
120121

121-
expectResultToMatchTokenWith(result, "", "", "", 0, maxAgeInSeconds);
122+
expectResultToMatchTokenWith(result, "", "", "", "", 0, maxAgeInSeconds);
122123
});
123124

124125
it("should fill in missing values in token with default empty string", async () => {
@@ -166,7 +167,7 @@ describe("getToken", () => {
166167

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

169-
expectResultToMatchTokenWith(result, profile.nhs_number, "newIdToken", "", 0, maxAgeInSeconds);
170+
expectResultToMatchTokenWith(result, profile.nhs_number, "","newIdToken", "", 0, maxAgeInSeconds);
170171
});
171172
});
172173

@@ -182,13 +183,14 @@ describe("getToken", () => {
182183

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

185-
expectResultToMatchTokenWith(result, profile.nhs_number, "newIdToken", "", 0, maxAgeInSeconds);
186+
expectResultToMatchTokenWith(result, profile.nhs_number, "", "newIdToken", "", 0, maxAgeInSeconds);
186187
});
187188
});
188189

189190
const expectResultToMatchTokenWith = (
190191
result: JWT | null,
191192
nhsNumber: string,
193+
birthdate: string,
192194
idToken: string,
193195
apimToken: string,
194196
apimExpiresAt: number,
@@ -198,6 +200,7 @@ describe("getToken", () => {
198200
expect(result).toMatchObject({
199201
user: {
200202
nhs_number: nhsNumber,
203+
birthdate: birthdate,
201204
},
202205
nhs_login: {
203206
id_token: idToken,

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NhsNumber } from "@src/models/vaccine";
22
import { getOrRefreshApimCredentials } from "@src/utils/auth/apim/get-or-refresh-apim-credentials";
33
import { ApimAccessCredentials } from "@src/utils/auth/apim/types";
4-
import { IdToken, MaxAgeInSeconds, NowInSeconds } from "@src/utils/auth/types";
4+
import { BirthDate, IdToken, MaxAgeInSeconds, NowInSeconds } from "@src/utils/auth/types";
55
import { logger } from "@src/utils/logger";
66
import { RequestContext, asyncLocalStorage } from "@src/utils/requestContext";
77
import { extractRequestContextFromHeaders } from "@src/utils/requestScopedStorageWrapper";
@@ -10,6 +10,22 @@ 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+
1329
const log: Logger = logger.child({ module: "utils-auth-callbacks-get-token" });
1430

1531
/* from Next Auth documentation:
@@ -86,10 +102,11 @@ const fillMissingFieldsInTokenWithDefaultValues = (token: JWT, apimAccessCredent
86102
return {
87103
...token,
88104
user: {
89-
nhs_number: token.user?.nhs_number ?? "",
105+
nhs_number: token.user?.nhs_number ?? "" as NhsNumber,
106+
birthdate: token.user?.birthdate ?? "" as BirthDate,
90107
},
91108
nhs_login: {
92-
id_token: token.nhs_login?.id_token ?? "",
109+
id_token: token.nhs_login?.id_token ?? "" as IdToken,
93110
},
94111
apim: {
95112
access_token: (apimAccessCredentials ? apimAccessCredentials.accessToken : token.apim?.access_token) ?? "",
@@ -108,11 +125,12 @@ const updateTokenWithValuesFromAccountAndProfile = (
108125
profile: Profile,
109126
nowInSeconds: NowInSeconds,
110127
maxAgeInSeconds: MaxAgeInSeconds,
111-
) => {
128+
): JWT => {
112129
const updatedToken: JWT = {
113130
...token,
114131
user: {
115132
nhs_number: (profile.nhs_number ?? "") as NhsNumber,
133+
birthdate: token.user?.birthdate ?? "" as BirthDate,
116134
},
117135
nhs_login: {
118136
id_token: (account.id_token ?? "") as IdToken,

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NhsNumber } from "@src/models/vaccine";
22
import { getUpdatedSession } from "@src/utils/auth/callbacks/get-updated-session";
3-
import { AccessToken, ExpiresAt, IdToken } from "@src/utils/auth/types";
3+
import { AccessToken, Age, BirthDate, ExpiresAt, IdToken } from "@src/utils/auth/types";
44
import { Session } from "next-auth";
55
import { JWT } from "next-auth/jwt";
66

@@ -11,13 +11,15 @@ describe("getSession", () => {
1111
const session: Session = {
1212
user: {
1313
nhs_number: "" as NhsNumber,
14+
age: 0 as Age,
1415
},
1516
expires: "some-date",
1617
};
1718

1819
const token = {
1920
user: {
2021
nhs_number: "test-nhs-number" as NhsNumber,
22+
birthdate: "1995-01-01"
2123
},
2224
nhs_login: {
2325
id_token: "test-id-token" as IdToken,
@@ -31,12 +33,14 @@ describe("getSession", () => {
3133
const result: Session = getUpdatedSession(session, token);
3234

3335
expect(result.user.nhs_number).toBe("test-nhs-number");
36+
expect(result.user.age).toBe(30);
3437
});
3538

3639
it("does not update session if token.user is missing", () => {
3740
const session: Session = {
3841
user: {
3942
nhs_number: "old-nhs-number" as NhsNumber,
43+
age: 36 as Age,
4044
},
4145
expires: "some-date",
4246
};
@@ -46,6 +50,7 @@ describe("getSession", () => {
4650
const result: Session = getUpdatedSession(session, token);
4751

4852
expect(result.user.nhs_number).toBe("old-nhs-number");
53+
expect(result.user.age).toBe(36);
4954
});
5055

5156
it("does not update session if session.user is missing", () => {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import { logger } from "@src/utils/logger";
22
import { Session } from "next-auth";
33
import { JWT } from "next-auth/jwt";
44
import { Logger } from "pino";
5+
import { calculateAge } from "@src/utils/date";
56

67
const log: Logger = logger.child({
78
module: "utils-auth-callbacks-get-session",
89
});
910

10-
const getUpdatedSession = (session: Session, token: JWT) => {
11+
const getUpdatedSession = (session: Session, token: JWT): Session => {
1112
if (token?.user && session.user) {
1213
session.user.nhs_number = token.user.nhs_number;
14+
session.user.age = calculateAge(token.user.birthdate);
1315
} else {
1416
log.info(
1517
{

src/utils/auth/types.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export type AccessToken = Brand<string, "AccessToken">;
66
export type ExpiresIn = Brand<string, "ExpiresIn">;
77
export type ExpiresAt = Brand<number, "ExpiresAt">;
88
export type IdToken = Brand<string, "IdToken">;
9+
export type BirthDate = Brand<string, "BirthDate">;
10+
export type Age = Brand<number, "Age">;
911
export type NowInSeconds = Brand<number, "NowInSeconds">;
1012
export type MaxAgeInSeconds = Brand<number, "MaxAgeInSeconds">;
1113
export type ExpiresSoonAt = Brand<number, "ExpiresSoonAt">;
@@ -43,6 +45,7 @@ declare module "next-auth" {
4345
interface Session {
4446
user: {
4547
nhs_number: NhsNumber;
48+
age: Age;
4649
} & DefaultSession["user"];
4750
}
4851

@@ -55,6 +58,7 @@ declare module "next-auth/jwt" {
5558
interface JWT {
5659
user: {
5760
nhs_number: NhsNumber;
61+
birthdate: BirthDate;
5862
};
5963
nhs_login: {
6064
id_token: IdToken;

src/utils/logger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const REDACT_KEYS: string[] = [
1818
"api_key",
1919
"apimToken",
2020
"aud",
21+
"birthdate",
2122
"clientAssertion",
2223
"client_assertion",
2324
"elidUri",

0 commit comments

Comments
 (0)