Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add email validation to login and style lastUsed
Marc Seitz41896f96a642
diff --git a/app/(auth)/login/page.tsx b/app/(auth)/login/page.tsxindex 340b6361..2627697d 100644--- a/app/(auth)/login/page.tsx+++ b/app/(auth)/login/page.tsx@@ -9,22 +9,20 @@ import { signInWithPasskey } from "@teamhanko/passkeys-next-auth-provider/clientimport { signIn } from "next-auth/react";import { toast } from "sonner";+import { LastUsed, useLastUsed } from "@/components/hooks/useLastUsed";import Google from "@/components/shared/icons/google";import LinkedIn from "@/components/shared/icons/linkedin";import Passkey from "@/components/shared/icons/passkey";import { Button } from "@/components/ui/button";import { Input } from "@/components/ui/input";import { Label } from "@/components/ui/label";-import { LastUsed, useLastUsed } from "@/components/hooks/useLastUsed";-import { Loader } from "lucide-react";++import { cn } from "@/lib/utils";+import { simpleEmailRegex, validateEmail } from "@/lib/utils/validate-email";export default function Login() {const { next } = useParams as { next?: string };- const [isLoginWithEmail, setIsLoginWithEmail] = useState<boolean>(false);- const [isLoginWithGoogle, setIsLoginWithGoogle] = useState<boolean>(false);- const [isLoginWithLinkedIn, setIsLoginWithLinkedIn] =- useState<boolean>(false);const [lastUsed, setLastUsed] = useLastUsed();const authMethods = ["google", "email", "linkedin", "passkey"] as const;type AuthMethod = (typeof authMethods)[number];@@ -36,6 +34,7 @@ export default function Login() {"Continue with Email",);+ const isValidEmail = email.length > 0 && validateEmail(email);return (<div className="flex h-screen w-full flex-wrap">@@ -60,6 +59,8 @@ export default function Login() {className="flex flex-col gap-4 px-4 pt-8 sm:px-16"onSubmit={(e) => {e.preventDefault();+ if (!isValidEmail) return;+setClickedMethod("email");signIn("email", {email: email,@@ -68,24 +69,17 @@ export default function Login() {}).then((res) => {if (res?.ok && !res?.error) {setEmail("");- setLastUsed("credentials")+ setLastUsed("credentials");setEmailButtonText("Email sent - check your inbox!");toast.success("Email sent - check your inbox!");} else {setEmailButtonText("Error sending email - try again?");toast.error("Error sending email - try again?");}- // setIsLoginWithEmail(false);setClickedMethod(undefined);});}}>- {/* <Input- className="border-1 bg-white border-gray-200 hover:border-gray-200 text-gray-800"- placeholder="[email protected]"- value={email}- onChange={(e) => setEmail(e.target.value)}- /> */}<Label className="sr-only" htmlFor="email"></Label>@@ -97,28 +91,31 @@ export default function Login() {autoComplete="email"autoCorrect="off"disabled={clickedMethod === "email"}+ pattern={simpleEmailRegex.source}value={email}onChange={(e) => setEmail(e.target.value)}- className="flex h-10 w-full rounded-md border-0 bg-background bg-white px-3 py-2 text-sm text-gray-900 ring-1 ring-gray-200 transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 dark:bg-white"- />- {/* <Button type="submit" disabled={isLoginWithEmail}>- {isLoginWithEmail && (- <Loader className="h-5 w-5 mr-2 animate-spin bg-gray-800 hover:bg-gray-900" />+ className={cn(+ "flex h-10 w-full rounded-md border-0 bg-background bg-white px-3 py-2 text-sm text-gray-900 ring-1 ring-gray-200 transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 dark:bg-white",+ email.length > 0 && !isValidEmail+ ? "ring-red-500"+ : "ring-gray-200",)}- Continue with Email- </Button> */}+ /><div className="relative"><Buttontype="submit"loading={clickedMethod === "email"}- className={`${clickedMethod === "email"- ? "bg-black"- : "bg-gray-800 hover:bg-gray-900"- } w-full focus:shadow-outline transform rounded px-4 py-2 text-white transition-colors duration-300 ease-in-out focus:outline-none`}+ disabled={!isValidEmail || !!clickedMethod}+ className={cn(+ "focus:shadow-outline w-full transform rounded px-4 py-2 text-white transition-colors duration-300 ease-in-out focus:outline-none",+ clickedMethod === "email"+ ? "bg-black"+ : "bg-gray-800 hover:bg-gray-900",+ )}>{emailButtonText}- {lastUsed === "credentials" && <LastUsed />}</Button>+ {lastUsed === "credentials" && <LastUsed />}</div></form><p className="py-4 text-center">or</p>@@ -126,32 +123,32 @@ export default function Login() {<div className="relative"><ButtononClick={() => {- setLastUsed("google")- setIsLoginWithGoogle(true);+ setClickedMethod("google");+ setLastUsed("google");signIn("google", {...(next && next.length > 0 ? { callbackUrl: next } : {}),}).then((res) => {if (res?.status) {- setIsLoginWithGoogle(false);+ setClickedMethod(undefined);}});}}- disabled={isLoginWithGoogle}- className="w-full flex items-center justify-center space-x-2 border border-gray-200 bg-gray-100 font-normal text-gray-900 hover:bg-gray-200 "+ loading={clickedMethod === "google"}+ disabled={clickedMethod && clickedMethod !== "google"}+ className="flex w-full items-center justify-center space-x-2 border border-gray-200 bg-gray-100 font-normal text-gray-900 hover:bg-gray-200">- {isLoginWithGoogle ? (- <Loader className="mr-2 h-5 w-5 animate-spin" />- ) : (- <Google className="h-5 w-5" />- )}+ <Google className="h-5 w-5" /><span>Continue with Google</span>- {lastUsed === "google" && <LastUsed />}+ {clickedMethod !== "google" && lastUsed === "google" && (+ <LastUsed />+ )}</Button></div><div className="relative"><ButtononClick={() => {setClickedMethod("linkedin");+ setLastUsed("linkedin");signIn("linkedin", {...(next && next.length > 0 ? { callbackUrl: next } : {}),}).then((res) => {@@ -162,42 +159,53 @@ export default function Login() {}}loading={clickedMethod === "linkedin"}disabled={clickedMethod && clickedMethod !== "linkedin"}- className="w-full flex items-center justify-center space-x-2 border border-gray-200 bg-gray-100 font-normal text-gray-900 hover:bg-gray-200"+ className="flex w-full items-center justify-center space-x-2 border border-gray-200 bg-gray-100 font-normal text-gray-900 hover:bg-gray-200"><LinkedIn /><span>Continue with LinkedIn</span>- {lastUsed === "linkedin" && <LastUsed />}+ {clickedMethod !== "linkedin" && lastUsed === "linkedin" && (+ <LastUsed />+ )}</Button></div><div className="relative"><ButtononClick={() => {- setLastUsed("saml")+ setLastUsed("passkey");+ setClickedMethod("passkey");signInWithPasskey({tenantId: process.env.NEXT_PUBLIC_HANKO_TENANT_ID as string,- })- }- }+ });+ }}variant="outline"+ loading={clickedMethod === "passkey"}disabled={clickedMethod && clickedMethod !== "passkey"}- className="w-full flex items-center justify-center space-x-2 border border-gray-200 bg-gray-100 font-normal text-gray-900 hover:bg-gray-200 hover:text-gray-900"+ className="flex w-full items-center justify-center space-x-2 border border-gray-200 bg-gray-100 font-normal text-gray-900 hover:bg-gray-200 hover:text-gray-900"><Passkey className="h-4 w-4" /><span>Continue with a passkey</span>- {lastUsed === "saml" && <LastUsed />}+ {lastUsed === "passkey" && <LastUsed />}</Button></div></div><p className="mt-10 w-full max-w-md px-4 text-xs text-muted-foreground sm:px-16">By clicking continue, you acknowledge that you have read and agreeto Papermark's{" "}- <Link href="/terms" className="underline">+ <a+ href="https://www.papermark.io/terms"+ target="_blank"+ className="underline"+ >Terms of Service- </Link>{" "}+ </a>{" "}and{" "}- <Link href="/privacy" className="underline">+ <a+ href="https://www.papermark.io/privacy"+ target="_blank"+ className="underline"+ >Privacy Policy- </Link>+ </a>.</p></div>@@ -239,6 +247,6 @@ export default function Login() {</div></div></div>- </div >+ </div>);}diff --git a/components/hooks/useLastUsed.tsx b/components/hooks/useLastUsed.tsxindex 428961b2..94f3a349 100644--- a/components/hooks/useLastUsed.tsx+++ b/components/hooks/useLastUsed.tsx@@ -1,42 +1,45 @@-import { classNames } from "@/lib/utils";-import { localStorage } from "@/lib/webstorage";-import { useState, useEffect } from "react";-+"use client";+import { useEffect, useState } from "react";+import { cn } from "@/lib/utils";+import { localStorage } from "@/lib/webstorage";-type LoginType = "saml" | "google" | "credentials" | "linkedin";+type LoginType = "passkey" | "google" | "credentials" | "linkedin";export function useLastUsed() {- const [lastUsed, setLastUsed] = useState<LoginType>();-- useEffect(() => {- const storedValue = localStorage.getItem("last_papermark_login");- if (storedValue) {- setLastUsed(storedValue as LoginType);- }- }, []);-- useEffect(() => {- if (lastUsed) {- localStorage.setItem("last_papermark_login", lastUsed);- } else {- localStorage.removeItem("last_papermark_login");- }- }, [lastUsed]);-- return [lastUsed, setLastUsed] as const;+ const [lastUsed, setLastUsed] = useState<LoginType>();++ useEffect(() => {+ const storedValue = localStorage.getItem("last_papermark_login");+ if (storedValue) {+ setLastUsed(storedValue as LoginType);+ }+ }, []);++ useEffect(() => {+ if (lastUsed) {+ localStorage.setItem("last_papermark_login", lastUsed);+ } else {+ localStorage.removeItem("last_papermark_login");+ }+ }, [lastUsed]);++ return [lastUsed, setLastUsed] as const;}export const LastUsed = ({ className }: { className?: string | undefined }) => {- return (- <>- <div className="absolute left-11 sm:left-1 top-1/2 -translate-y-1/2 -translate-x-full">- <div className={`bg-[#333333] text-white text-xs py-1 px-2 rounded-md relative ${className} z-[999]`}>- Last used- <div className={`absolute -right-1 top-1/2 -translate-y-1/2 w-0 h-0 border-t-4 border-b-4 border-l-4 border-r-0 border-transparent border-l-[#333333] `}></div>- </div>- </div>- </>- );+ return (+ <div className="absolute right-2 top-1/2 w-fit -translate-y-1/2 sm:right-[-50px]">+ <div+ className={cn(+ "relative z-[999] rounded-md bg-input px-2 py-1 text-xs text-foreground",+ className,+ )}+ >+ Last used+ <div className="absolute -left-1 top-1/2 hidden h-0 w-0 -translate-y-1/2 border-b-4 border-l-0 border-r-4 border-t-4 border-transparent border-r-input sm:block" />+ </div>+ </div>+ );};