Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
✨ Show the last used login method on the login page (#3458)
Luke Vella381a9288dc55
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.jsonindex d6ff57be4..7459bdcb9 100644--- a/apps/web/public/locales/en/app.json+++ b/apps/web/public/locales/en/app.json@@ -506,6 +506,7 @@"languageAndRegionDescription": "Set your language, time zone, and date and time formats","languageSettingDescription": "The language the app is displayed in.","lastUsedAt": "Last used {date}",+ "lastUsedLoginMethod": "Last used","learnMore": "Learn more","leaveSpace": "Leave space","leaveSpaceDescription": "Remove yourself from this space.",diff --git a/apps/web/src/app/[locale]/(auth)/login/components/login-email-form.tsx b/apps/web/src/app/[locale]/(auth)/login/components/login-email-form.tsxindex b668a03b1..2145a437b 100644--- a/apps/web/src/app/[locale]/(auth)/login/components/login-email-form.tsx+++ b/apps/web/src/app/[locale]/(auth)/login/components/login-email-form.tsx@@ -18,6 +18,7 @@ import * as z from "zod";import { setVerificationEmail } from "@/app/[locale]/(auth)/login/actions";import { Link } from "@/components/link";import { Turnstile } from "@/components/turnstile";+import { LastUsedBadge } from "@/features/auth/components/last-used-badge";import { Trans, useTranslation } from "@/i18n/client";import { authClient } from "@/lib/auth-client";import { useFeatureFlag } from "@/lib/feature-flags/client";@@ -40,8 +41,10 @@ function useLoginWithEmailSchema() {export function LoginWithEmailForm({isRegistrationEnabled,+ isLastUsed,}: {isRegistrationEnabled: boolean;+ isLastUsed?: boolean;}) {const isCaptchaEnabled = useFeatureFlag("captcha");const isTurnstileEnabled = isCaptchaEnabled && !!turnstileSiteKey;@@ -294,7 +297,7 @@ export function LoginWithEmailForm({size="xl"loading={form.formState.isSubmitting}type="submit"- className="w-full"+ className="relative w-full"variant="primary">{showPasswordField ? (@@ -312,6 +315,7 @@ export function LoginWithEmailForm({defaults="Continue with email"/>)}+ {isLastUsed ? <LastUsedBadge /> : null}</Button></div></form>diff --git a/apps/web/src/app/[locale]/(auth)/login/page.tsx b/apps/web/src/app/[locale]/(auth)/login/page.tsxindex 851038383..db65df051 100644--- a/apps/web/src/app/[locale]/(auth)/login/page.tsx+++ b/apps/web/src/app/[locale]/(auth)/login/page.tsx@@ -1,5 +1,6 @@import { absoluteUrl } from "@rallly/utils/absolute-url";import type { Metadata } from "next";+import { cookies } from "next/headers";import { Trans } from "react-i18next/TransWithoutContext";import { OIDCAutoSignIn } from "@/app/[locale]/(auth)/login/components/oidc-auto-sign-in";import { env } from "@/env";@@ -9,6 +10,7 @@ import { SSOProvider } from "@/features/auth/components/sso-provider";import { getRegistrationEnabled } from "@/features/instance-settings/data";import { getTranslation } from "@/i18n/server";import { authLib, getSessionState } from "@/lib/auth";+import { LAST_LOGIN_METHOD_COOKIE_NAME } from "@/lib/auth-config";import { isFeatureEnabled } from "@/lib/feature-flags/server";import { AlreadyLoggedIn } from "../components/already-logged-in";import {@@ -22,13 +24,15 @@ import { AuthErrors } from "./components/auth-errors";import { LoginWithEmailForm } from "./components/login-email-form";async function loadData() {- const [isRegistrationEnabled, { t, i18n }] = await Promise.all([+ const [isRegistrationEnabled, { t, i18n }, cookieStore] = await Promise.all([getRegistrationEnabled(),getTranslation(),+ cookies(),]);return {isRegistrationEnabled,+ lastLoginMethod: cookieStore.get(LAST_LOGIN_METHOD_COOKIE_NAME)?.value,t,i18n,};@@ -54,7 +58,7 @@ export default async function LoginPage(props: {return <AlreadyLoggedIn redirectTo={searchParams?.redirectTo} />;}- const { isRegistrationEnabled, t, i18n } = await loadData();+ const { isRegistrationEnabled, lastLoginMethod, t, i18n } = await loadData();const isEmailLoginEnabled = isFeatureEnabled("emailLogin");const hasGoogleProvider = !!authLib.options.socialProviders.google;@@ -67,6 +71,16 @@ export default async function LoginPage(props: {const hasAlternateLoginMethods = hasSocialLogin || hasOidc;+ // With a single method on offer there is nothing to choose between.+ const loginMethodCount = [+ isEmailLoginEnabled,+ hasOidc,+ hasGoogleProvider,+ hasMicrosoftProvider,+ ].filter(Boolean).length;+ const isLastUsed = (method: string) =>+ loginMethodCount > 1 && lastLoginMethod === method;+const hasError = !!searchParams?.error;const shouldAutoSignIn =@@ -120,7 +134,10 @@ export default async function LoginPage(props: {</AuthPageHeader><AuthPageContent>{isEmailLoginEnabled && (- <LoginWithEmailForm isRegistrationEnabled={isRegistrationEnabled} />+ <LoginWithEmailForm+ isRegistrationEnabled={isRegistrationEnabled}+ isLastUsed={isLastUsed("email")}+ />)}{isEmailLoginEnabled && hasAlternateLoginMethods ? <OrDivider /> : null}<div className="grid gap-3">@@ -128,6 +145,7 @@ export default async function LoginPage(props: {<LoginWithOIDCname={env.OIDC_NAME}redirectTo={searchParams?.redirectTo}+ isLastUsed={isLastUsed("oidc")}/>) : null}{hasGoogleProvider ? (@@ -135,6 +153,7 @@ export default async function LoginPage(props: {providerId="google"name="Google"redirectTo={searchParams?.redirectTo}+ isLastUsed={isLastUsed("google")}/>) : null}{hasMicrosoftProvider ? (@@ -142,6 +161,7 @@ export default async function LoginPage(props: {providerId="microsoft"name="Microsoft"redirectTo={searchParams?.redirectTo}+ isLastUsed={isLastUsed("microsoft")}/>) : null}</div>diff --git a/apps/web/src/features/auth/components/last-used-badge.tsx b/apps/web/src/features/auth/components/last-used-badge.tsxnew file mode 100644index 000000000..21ae8c674--- /dev/null+++ b/apps/web/src/features/auth/components/last-used-badge.tsx@@ -0,0 +1,17 @@+"use client";+import { cn } from "@rallly/ui";+import { badgeVariants } from "@rallly/ui/badge";+import { Trans } from "@/i18n/client";++export function LastUsedBadge() {+ return (+ <span+ className={cn(+ badgeVariants({ variant: "secondary", size: "sm" }),+ "absolute -top-2.5 right-3 border bg-popover text-muted-foreground",+ )}+ >+ <Trans i18nKey="lastUsedLoginMethod" defaults="Last used" />+ </span>+ );+}diff --git a/apps/web/src/features/auth/components/login-with-oidc.tsx b/apps/web/src/features/auth/components/login-with-oidc.tsxindex 45d7f3780..45658581b 100644--- a/apps/web/src/features/auth/components/login-with-oidc.tsx+++ b/apps/web/src/features/auth/components/login-with-oidc.tsx@@ -1,15 +1,18 @@"use client";import { Button } from "@rallly/ui/button";+import { LastUsedBadge } from "@/features/auth/components/last-used-badge";import { authClient } from "@/lib/auth-client";import { validateRedirectUrl } from "@/lib/utils/redirect";export function LoginWithOIDC({name,redirectTo,+ isLastUsed,}: {name: string;redirectTo?: string;+ isLastUsed?: boolean;}) {return (<Button@@ -20,10 +23,11 @@ export function LoginWithOIDC({errorCallbackURL: "/login",});}}- className="w-full"+ className="relative w-full"size="xl">{name}+ {isLastUsed ? <LastUsedBadge /> : null}</Button>);}diff --git a/apps/web/src/features/auth/components/sso-provider.tsx b/apps/web/src/features/auth/components/sso-provider.tsxindex 12afc0171..766c58594 100644--- a/apps/web/src/features/auth/components/sso-provider.tsx+++ b/apps/web/src/features/auth/components/sso-provider.tsx@@ -2,6 +2,7 @@import { Button } from "@rallly/ui/button";import { UserIcon } from "lucide-react";import Image from "next/image";+import { LastUsedBadge } from "@/features/auth/components/last-used-badge";import { authClient } from "@/lib/auth-client";import { validateRedirectUrl } from "@/lib/utils/redirect";@@ -42,14 +43,17 @@ export function SSOProvider({providerId,name,redirectTo,+ isLastUsed,}: {providerId: string;name: string;redirectTo?: string;+ isLastUsed?: boolean;}) {return (<Buttonsize="xl"+ className="relative"key={providerId}onClick={() => {authClient.signIn.social({@@ -61,6 +65,7 @@ export function SSOProvider({><SSOImage provider={providerId} /><span>{name}</span>+ {isLastUsed ? <LastUsedBadge /> : null}</Button>);}diff --git a/apps/web/src/lib/auth-client.ts b/apps/web/src/lib/auth-client.tsindex babc59e40..ba07d4705 100644--- a/apps/web/src/lib/auth-client.ts+++ b/apps/web/src/lib/auth-client.ts@@ -9,6 +9,7 @@ import {} from "better-auth/client/plugins";import { createAuthClient } from "better-auth/react";import type { Auth } from "@/lib/auth";+import { LAST_LOGIN_METHOD_COOKIE_NAME } from "@/lib/auth-config";import { clearBrowserQueryCache } from "@/lib/query-client";export const authClient = createAuthClient({@@ -17,7 +18,7 @@ export const authClient = createAuthClient({inferAdditionalFields<Auth>(),emailOTPClient(),genericOAuthClient(),- lastLoginMethodClient(),+ lastLoginMethodClient({ cookieName: LAST_LOGIN_METHOD_COOKIE_NAME }),anonymousClient(),],});diff --git a/apps/web/src/lib/auth-config.ts b/apps/web/src/lib/auth-config.tsindex 5309551d1..d8945fb93 100644--- a/apps/web/src/lib/auth-config.ts+++ b/apps/web/src/lib/auth-config.ts@@ -2,3 +2,6 @@// (e.g. the orphaned-guest cleanup) can't drift. The cleanup window must// stay >= this value or its liveness guarantee breaks.export const SESSION_TTL_SECONDS = 60 * 60 * 24 * 60; // 60 days++export const LAST_LOGIN_METHOD_COOKIE_NAME =+ "better-auth.last_used_login_method";diff --git a/apps/web/src/lib/auth.ts b/apps/web/src/lib/auth.tsindex 51ddbbbbd..88fda82c1 100644--- a/apps/web/src/lib/auth.ts+++ b/apps/web/src/lib/auth.ts@@ -43,7 +43,10 @@ import {parseRefParam,REF_COOKIE_NAME,} from "@/lib/acquisition";-import { SESSION_TTL_SECONDS } from "@/lib/auth-config";+import {+ LAST_LOGIN_METHOD_COOKIE_NAME,+ SESSION_TTL_SECONDS,+} from "@/lib/auth-config";import { hostOnlyCookieCleanup } from "@/lib/auth-plugins/host-only-cookie-cleanup";import { redis } from "@/lib/kv";import {@@ -201,6 +204,14 @@ export const authLib = betterAuth({}),lastLoginMethod({storeInDatabase: true,+ cookieName: LAST_LOGIN_METHOD_COOKIE_NAME,+ // The default resolver only knows password sign-in as "email"; both+ // OTP verification paths are the same email login to the user.+ customResolveMethod: (ctx) =>+ ctx.path === "/sign-in/email-otp" ||+ ctx.path === "/email-otp/verify-email"+ ? "email"+ : null,}),emailOTP({// The "sign-in" OTP type creates an account for an unknown address; the