Skip to content

Commit

Permalink
feat(core): add default cache control header for GET session
Browse files Browse the repository at this point in the history
  • Loading branch information
ThangHuuVu committed Aug 24, 2024
1 parent b8d424a commit ebca184
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/pages/getting-started/session-management/get-session.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,8 @@ app.get("/", (req, res) => {
</Code>

If you'd like to extend your session with more fields from your OAuth provider, for example, please check out our ["extending the session" guide](/guides/extending-the-session).

<Callout>
By default, GET requests to the session endpoint will automatically return the
headers to prevent caching.
</Callout>
9 changes: 8 additions & 1 deletion packages/core/src/lib/actions/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ export async function session(

const response: ResponseInternal<Session | null> = {
body: null,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
...(!isUpdate && {
"Cache-Control": "private, no-cache, no-store",
Expires: "0",
Pragma: "no-cache",
}),
},
cookies,
}

Expand Down
23 changes: 22 additions & 1 deletion packages/core/test/actions/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ import {
SESSION_COOKIE_NAME,
} from "../utils.js"

const assertResponseHeaders = (response: Response) => {
expect(response.headers.get("Content-Type")).toEqual("application/json")
expect(response.headers.get("Cache-Control")).toEqual(
"private, no-cache, no-store"
)
expect(response.headers.get("Expires")).toEqual("0")
expect(response.headers.get("Pragma")).toEqual("no-cache")
}

describe("assert GET session action", () => {
beforeEach(() => {
vi.resetAllMocks()
Expand Down Expand Up @@ -94,6 +103,8 @@ describe("assert GET session action", () => {
session: expectedSession,
token: expectedToken,
})

assertResponseHeaders(response)
})

it("should return null if no JWT session in the requests cookies", async () => {
Expand All @@ -102,6 +113,8 @@ describe("assert GET session action", () => {
})
const actual = await response.json()
expect(actual).toEqual(null)

assertResponseHeaders(response)
})

it("should return null if JWT session is invalid", async () => {
Expand All @@ -113,6 +126,8 @@ describe("assert GET session action", () => {
})
const actual = await response.json()
expect(actual).toEqual(null)

assertResponseHeaders(response)
})

it("should throw invalid JWT error if salt is invalid", async () => {
Expand All @@ -132,8 +147,10 @@ describe("assert GET session action", () => {
})
const actual = await response.json()

expect(logger.error).toHaveBeenCalledOnce()
expect(actual).toEqual(null)
expect(logger.error).toHaveBeenCalledOnce()

assertResponseHeaders(response)
})
})
describe("Database strategy", () => {
Expand Down Expand Up @@ -209,6 +226,8 @@ describe("assert GET session action", () => {
email: expectedUser.email,
})
expect(actualBodySession.expires).toEqual(currentExpires.toISOString())

assertResponseHeaders(response)
})

it("should return null in the response, and delete the session", async () => {
Expand Down Expand Up @@ -263,6 +282,8 @@ describe("assert GET session action", () => {

expect(actualSessionToken).toEqual("")
expect(actualBodySession).toEqual(null)

assertResponseHeaders(response)
})
})
})

0 comments on commit ebca184

Please sign in to comment.