Skip to content
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

feat: disabling authentication for local testing #1301

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ APP_ORIGIN=http://localhost:3030
ELECTRIC_ORIGIN=http://localhost:3060
NODE_ENV=development
V3_ENABLED=true
AUTH_DISABLED=true

# Redis is used for the v3 queuing and v2 concurrency control
REDIS_HOST="localhost"
Expand Down
5 changes: 3 additions & 2 deletions apps/webapp/app/env.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SecretStoreOptionsSchema } from "./services/secrets/secretStoreOptionsSchema.server";
import { z } from "zod";
import { isValidRegex } from "./utils/regex";
import { SecretStoreOptionsSchema } from "./services/secrets/secretStoreOptionsSchema.server";
import { isValidDatabaseUrl } from "./utils/db";
import { isValidRegex } from "./utils/regex";

const EnvironmentSchema = z.object({
NODE_ENV: z.union([z.literal("development"), z.literal("production"), z.literal("test")]),
Expand All @@ -28,6 +28,7 @@ const EnvironmentSchema = z.object({
.refine(isValidRegex, "WHITELISTED_EMAILS must be a valid regex.")
.optional(),
ADMIN_EMAILS: z.string().refine(isValidRegex, "ADMIN_EMAILS must be a valid regex.").optional(),
AUTH_DISABLED: z.string().optional(),
REMIX_APP_PORT: z.string().optional(),
LOGIN_ORIGIN: z.string().default("http://localhost:3030"),
APP_ORIGIN: z.string().default("http://localhost:3030"),
Expand Down
22 changes: 16 additions & 6 deletions apps/webapp/app/services/auth.server.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import { Authenticator } from "remix-auth";
import { env } from "~/env.server";
import type { AuthUser } from "./authUser";
import { addEmailLinkStrategy } from "./emailAuth.server";
import { addGitHubStrategy } from "./gitHubAuth.server";
import { sessionStorage } from "./sessionStorage.server";
import { env } from "~/env.server";

// Create an instance of the authenticator, pass a generic with what
// strategies will return and will store in the session
const authenticator = new Authenticator<AuthUser>(sessionStorage);

const isGithubAuthSupported =
typeof env.AUTH_GITHUB_CLIENT_ID === "string" &&
typeof env.AUTH_GITHUB_CLIENT_SECRET === "string";
typeof env.AUTH_GITHUB_CLIENT_ID === "string" &&
typeof env.AUTH_GITHUB_CLIENT_SECRET === "string";

if (env.AUTH_GITHUB_CLIENT_ID && env.AUTH_GITHUB_CLIENT_SECRET) {
addGitHubStrategy(authenticator, env.AUTH_GITHUB_CLIENT_ID, env.AUTH_GITHUB_CLIENT_SECRET);
if (
env.AUTH_GITHUB_CLIENT_ID &&
env.AUTH_GITHUB_CLIENT_SECRET &&
env.AUTH_DISABLED !== "true"
) {
addGitHubStrategy(
authenticator,
env.AUTH_GITHUB_CLIENT_ID,
env.AUTH_GITHUB_CLIENT_SECRET,
);
}

addEmailLinkStrategy(authenticator);
if (env.AUTH_DISABLED !== "true") {
addEmailLinkStrategy(authenticator);
}

export { authenticator, isGithubAuthSupported };
54 changes: 31 additions & 23 deletions apps/webapp/app/services/session.server.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,56 @@
import { redirect } from "@remix-run/node";
import { env } from "~/env.server";
import { getUserById } from "~/models/user.server";
import { authenticator } from "./auth.server";
import { getImpersonationId } from "./impersonation.server";

export async function getUserId(request: Request): Promise<string | undefined> {
const impersonatedUserId = await getImpersonationId(request);
const impersonatedUserId = await getImpersonationId(request);

if (impersonatedUserId) return impersonatedUserId;
if (impersonatedUserId) return impersonatedUserId;

let authUser = await authenticator.isAuthenticated(request);
return authUser?.userId;
let authUser = await authenticator.isAuthenticated(request);
return authUser?.userId;
}

export async function getUser(request: Request) {
const userId = await getUserId(request);
if (userId === undefined) return null;
const userId = await getUserId(request);
if (userId === undefined) return null;

const user = await getUserById(userId);
if (user) return user;
const user = await getUserById(userId);
if (user) return user;

throw await logout(request);
throw await logout(request);
}

export async function requireUserId(request: Request, redirectTo?: string) {
const userId = await getUserId(request);
if (!userId) {
const url = new URL(request.url);
const searchParams = new URLSearchParams([
["redirectTo", redirectTo ?? `${url.pathname}${url.search}`],
]);
throw redirect(`/login?${searchParams}`);
}
return userId;
const userId = await getUserId(request);
if (!userId) {
const url = new URL(request.url);
const searchParams = new URLSearchParams([
["redirectTo", redirectTo ?? `${url.pathname}${url.search}`],
]);
throw redirect(`/login?${searchParams}`);
}
return userId;
}

export async function requireUser(request: Request) {
const userId = await requireUserId(request);
if (env.AUTH_DISABLED === "true") {
return {
id: "1",
email: "[email protected]",
};
}

const user = await getUserById(userId);
if (user) return user;
const userId = await requireUserId(request);

throw await logout(request);
const user = await getUserById(userId);
if (user) return user;

throw await logout(request);
}

export async function logout(request: Request) {
return redirect("/logout");
return redirect("/logout");
}