Skip to content

OpenStock is an open-source alternative to expensive market platforms. Track real-time prices, set personalized alerts, and explore detailed company insights — built openly, for everyone, forever free.

fix(auth): only show social sign-in buttons for configured providers

ravixalgorithme60d24b1fe9b
diff --git a/app/(auth)/layout.tsx b/app/(auth)/layout.tsx
index 89dff50..1f5f523 100644
--- a/app/(auth)/layout.tsx
+++ b/app/(auth)/layout.tsx
@@ -4,12 +4,19 @@ import Image from "next/image";
import {redirect} from "next/navigation";
import {getSession} from "@/lib/better-auth/auth";
import ProductPreview from "@/components/landing/ProductPreview";
+import { SocialProvidersProvider } from "@/components/forms/SocialAuthButtons";
const Layout = async ({ children }: { children : React.ReactNode }) => {
const session = await getSession();
if (session?.user) redirect('/dashboard')
+
+ // Same condition that enables the providers in lib/better-auth/auth.ts
+ const socialProviders = [
+ ...(process.env.GOOGLE_CLIENT_ID ? ['google' as const] : []),
+ ...(process.env.GITHUB_CLIENT_ID ? ['github' as const] : []),
+ ];
return (
<main className="auth-layout">
<section className="auth-left-section scrollbar-hide-default">
@@ -18,7 +25,7 @@ const Layout = async ({ children }: { children : React.ReactNode }) => {
</Link>
<div className="pb-6 lg:pb-8 flex-1">
- {children}
+ <SocialProvidersProvider enabled={socialProviders}>{children}</SocialProvidersProvider>
</div>
</section>
<section className="auth-right-section">
diff --git a/components/forms/SocialAuthButtons.tsx b/components/forms/SocialAuthButtons.tsx
index cfdae06..b40cd72 100644
--- a/components/forms/SocialAuthButtons.tsx
+++ b/components/forms/SocialAuthButtons.tsx
@@ -1,6 +1,6 @@
'use client';
-import React, { useState } from "react";
+import React, { createContext, useContext, useState } from "react";
import { Github } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
@@ -20,8 +20,19 @@ const PROVIDERS = [
{ id: 'github', label: 'GitHub', icon: <Github className="size-5" /> },
] as const;
+type ProviderId = 'google' | 'github';
+
+// The auth layout (a server component) knows which providers have keys and passes them down,
+// so pages never offer a button that can only fail.
+const EnabledProviders = createContext<ProviderId[]>([]);
+export const SocialProvidersProvider = ({ enabled, children }: { enabled: ProviderId[]; children: React.ReactNode }) => (
+ <EnabledProviders.Provider value={enabled}>{children}</EnabledProviders.Provider>
+);
+
const SocialAuthButtons = () => {
+ const enabled = useContext(EnabledProviders);
const [pending, setPending] = useState<string | null>(null);
+ const providers = PROVIDERS.filter((p) => enabled.includes(p.id));
const onClick = async (provider: 'google' | 'github') => {
setPending(provider);
@@ -34,10 +45,12 @@ const SocialAuthButtons = () => {
toast.error('Sign in failed', { description: result.error });
}
+ if (providers.length === 0) return null;
+
return (
<div className="space-y-5 mb-5">
- <div className="grid grid-cols-2 gap-3">
- {PROVIDERS.map(({ id, label, icon }) => (
+ <div className={providers.length > 1 ? 'grid grid-cols-2 gap-3' : 'grid gap-3'}>
+ {providers.map(({ id, label, icon }) => (
<Button
key={id}
type="button"