A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
feat: add OAuth auto-redirect functionality (#2483)
Mohamed Bassem33b91e3f1a57
diff --git a/apps/web/components/signin/OAuthAutoRedirect.tsx b/apps/web/components/signin/OAuthAutoRedirect.tsxnew file mode 100644index 00000000..28ef6beb--- /dev/null+++ b/apps/web/components/signin/OAuthAutoRedirect.tsx@@ -0,0 +1,46 @@+"use client";++import { useEffect, useState } from "react";+import { useSearchParams } from "next/navigation";+import { signIn } from "@/lib/auth/client";+import { useClientConfig } from "@/lib/clientConfig";++export default function OAuthAutoRedirect({+ oauthProviderId,+}: {+ oauthProviderId: string;+}) {+ const clientConfig = useClientConfig();+ const searchParams = useSearchParams();+ const hasError = searchParams.has("error");+ const callbackUrl = searchParams.get("callbackUrl") ?? "/";++ const shouldRedirect =+ clientConfig.auth.oauthAutoRedirect &&+ clientConfig.auth.disablePasswordAuth &&+ !!oauthProviderId &&+ !hasError;++ const [isRedirecting, setIsRedirecting] = useState(shouldRedirect);++ useEffect(() => {+ if (shouldRedirect) {+ signIn(oauthProviderId, {+ callbackUrl,+ });+ } else {+ setIsRedirecting(false);+ }+ }, [shouldRedirect, oauthProviderId, callbackUrl]);++ if (isRedirecting) {+ return (+ <div className="flex justify-center p-8">+ <span className="text-muted-foreground">+ Redirecting to login provider...+ </span>+ </div>+ );+ }+ return null;+}diff --git a/apps/web/components/signin/SignInForm.tsx b/apps/web/components/signin/SignInForm.tsxindex 8ad39f8f..7501b63d 100644--- a/apps/web/components/signin/SignInForm.tsx+++ b/apps/web/components/signin/SignInForm.tsx@@ -12,6 +12,7 @@ import { Info } from "lucide-react";import serverConfig from "@karakeep/shared/config";import CredentialsForm from "./CredentialsForm";+import OAuthAutoRedirect from "./OAuthAutoRedirect";import SignInProviderButton from "./SignInProviderButton";export default async function SignInForm() {@@ -26,6 +27,10 @@ export default async function SignInForm() {return (<div className="w-full">+ {/* Auto-redirect to OAuth provider if configured */}+ {providerValues && providerValues.length > 0 && (+ <OAuthAutoRedirect oauthProviderId={providerValues[0].id} />+ )}<Card className="w-full"><CardHeader className="text-center"><CardTitle className="text-2xl font-bold">Welcome Back</CardTitle>diff --git a/apps/web/lib/clientConfig.tsx b/apps/web/lib/clientConfig.tsxindex ab367be0..a50e881d 100644--- a/apps/web/lib/clientConfig.tsx+++ b/apps/web/lib/clientConfig.tsx@@ -9,6 +9,7 @@ export const ClientConfigCtx = createContext<ClientConfig>({auth: {disableSignups: false,disablePasswordAuth: false,+ oauthAutoRedirect: false,},turnstile: null,inference: {diff --git a/docs/docs/03-configuration/01-environment-variables.md b/docs/docs/03-configuration/01-environment-variables.mdindex 091ec003..f06a9e81 100644--- a/docs/docs/03-configuration/01-environment-variables.md+++ b/docs/docs/03-configuration/01-environment-variables.md@@ -70,6 +70,7 @@ When setting up OAuth, the allowed redirect URLs configured at the provider shou| DISABLE_SIGNUPS | No | false | If enabled, no new signups will be allowed and the signup button will be disabled in the UI || DISABLE_PASSWORD_AUTH | No | false | If enabled, only signups and logins using OAuth are allowed and the signup button and login form for local accounts will be disabled in the UI || EMAIL_VERIFICATION_REQUIRED | No | false | Whether email verification is required during user signup. If enabled, users must verify their email address before they can use their account. If you enable this, you must configure SMTP settings. |+| OAUTH_AUTO_REDIRECT | No | false | If enabled and password authentication is disabled, automatically redirect to the OAuth provider instead of showing the login page. Useful when OAuth is the only authentication method available. || OAUTH_WELLKNOWN_URL | No | Not set | The "wellknown Url" for openid-configuration as provided by the OAuth provider || OAUTH_CLIENT_SECRET | No | Not set | The "Client Secret" as provided by the OAuth provider || OAUTH_CLIENT_ID | No | Not set | The "Client ID" as provided by the OAuth provider |diff --git a/packages/shared/config.ts b/packages/shared/config.tsindex 3c10cd19..42b62524 100644--- a/packages/shared/config.ts+++ b/packages/shared/config.ts@@ -47,6 +47,7 @@ const allEnv = z.object({NEXTAUTH_SECRET: z.string().optional(),DISABLE_SIGNUPS: stringBool("false"),DISABLE_PASSWORD_AUTH: stringBool("false"),+ OAUTH_AUTO_REDIRECT: stringBool("false"),OAUTH_ALLOW_DANGEROUS_EMAIL_ACCOUNT_LINKING: stringBool("false"),OAUTH_WELLKNOWN_URL: z.string().url().optional(),OAUTH_CLIENT_SECRET: z.string().optional(),@@ -246,6 +247,7 @@ const serverConfigSchema = allEnv.transform((val, ctx) => {disablePasswordAuth: val.DISABLE_PASSWORD_AUTH,emailVerificationRequired: val.EMAIL_VERIFICATION_REQUIRED,oauth: {+ autoRedirect: val.OAUTH_AUTO_REDIRECT,allowDangerousEmailAccountLinking:val.OAUTH_ALLOW_DANGEROUS_EMAIL_ACCOUNT_LINKING,wellKnownUrl: val.OAUTH_WELLKNOWN_URL,@@ -465,6 +467,7 @@ export const clientConfig = {auth: {disableSignups: serverConfig.auth.disableSignups,disablePasswordAuth: serverConfig.auth.disablePasswordAuth,+ oauthAutoRedirect: serverConfig.auth.oauth.autoRedirect,},turnstile:serverConfig.auth.turnstile.enabled && serverConfig.auth.turnstile.siteKey