-
Notifications
You must be signed in to change notification settings - Fork 1
fix(nextjs): fix JSON parsing in Pages Router handler #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,252 @@ | ||
| import { describe, test, expect } from "vitest" | ||
| import { createMocks, createResponse, RequestOptions, ResponseOptions } from "node-mocks-http" | ||
| import { auth } from "@test/pages/preset" | ||
| import { NextApiRequest, NextApiResponse } from "next" | ||
| import { createCSRF } from "@aura-stack/react/crypto" | ||
| import { setResponseHeaders } from "@/pages/handler" | ||
|
|
||
| const createHandler = async (reqOptions?: RequestOptions, resOptions?: ResponseOptions) => { | ||
| if (reqOptions?.body) { | ||
| // @ts-ignore Next.js by default parses the body | ||
| reqOptions.body = JSON.stringify(reqOptions.body) | ||
| } | ||
| const { req, res } = createMocks(reqOptions, resOptions) | ||
| await auth.toHandler(req as unknown as NextApiRequest, res as unknown as NextApiResponse) | ||
| return { req, res } | ||
| } | ||
|
|
||
| describe("toHandler", () => { | ||
| test("unsupported method", async () => { | ||
| const { res } = await createHandler({ | ||
| method: "DELETE", | ||
| }) | ||
| expect(res.statusCode).toBe(405) | ||
| expect(res._getJSONData()).toEqual({ error: "Method DELETE Not Allowed" }) | ||
| }) | ||
|
|
||
| test("GET /auth/session - http connection", async () => { | ||
| const payload = { | ||
| sub: "1234567890", | ||
| name: "John Doe", | ||
| email: "john.doe@example.com", | ||
| image: "https://example.com/john-doe.jpg", | ||
| } | ||
|
|
||
| const sessionToken = await auth.jose.encodeJWT(payload) | ||
|
|
||
| const { res } = await createHandler({ | ||
| method: "GET", | ||
| url: "/auth/session", | ||
| headers: { | ||
| Cookie: `aura-auth.session_token=${sessionToken}`, | ||
| Host: "localhost:3000", | ||
| }, | ||
| }) | ||
| expect(res.statusCode).toBe(200) | ||
| expect(res._getJSONData()).toEqual({ | ||
| session: { | ||
| user: payload, | ||
| expires: expect.any(String), | ||
| }, | ||
| success: true, | ||
| }) | ||
| }) | ||
|
|
||
| test("GET /auth/session - https connection", async () => { | ||
| const payload = { | ||
| sub: "1234567890", | ||
| name: "John Doe", | ||
| email: "john.doe@example.com", | ||
| image: "https://example.com/john-doe.jpg", | ||
| } | ||
|
|
||
| const sessionToken = await auth.jose.encodeJWT(payload) | ||
|
|
||
| const { res } = await createHandler({ | ||
| method: "GET", | ||
| url: "/auth/session", | ||
| headers: { | ||
| Cookie: `__Secure-aura-auth.session_token=${sessionToken}`, | ||
| Host: "example.com", | ||
| "X-Forwarded-Proto": "https", | ||
| }, | ||
| }) | ||
| expect(res.statusCode).toBe(200) | ||
| expect(res._getJSONData()).toEqual({ | ||
| session: { | ||
| user: payload, | ||
| expires: expect.any(String), | ||
| }, | ||
| success: true, | ||
| }) | ||
| }) | ||
|
|
||
| test("POST /auth/signIn/credentials", async () => { | ||
| const { res } = await createHandler({ | ||
| method: "POST", | ||
| url: "/auth/signIn/credentials", | ||
| headers: { | ||
| Host: "localhost:3000", | ||
| "X-Forwarded-Proto": "http", | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: { | ||
| username: "john.doe", | ||
| password: "super-password-123", | ||
| }, | ||
| }) | ||
| expect(res.statusCode).toBe(200) | ||
| expect(res._getJSONData()).toEqual({ | ||
| success: true, | ||
| redirectURL: "/", | ||
| }) | ||
| }) | ||
|
|
||
| test("POST /auth/signIn/credentials - invalid body", async () => { | ||
| const { res } = await createHandler({ | ||
| method: "POST", | ||
| url: "/auth/signIn/credentials", | ||
| headers: { | ||
| Host: "localhost:3000", | ||
| "X-Forwarded-Proto": "http", | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: { | ||
| name: "john.doe", | ||
| hash: "super-password-123", | ||
| }, | ||
| }) | ||
| expect(res.statusCode).toBe(422) | ||
| expect(res._getJSONData()).toEqual({ | ||
| type: "ROUTER_ERROR", | ||
| code: "INVALID_REQUEST", | ||
| message: { | ||
| password: { | ||
| code: "invalid_type", | ||
| message: "Invalid input: expected string, received undefined", | ||
| }, | ||
| username: { | ||
| code: "invalid_type", | ||
| message: "Invalid input: expected string, received undefined", | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| test("PATCH /auth/session", async () => { | ||
| const payload = { | ||
| sub: "1234567890", | ||
| name: "John Doe", | ||
| email: "john.doe@example.com", | ||
| image: "https://example.com/john-doe.jpg", | ||
| } | ||
|
|
||
| const csrfToken = await createCSRF(auth.jose) | ||
| const sessionToken = await auth.jose.encodeJWT(payload) | ||
|
|
||
| const { res } = await createHandler({ | ||
| method: "PATCH", | ||
| url: "/auth/session", | ||
| headers: { | ||
| Host: "localhost:3000", | ||
| "X-Forwarded-Proto": "http", | ||
| "Content-Type": "application/json", | ||
| Cookie: `aura-auth.session_token=${sessionToken}; aura-auth.csrf_token=${csrfToken}`, | ||
| "X-CSRF-Token": csrfToken, | ||
| }, | ||
| body: { | ||
| user: { | ||
| name: "Alice", | ||
| image: "https://example.com/alice.jpg", | ||
| }, | ||
| }, | ||
| }) | ||
| expect(res.statusCode).toBe(200) | ||
| expect(res._getJSONData()).toEqual({ | ||
| success: true, | ||
| session: { | ||
| user: { | ||
| sub: "1234567890", | ||
| name: "Alice", | ||
| email: "john.doe@example.com", | ||
| image: "https://example.com/alice.jpg", | ||
| }, | ||
| expires: expect.any(String), | ||
| }, | ||
| redirectURL: null, | ||
| }) | ||
| }) | ||
|
|
||
| test("POST /auth/signOut", async () => { | ||
| const payload = { | ||
| sub: "1234567890", | ||
| name: "John Doe", | ||
| email: "john.doe@example.com", | ||
| image: "https://example.com/john-doe.jpg", | ||
| } | ||
|
|
||
| const csrfToken = await createCSRF(auth.jose) | ||
| const sessionToken = await auth.jose.encodeJWT(payload) | ||
|
|
||
| const { res } = await createHandler({ | ||
| method: "POST", | ||
| url: "/auth/signOut?token_type_hint=session_token", | ||
| headers: { | ||
| Host: "localhost:3000", | ||
| "X-Forwarded-Proto": "http", | ||
| "Content-Type": "application/json", | ||
| Cookie: `aura-auth.session_token=${sessionToken}; aura-auth.csrf_token=${csrfToken}`, | ||
| "X-CSRF-Token": csrfToken, | ||
| }, | ||
| body: {}, | ||
| }) | ||
| expect(res.statusCode).toBe(202) | ||
| expect(res._getJSONData()).toEqual({ | ||
| success: true, | ||
| redirect: false, | ||
| redirectURL: "/", | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe("setResponseHeaders", () => { | ||
| test("sets headers on Next.js response", () => { | ||
| const response = createResponse() as unknown as NextApiResponse | ||
| const headers = new Headers({ | ||
| "Content-Type": "application/json", | ||
| Cookie: "session_token=session; Path=/; HttpOnly; Secure; Domain=/", | ||
| }) | ||
| setResponseHeaders(response, headers) | ||
| expect(response.getHeaders()).toEqual({ | ||
| "content-type": "application/json", | ||
| cookie: "session_token=session; Path=/; HttpOnly; Secure; Domain=/", | ||
| }) | ||
| }) | ||
|
|
||
| test("overwrites existing headers", () => { | ||
| const response = createResponse() as unknown as NextApiResponse | ||
| response.setHeader("Content-Type", "text/plain") | ||
| const headers = new Headers({ | ||
| "Content-Type": "application/json", | ||
| }) | ||
| setResponseHeaders(response, headers) | ||
| expect(response.getHeader("Content-Type")).toBe("application/json") | ||
| }) | ||
|
|
||
| test("handle set-cookie headers", () => { | ||
| const response = createResponse() as unknown as NextApiResponse | ||
| const headers = new Headers([ | ||
| ["Set-Cookie", "session_token=session; Path=/; HttpOnly; Secure; Domain=/"], | ||
| ["Set-Cookie", "csrf_token=csrf; Path=/; HttpOnly; Secure; Domain=/"], | ||
| ["Set-Cookie", "__Secure-session=session; Path=/; HttpOnly; Secure; Domain=/"], | ||
| ["Set-Cookie", "__Secure-csrf=csrf; Path=/; HttpOnly; Secure; Domain=/"], | ||
| ]) | ||
| setResponseHeaders(response, headers) | ||
| expect(response.getHeader("Set-Cookie")).toEqual([ | ||
| "session_token=session; Path=/; HttpOnly; Secure; Domain=/", | ||
| "csrf_token=csrf; Path=/; HttpOnly; Secure; Domain=/", | ||
| "__Secure-session=session; Path=/; HttpOnly; Secure; Domain=/", | ||
| "__Secure-csrf=csrf; Path=/; HttpOnly; Secure; Domain=/", | ||
| ]) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { createAuth } from "@/pages/createAuth" | ||
| import { createSecretValue } from "@aura-stack/react/crypto" | ||
|
|
||
| export const auth = createAuth({ | ||
| oauth: [], | ||
| logger: true, | ||
| credentials: { | ||
| authorize: (ctx) => { | ||
| const { username, password } = ctx.credentials | ||
| if (!username || !password) return null | ||
| const sub = createSecretValue(10) | ||
| return { | ||
| sub, | ||
| name: username, | ||
| email: `${username.toLowerCase()}@example.com`, | ||
| } | ||
| }, | ||
| }, | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.