Skip to content

Commit

Permalink
feat: add redirectUrl as a constructor option
Browse files Browse the repository at this point in the history
  • Loading branch information
SayakMukhopadhyay committed Dec 6, 2022
1 parent 22bf430 commit 9b1772c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export class OAuthApp<
defaultScopes: options.defaultScopes || [],
allowSignup: options.allowSignup,
baseUrl: options.baseUrl,
redirectUrl: options.redirectUrl,
log: options.log,
Octokit,
octokit,
Expand Down
1 change: 1 addition & 0 deletions src/methods/get-web-flow-authorization-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function getWebFlowAuthorizationUrlWithState(
request: state.octokit.request,
...options,
allowSignup,
redirectUrl: options.redirectUrl || state.redirectUrl,
scopes: options.scopes || state.defaultScopes,
};

Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type CommonOptions<TOctokit extends OAuthAppOctokitClassType> = {
clientSecret?: ClientSecret;
allowSignup?: boolean;
baseUrl?: string;
redirectUrl?: string;
log?: typeof console;
Octokit?: TOctokit;
};
Expand Down Expand Up @@ -82,6 +83,7 @@ export type State = {
defaultScopes: Scope[];
allowSignup?: boolean;
baseUrl?: string;
redirectUrl?: string;
log?: typeof console;
Octokit: OAuthAppOctokitClassType;
octokit: OctokitInstance;
Expand Down
67 changes: 67 additions & 0 deletions test/node-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1080,4 +1080,71 @@ describe("createNodeMiddleware(app)", () => {
expect(url.searchParams.get("state")).toMatch(/^\w+$/);
expect(url.searchParams.get("allow_signup")).toEqual("true");
});

it("GET /api/github/oauth/login?redirectUrl=http://localhost:12345 with redirectUrl option not set", async () => {
const app = new OAuthApp({
clientId: "0123",
clientSecret: "0123secret",
});

const server = createServer(createNodeMiddleware(app)).listen();
// @ts-expect-error complains about { port } although it's included in returned AddressInfo interface
const { port } = server.address();

const { status, headers } = await fetch(
`http://localhost:${port}/api/github/oauth/login?redirectUrl=http://localhost:12345`,
{
redirect: "manual",
}
);

server.close();

expect(status).toEqual(302);

const url = new URL(headers.get("location") as string);
expect(url).toMatchObject({
origin: "https://github.com",
pathname: "/login/oauth/authorize",
});
expect(url.searchParams.get("client_id")).toEqual("0123");
expect(url.searchParams.get("state")).toMatch(/^\w+$/);
expect(url.searchParams.get("redirect_uri")).toEqual(
"http://localhost:12345"
);
});

it("GET /api/github/oauth/login with redirectUrl option set to http://localhost:1234", async () => {
const app = new OAuthApp({
clientId: "0123",
clientSecret: "0123secret",
redirectUrl: "http://localhost:12345",
});

const server = createServer(createNodeMiddleware(app)).listen();
// @ts-expect-error complains about { port } although it's included in returned AddressInfo interface
const { port } = server.address();

const { status, headers } = await fetch(
`http://localhost:${port}/api/github/oauth/login`,
{
redirect: "manual",
}
);

server.close();

expect(status).toEqual(302);

const url = new URL(headers.get("location") as string);
expect(url).toMatchObject({
origin: "https://github.com",
pathname: "/login/oauth/authorize",
});
expect(url.searchParams.get("client_id")).toEqual("0123");
expect(url.searchParams.get("state")).toMatch(/^\w+$/);
expect(url.searchParams.get("redirect_uri")).toEqual(
"http://localhost:12345"
);
});
});

0 comments on commit 9b1772c

Please sign in to comment.