Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/alchemy/src/Auth/AuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import * as Schema from "effect/Schema";
import * as Semaphore from "effect/Semaphore";
import { withLock } from "./Lock.ts";

/**
* Canonical web host for OAuth provider-agnostic landing pages
* (`/auth/success`, `/auth/error`). The CLI's loopback server 302s the
* browser to one of these after handling the OAuth callback. Centralized
* here so the redirect target lives in exactly one place across all
* provider OAuth clients.
*/
export const AUTH_LANDING_HOST = "https://v2.alchemy.run";
export const AUTH_SUCCESS_URL = `${AUTH_LANDING_HOST}/auth/success`;
export const AUTH_ERROR_URL = `${AUTH_LANDING_HOST}/auth/error`;

/**
* Methods on an {@link AuthProviderImpl} that mutate (or could trigger
* mutation of) on-disk credentials. The factory wraps these in a
Expand Down
13 changes: 6 additions & 7 deletions packages/alchemy/src/Cloudflare/Auth/OAuthClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import crypto from "node:crypto";
import http from "node:http";
import { AUTH_ERROR_URL, AUTH_SUCCESS_URL } from "../../Auth/AuthProvider.ts";
import {
OAUTH_CLIENT_ID,
OAUTH_ENDPOINTS,
Expand Down Expand Up @@ -224,7 +225,7 @@ function callbackPromise(
const error = url.searchParams.get("error");
const errorDescription = url.searchParams.get("error_description");
if (error) {
res.writeHead(302, { Location: "https://alchemy.run/auth/error" });
res.writeHead(302, { Location: AUTH_ERROR_URL });
res.end();
cleanup();
reject(
Expand All @@ -239,7 +240,7 @@ function callbackPromise(
const code = url.searchParams.get("code");
const state = url.searchParams.get("state");
if (!code || !state) {
res.writeHead(302, { Location: "https://alchemy.run/auth/error" });
res.writeHead(302, { Location: AUTH_ERROR_URL });
res.end();
cleanup();
reject(
Expand All @@ -252,7 +253,7 @@ function callbackPromise(
}

if (state !== authorization.state) {
res.writeHead(302, { Location: "https://alchemy.run/auth/error" });
res.writeHead(302, { Location: AUTH_ERROR_URL });
res.end();
cleanup();
reject(
Expand All @@ -268,14 +269,12 @@ function callbackPromise(
const credentials = await Effect.runPromise(
exchange(code, authorization.verifier),
);
res.writeHead(302, {
Location: "https://alchemy.run/auth/success",
});
res.writeHead(302, { Location: AUTH_SUCCESS_URL });
res.end();
cleanup();
resolve(credentials);
} catch (err) {
res.writeHead(302, { Location: "https://alchemy.run/auth/error" });
res.writeHead(302, { Location: AUTH_ERROR_URL });
res.end();
cleanup();
reject(err);
Expand Down
Loading
Loading