Skip to content

Commit af4087d

Browse files
VIA-172 AJ/AS Moved types to separate file
1 parent 2ac5df3 commit af4087d

File tree

3 files changed

+40
-37
lines changed

3 files changed

+40
-37
lines changed

auth.ts

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,15 @@
11
import NHSLoginAuthProvider from "@src/app/api/auth/[...nextauth]/provider";
22
import { SESSION_LOGOUT_ROUTE } from "@src/app/session-logout/constants";
33
import { SSO_FAILURE_ROUTE } from "@src/app/sso-failure/constants";
4+
import type { DecodedToken } from "@src/utils/auth/types";
45
import { AppConfig, configProvider } from "@src/utils/config";
56
import { logger } from "@src/utils/logger";
6-
import NextAuth, { type DefaultSession } from "next-auth";
7+
import NextAuth from "next-auth";
78
import "next-auth/jwt";
89
import { jwtDecode } from "jwt-decode";
910
import { Logger } from "pino";
1011
import { generateClientAssertion } from "@src/utils/auth/generate-refresh-client-assertion";
1112

12-
export interface DecodedToken {
13-
iss: string;
14-
aud: string;
15-
identity_proofing_level: string;
16-
}
17-
18-
// Augmenting types. Ref https://authjs.dev/getting-started/typescript#module-augmentation
19-
declare module "next-auth" {
20-
interface Session {
21-
user: {
22-
nhs_number: string,
23-
birthdate: string,
24-
access_token: string,
25-
} & DefaultSession["user"],
26-
}
27-
28-
interface Profile {
29-
nhs_number: string,
30-
}
31-
}
32-
33-
declare module "next-auth/jwt" {
34-
interface JWT {
35-
user: {
36-
nhs_number: string,
37-
birthdate: string,
38-
},
39-
expires_at: number,
40-
refresh_token: string,
41-
access_token: string,
42-
fixedExpiry: number;
43-
}
44-
}
45-
4613
const log: Logger = logger.child({ module: "auth" });
4714

4815
const MAX_SESSION_AGE_SECONDS: number = 12 * 60 * 60; // 12 hours of continuous usage
@@ -85,6 +52,7 @@ export const { handlers, signIn, signOut, auth } = NextAuth(async () => {
8552
}
8653
return isValidToken;
8754
},
55+
8856
async jwt({ token, account, profile}) {
8957
if (!token) {
9058
log.error("No token available in jwt callback.");

src/utils/auth/types.d.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { DefaultSession } from "next-auth";
2+
3+
export interface DecodedToken {
4+
iss: string;
5+
aud: string;
6+
identity_proofing_level: string;
7+
}
8+
9+
// Augmenting types. Ref https://authjs.dev/getting-started/typescript#module-augmentation
10+
declare module "next-auth" {
11+
interface Session {
12+
user: {
13+
nhs_number: string;
14+
birthdate: string;
15+
access_token: string;
16+
} & DefaultSession["user"];
17+
}
18+
19+
interface Profile {
20+
nhs_number: string;
21+
}
22+
}
23+
24+
declare module "next-auth/jwt" {
25+
interface JWT {
26+
user: {
27+
nhs_number: string;
28+
birthdate: string;
29+
};
30+
expires_at: number;
31+
refresh_token: string;
32+
access_token: string;
33+
fixedExpiry: number;
34+
}
35+
}

src/utils/auth/user-logout.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { userLogout } from "@src/utils/auth/user-logout";
33
import { signOut } from "next-auth/react";
44

55
jest.mock("next-auth/react", () => ({
6-
signOut: jest.fn()
6+
signOut: jest.fn(),
77
}));
88

99
describe("user-logout", () => {
@@ -12,7 +12,7 @@ describe("user-logout", () => {
1212

1313
expect(signOut).toHaveBeenCalledWith({
1414
redirect: true,
15-
redirectTo: SESSION_LOGOUT_ROUTE
15+
redirectTo: SESSION_LOGOUT_ROUTE,
1616
});
1717
});
1818
});

0 commit comments

Comments
 (0)