Skip to content
Merged
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
9 changes: 9 additions & 0 deletions apps/web/app/api/auth-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { trpc } from "./trpc";

export const useAuthConfig = () => {
const { data } = trpc.auth.config.useQuery(undefined, {
staleTime: 60_000,
});

return data ?? null;
};
12 changes: 10 additions & 2 deletions apps/web/app/routes/auth/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useForm } from "react-hook-form";
import { z } from "zod";

import { authClient } from "~/api/auth-client";
import { useAuthConfig } from "~/api/auth-config";
import { Button } from "~/components/ui/button";
import {
Card,
Expand Down Expand Up @@ -109,6 +110,9 @@ function LoginEmailPassword() {
}

export default function Login() {
const authConfig = useAuthConfig();
const isCredentialsEnabled = authConfig?.credentialsEnabled ?? false;

return (
<div className="bg-linear-to-br flex min-h-screen items-center justify-center from-background via-background to-muted/20 p-4">
<div className="mx-auto w-full" style={{ maxWidth: "400px" }}>
Expand Down Expand Up @@ -169,8 +173,12 @@ export default function Login() {
</svg>
<span>Continue with Google</span>
</Button>
<LoginSeparator />
<LoginEmailPassword />
{isCredentialsEnabled ? (
<>
<LoginSeparator />
<LoginEmailPassword />
</>
) : null}
</div>
</CardContent>
</Card>
Expand Down
16 changes: 15 additions & 1 deletion apps/web/app/routes/auth/signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useForm } from "react-hook-form";
import { z } from "zod";

import { authClient } from "~/api/auth-client";
import { useAuthConfig } from "~/api/auth-config";
import { Button } from "~/components/ui/button";
import {
Card,
Expand Down Expand Up @@ -101,6 +102,9 @@ function SignupForm() {
}

export default function Signup() {
const authConfig = useAuthConfig();
const credentialsEnabled = authConfig?.credentialsEnabled;

return (
<div className="bg-linear-to-br flex min-h-screen items-center justify-center from-background via-background to-muted/20 p-4">
<div className="mx-auto w-full" style={{ maxWidth: "400px" }}>
Expand Down Expand Up @@ -134,7 +138,17 @@ export default function Signup() {
</CardHeader>

<CardContent className="space-y-6 px-6">
<SignupForm />
{credentialsEnabled === true ? (
<SignupForm />
) : credentialsEnabled === false ? (
<p className="text-sm text-muted-foreground">
Email and password sign-up is disabled.
</p>
) : (
<p className="text-sm text-muted-foreground">
Checking available sign-up options...
</p>
)}
</CardContent>
</Card>
</div>
Expand Down
18 changes: 11 additions & 7 deletions packages/auth/src/better/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ export const isGoogleAuthEnabled =
env.AUTH_GOOGLE_CLIENT_SECRET != null &&
env.AUTH_GOOGLE_CLIENT_SECRET !== "";

export const isOIDCAuthEnabled = false;
// env.AUTH_OIDC_CLIENT_ID != null && env.AUTH_OIDC_ISSUER !== "";
export const isCredentialsAuthEnabled = false;
// env.AUTH_CREDENTIALS_ENABLED === "auto"
// ? !isGoogleAuthEnabled && !isOIDCAuthEnabled
// : env.AUTH_CREDENTIALS_ENABLED === "true";
export const isOIDCAuthEnabled =
env.AUTH_OIDC_CLIENT_ID != null &&
env.AUTH_OIDC_CLIENT_ID !== "" &&
env.AUTH_OIDC_ISSUER != null &&
env.AUTH_OIDC_ISSUER !== "";

export const isCredentialsAuthEnabled =
env.AUTH_CREDENTIALS_ENABLED === "auto"
? !isGoogleAuthEnabled && !isOIDCAuthEnabled
: env.AUTH_CREDENTIALS_ENABLED === "true";

export const auth = betterAuth({
database: drizzleAdapter(db, {
Expand All @@ -39,7 +43,7 @@ export const auth = betterAuth({
},
},
emailAndPassword: {
enabled: true,
enabled: isCredentialsAuthEnabled,
},
trustedOrigins: [env.BASE_URL, "http://localhost:5173"],
advanced: {
Expand Down
2 changes: 2 additions & 0 deletions packages/trpc/src/root.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { authRouter } from "./routes/auth.js";
import { deploymentTracesRouter } from "./routes/deployment-traces.js";
import { deploymentVersionsRouter } from "./routes/deployment-versions.js";
import { deploymentsRouter } from "./routes/deployments.js";
Expand All @@ -18,6 +19,7 @@ import { workspaceRouter } from "./routes/workspace.js";
import { router } from "./trpc.js";

export const appRouter = router({
auth: authRouter,
user: userRouter,
resource: resourcesRouter,
workspace: workspaceRouter,
Expand Down
15 changes: 15 additions & 0 deletions packages/trpc/src/routes/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
isCredentialsAuthEnabled,
isGoogleAuthEnabled,
isOIDCAuthEnabled,
} from "@ctrlplane/auth/server";

import { publicProcedure, router } from "../trpc.js";

export const authRouter = router({
config: publicProcedure.query(() => ({
credentialsEnabled: isCredentialsAuthEnabled,
googleEnabled: isGoogleAuthEnabled,
oidcEnabled: isOIDCAuthEnabled,
})),
});
Loading