Skip to content

Commit

Permalink
Removing getPatronData and middleware and calling auth function on ge…
Browse files Browse the repository at this point in the history
…tServerSideProps instead
  • Loading branch information
EdwinGuzman committed Jan 8, 2024
1 parent 10f67f5 commit e968371
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 251 deletions.
7 changes: 7 additions & 0 deletions __test__/pages/Home.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import { render, screen } from "@testing-library/react"

import Home from "../../pages/index"

// Mock the auth module on the Page component level.
jest.mock("jose", () => ({
importSPKI: async () => Promise.resolve("testPublicKey"),
jwtVerify: async () => ({
payload: {},
}),
}))
// Mock next router
jest.mock("next/router", () => jest.requireActual("next-router-mock"))

Expand Down
39 changes: 0 additions & 39 deletions middleware.ts

This file was deleted.

15 changes: 5 additions & 10 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import Layout from "../src/components/Layout/Layout"
import RCLink from "../src/components/RCLink/RCLink"
import { SITE_NAME } from "../src/config/constants"
import { appConfig } from "../src/config/config"
import { getPatronData } from "../src/server/getPatronData"
import initializePatronTokenAuth from "../src/server/auth"

export default function Home() {
Expand Down Expand Up @@ -161,16 +160,12 @@ export default function Home() {
}

export async function getServerSideProps({ req }) {
// Get patron data if the cookie has been parsed in the middleware.
// This could be passed down to props for the page component to use.
// Every page that needs patron data must call initializePatronTokenAuth
// to find if the token is valid and what the patron id is.
const patronTokenResponse = await initializePatronTokenAuth(req)
// const patronData = await getPatronData({
// isTokenValid: patronTokenResponse?.isTokenValid,
// userId: patronTokenResponse?.decodedPatron?.sub,
// })
console.log("sevre side props")
// console.log(req.cookies)
console.log("This is just to test patronTokenResponse", patronTokenResponse)
// Now it can be used to get patron data from Sierra or Platform API
// or use `isTokenValid` to redirect to login page if it's not valid.
console.log("patronTokenResponse is", patronTokenResponse)

// return props object
return {
Expand Down
7 changes: 4 additions & 3 deletions src/server/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type { JWTPayload } from "jose"
import { importSPKI, jwtVerify } from "jose"
// import type { JWTPayload } from "jose"
import { importSPKI, jwtVerify, type JWTPayload } from "jose"
import type { NextRequest } from "next/server"

import { appConfig } from "../config/config"
Expand All @@ -20,8 +20,9 @@ interface UserJwtPayload extends JWTPayload {
* and then verify it through JWT. The decoded patron information is returned.
*/
export default async function initializePatronTokenAuth(req: NextRequest) {
const nyplIdentityPatron = (req.cookies as any)?.nyplIdentityPatron
type cookie = typeof req.cookies & { nyplIdentityPatron?: string }

const nyplIdentityPatron = (req.cookies as cookie)?.nyplIdentityPatron
const nyplIdentityCookieObject = nyplIdentityPatron
? JSON.parse(nyplIdentityPatron)
: {}
Expand Down
105 changes: 0 additions & 105 deletions src/server/getPatronData.ts

This file was deleted.

8 changes: 3 additions & 5 deletions src/server/tests/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ jest.mock("jose", () => ({
}))

const reqNoCookies = {
cookies: { get: () => undefined },
cookies: {},
} as NextRequest
const reqCookiesWithToken = {
cookies: {
get: () => ({
value: '{"access_token":123}',
}),
},
nyplIdentityPatron: '{"access_token":123}',
} as any,
} as NextRequest

describe("initializePatronTokenAuth", () => {
Expand Down
89 changes: 0 additions & 89 deletions src/server/tests/getPatronData.test.ts

This file was deleted.

0 comments on commit e968371

Please sign in to comment.