The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
display the last used method
Kiran K24165372b458
diff --git a/apps/web/app/app.dub.co/(auth)/login/form.tsx b/apps/web/app/app.dub.co/(auth)/login/form.tsxindex b8fc2d4d30..aafe9c4ce4 100644--- a/apps/web/app/app.dub.co/(auth)/login/form.tsx+++ b/apps/web/app/app.dub.co/(auth)/login/form.tsx@@ -1,6 +1,13 @@"use client";+import {+ AuthMethod,+ getLastUsedAuthMethod,+ LastUsedAuthMethodTooltip,+ setLastUsedAuthMethod,+} from "@/lib/auth/last-used-method";import { Button, Github, Google, InfoTooltip, useMediaQuery } from "@dub/ui";+import { Lock, Mail } from "lucide-react";import { signIn } from "next-auth/react";import Link from "next/link";import { useSearchParams } from "next/navigation";@@ -19,6 +26,14 @@ export default function LoginForm() {const [clickedEmail, setClickedEmail] = useState(false);const [clickedSSO, setClickedSSO] = useState(false);+ const [authMethod, setAuthMethod] = useState<AuthMethod | undefined>(+ undefined,+ );++ useEffect(() => {+ setAuthMethod(getLastUsedAuthMethod());+ }, []);+useEffect(() => {const error = searchParams?.get("error");error && toast.error(error);@@ -39,35 +54,48 @@ export default function LoginForm() {return (<><div className="flex flex-col gap-3">- <Button- variant="secondary"- onClick={() => {- setClickedGoogle(true);- signIn("google", {- ...(next && next.length > 0 ? { callbackUrl: next } : {}),- });- }}- loading={clickedGoogle}- disabled={clickedEmail || clickedSSO}- icon={<Google className="h-5 w-5" />}- />- <Button- variant="secondary"- onClick={() => {- setClickedGithub(true);- signIn("github", {- ...(next && next.length > 0 ? { callbackUrl: next } : {}),- });- }}- loading={clickedGithub}- disabled={clickedEmail || clickedSSO}- icon={<Github className="h-5 w-5 text-black" />}- />+ <div className="relative">+ <Button+ text="Continue with Google"+ variant="secondary"+ onClick={() => {+ setClickedGoogle(true);+ setLastUsedAuthMethod("google");+ signIn("google", {+ ...(next && next.length > 0 ? { callbackUrl: next } : {}),+ });+ }}+ loading={clickedGoogle}+ disabled={clickedEmail || clickedSSO}+ icon={<Google className="h-5 w-5" />}+ />+ {authMethod === "google" && <LastUsedAuthMethodTooltip />}+ </div>++ <div className="relative">+ <Button+ text="Continue with Github"+ variant="secondary"+ onClick={() => {+ setClickedGithub(true);+ setLastUsedAuthMethod("github");+ signIn("github", {+ ...(next && next.length > 0 ? { callbackUrl: next } : {}),+ });+ }}+ loading={clickedGithub}+ disabled={clickedEmail || clickedSSO}+ icon={<Github className="h-5 w-5 text-black" />}+ />+ {authMethod === "github" && <LastUsedAuthMethodTooltip />}+ </div></div>+<formonSubmit={async (e) => {e.preventDefault();setClickedEmail(true);+ setLastUsedAuthMethod("email");fetch("/api/auth/account-exists", {method: "POST",headers: { "Content-Type": "application/json" },@@ -122,25 +150,32 @@ export default function LoginForm() {/></div>)}- <Button- text="Continue with Email"- variant="secondary"- {...(!showEmailOption && {- type: "button",- onClick: (e) => {- e.preventDefault();- setShowSSOOption(false);- setShowEmailOption(true);- },- })}- loading={clickedEmail}- disabled={clickedGoogle || clickedSSO}- />++ <div className="relative">+ <Button+ text="Continue with Email"+ variant="secondary"+ icon={<Mail className="h-4 w-4" />}+ {...(!showEmailOption && {+ type: "button",+ onClick: (e) => {+ e.preventDefault();+ setLastUsedAuthMethod("email");+ setShowSSOOption(false);+ setShowEmailOption(true);+ },+ })}+ loading={clickedEmail}+ disabled={clickedGoogle || clickedSSO}+ />+ {authMethod === "email" && <LastUsedAuthMethodTooltip />}+ </div></form><formonSubmit={async (e) => {e.preventDefault();setClickedSSO(true);+ setLastUsedAuthMethod("saml");fetch("/api/auth/saml/verify", {method: "POST",body: JSON.stringify({ slug: e.currentTarget.slug.value }),@@ -182,20 +217,25 @@ export default function LoginForm() {/></div>)}- <Button- text="Continue with SAML SSO"- variant="secondary"- {...(!showSSOOption && {- type: "button",- onClick: (e) => {- e.preventDefault();- setShowEmailOption(false);- setShowSSOOption(true);- },- })}- loading={clickedSSO}- disabled={clickedGoogle || clickedEmail}- />++ <div className="relative">+ <Button+ text="Continue with SAML SSO"+ variant="secondary"+ icon={<Lock className="h-4 w-4" />}+ {...(!showSSOOption && {+ type: "button",+ onClick: (e) => {+ e.preventDefault();+ setShowEmailOption(false);+ setShowSSOOption(true);+ },+ })}+ loading={clickedSSO}+ disabled={clickedGoogle || clickedEmail}+ />+ {authMethod === "saml" && <LastUsedAuthMethodTooltip />}+ </div></form>{noSuchAccount ? (<p className="text-center text-sm text-red-500">diff --git a/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx b/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsxindex e8c588cf46..4b938bc101 100644--- a/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx+++ b/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx@@ -1,11 +1,17 @@"use client";+import {+ AuthMethod,+ getLastUsedAuthMethod,+ LastUsedAuthMethodTooltip,+ setLastUsedAuthMethod,+} from "@/lib/auth/last-used-method";import z from "@/lib/zod";import { signInSchema } from "@/lib/zod/schemas/auth";import { Button, Input } from "@dub/ui";import { signIn } from "next-auth/react";import { useRouter, useSearchParams } from "next/navigation";-import { useState } from "react";+import { useEffect, useState } from "react";import { useForm } from "react-hook-form";import { toast } from "sonner";@@ -22,6 +28,14 @@ export const SignInWithEmailPassword = () => {const searchParams = useSearchParams();const [error, setError] = useState<string | null>(null);+ const [authMethod, setAuthMethod] = useState<AuthMethod | undefined>(+ undefined,+ );++ useEffect(() => {+ setAuthMethod(getLastUsedAuthMethod());+ }, []);+const {register,resetField,@@ -47,6 +61,7 @@ export const SignInWithEmailPassword = () => {}if (response.url) {+ setLastUsedAuthMethod("password");toast.success("Redirecting...");router.replace(response.url);return;@@ -87,12 +102,16 @@ export const SignInWithEmailPassword = () => {required{...register("password")}/>- <Button- text="Sign in"- type="submit"- loading={isSubmitting}- disabled={disabled}- />++ <div className="relative">+ <Button+ text="Sign in"+ type="submit"+ loading={isSubmitting}+ disabled={disabled}+ />+ {authMethod === "password" && <LastUsedAuthMethodTooltip />}+ </div></div></form></>diff --git a/apps/web/lib/auth/last-used-method.tsx b/apps/web/lib/auth/last-used-method.tsxnew file mode 100644index 0000000000..1490c390cc--- /dev/null+++ b/apps/web/lib/auth/last-used-method.tsx@@ -0,0 +1,26 @@+// Store the last used auth method in localStorage+const LAST_USED_AUTH_METHOD = "lastUsedAuthMethod";++export type AuthMethod = "google" | "github" | "email" | "saml" | "password";++export const setLastUsedAuthMethod = (method: AuthMethod) => {+ if (typeof window === "undefined") return;++ localStorage.setItem(LAST_USED_AUTH_METHOD, method);+};++export const getLastUsedAuthMethod = () => {+ if (typeof window === "undefined") return;++ return localStorage.getItem(LAST_USED_AUTH_METHOD) as AuthMethod | undefined;+};++export const LastUsedAuthMethodTooltip = () => {+ return (+ <>+ <span className="absolute -left-20 top-1/2 -translate-y-1/2 rounded bg-gray-800 px-2 py-1 text-xs text-white">+ Last used+ </span>+ </>+ );+};