Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

feat: integrate input-otp component for OTP verification

Andrej Acevski70a80dd91007
diff --git a/apps/api/src/auth.ts b/apps/api/src/auth.ts
index 13bad8f1a..7d4b671fd 100644
--- a/apps/api/src/auth.ts
+++ b/apps/api/src/auth.ts
@@ -140,7 +140,7 @@ export const auth = betterAuth({
emailOTP({
async sendVerificationOTP({ email, otp, type }) {
if (type === "sign-in") {
- await sendOtpEmail(email, "Your Kaneo verification code", { otp });
+ await sendOtpEmail(email, "Authentication code for Kaneo", { otp });
}
},
}),
diff --git a/apps/web/package.json b/apps/web/package.json
index 8fe7b50b1..4b2f1ed00 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -54,6 +54,7 @@
"framer-motion": "^12.23.26",
"hono": "^4.11.3",
"immer": "^11.1.0",
+ "input-otp": "^1.4.2",
"lucide-react": "^0.562.0",
"react": "^19.2.0",
"react-day-picker": "9.13.0",
diff --git a/apps/web/src/components/ui/input-otp.tsx b/apps/web/src/components/ui/input-otp.tsx
new file mode 100644
index 000000000..48092af24
--- /dev/null
+++ b/apps/web/src/components/ui/input-otp.tsx
@@ -0,0 +1,76 @@
+import { OTPInput, OTPInputContext } from "input-otp";
+import * as React from "react";
+
+import { cn } from "@/lib/cn";
+
+const InputOTP = React.forwardRef<
+ React.ElementRef<typeof OTPInput>,
+ React.ComponentPropsWithoutRef<typeof OTPInput>
+>(({ className, containerClassName, ...props }, ref) => (
+ <OTPInput
+ ref={ref}
+ containerClassName={cn(
+ "flex items-center gap-2 has-[:disabled]:opacity-50",
+ containerClassName,
+ )}
+ className={cn("disabled:cursor-not-allowed", className)}
+ {...props}
+ />
+));
+InputOTP.displayName = "InputOTP";
+
+const InputOTPGroup = React.forwardRef<
+ React.ElementRef<"div">,
+ React.ComponentPropsWithoutRef<"div">
+>(({ className, ...props }, ref) => (
+ <div ref={ref} className={cn("flex items-center", className)} {...props} />
+));
+InputOTPGroup.displayName = "InputOTPGroup";
+
+const InputOTPSlot = React.forwardRef<
+ React.ElementRef<"div">,
+ React.ComponentPropsWithoutRef<"div"> & { index: number }
+>(({ index, className, ...props }, ref) => {
+ const inputOTPContext = React.useContext(OTPInputContext);
+ const { char, hasFakeCaret, isActive } = inputOTPContext.slots[index];
+
+ return (
+ <div
+ ref={ref}
+ className={cn(
+ "relative flex h-9 w-9 items-center justify-center border-y border-r border-input text-sm shadow-sm transition-all first:rounded-l-md first:border-l last:rounded-r-md",
+ isActive && "z-10 ring-1 ring-ring",
+ className,
+ )}
+ {...props}
+ >
+ {char}
+ {hasFakeCaret && (
+ <div className="pointer-events-none absolute inset-0 flex items-center justify-center">
+ <div className="h-4 w-px animate-caret-blink bg-foreground duration-1000" />
+ </div>
+ )}
+ </div>
+ );
+});
+InputOTPSlot.displayName = "InputOTPSlot";
+
+const InputOTPSeparator = React.forwardRef<
+ React.ElementRef<"div">,
+ React.ComponentPropsWithoutRef<"div">
+>(({ className, ...props }, ref) => (
+ // biome-ignore lint/a11y/useFocusableInteractive: we don't need to focus on the separator
+ // biome-ignore lint/a11y/useSemanticElements: we use a div to wrap the separator
+ <div
+ ref={ref}
+ // biome-ignore lint/a11y/useAriaPropsForRole: we use a div to wrap the separator
+ role="separator"
+ className={cn("flex items-center justify-center", className)}
+ {...props}
+ >
+ <div className="h-1 w-1 mx-2 rounded-full bg-muted-foreground" />
+ </div>
+));
+InputOTPSeparator.displayName = "InputOTPSeparator";
+
+export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator };
diff --git a/apps/web/src/routes/auth/verify-otp.tsx b/apps/web/src/routes/auth/verify-otp.tsx
index e5bcce659..ecb6fe72a 100644
--- a/apps/web/src/routes/auth/verify-otp.tsx
+++ b/apps/web/src/routes/auth/verify-otp.tsx
@@ -1,6 +1,7 @@
import { standardSchemaResolver } from "@hookform/resolvers/standard-schema";
import { createFileRoute, useRouter, useSearch } from "@tanstack/react-router";
-import { useState } from "react";
+import { REGEXP_ONLY_DIGITS } from "input-otp";
+import { useCallback, useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod/v4";
@@ -14,7 +15,11 @@ import {
FormLabel,
FormMessage,
} from "@/components/ui/form";
-import { Input } from "@/components/ui/input";
+import {
+ InputOTP,
+ InputOTPGroup,
+ InputOTPSlot,
+} from "@/components/ui/input-otp";
import { authClient } from "@/lib/auth-client";
import { AuthLayout } from "../../components/auth/layout";
@@ -42,33 +47,45 @@ function VerifyOtp() {
defaultValues: { otp: "" },
});
- const onSubmit = async (data: VerifyOtpFormValues) => {
- setIsPending(true);
- try {
- const result = await authClient.signIn.emailOtp({
- email,
- otp: data.otp,
- });
+ const onSubmit = useCallback(
+ async (data: VerifyOtpFormValues) => {
+ setIsPending(true);
+ try {
+ const result = await authClient.signIn.emailOtp({
+ email,
+ otp: data.otp,
+ });
- if (result.error) {
- toast.error(result.error.message || "Invalid verification code");
- return;
+ if (result.error) {
+ toast.error(result.error.message || "Invalid verification code");
+ return;
+ }
+
+ toast.success("Signed in successfully!");
+ if (invitationId) {
+ history.push(`/invitation/accept/${invitationId}`);
+ } else {
+ history.push("/dashboard");
+ }
+ } catch (error) {
+ toast.error(
+ error instanceof Error ? error.message : "Failed to verify code",
+ );
+ } finally {
+ setIsPending(false);
}
+ },
+ [email, invitationId, history],
+ );
- toast.success("Signed in successfully!");
- if (invitationId) {
- history.push(`/invitation/accept/${invitationId}`);
- } else {
- history.push("/dashboard");
+ useEffect(() => {
+ const subscription = form.watch((value, { name }) => {
+ if (name === "otp" && value.otp?.length === 6 && !isPending) {
+ form.handleSubmit(onSubmit)();
}
- } catch (error) {
- toast.error(
- error instanceof Error ? error.message : "Failed to verify code",
- );
- } finally {
- setIsPending(false);
- }
- };
+ });
+ return () => subscription.unsubscribe();
+ }, [form, isPending, onSubmit]);
const handleResendOtp = async () => {
setIsPending(true);
@@ -117,16 +134,22 @@ function VerifyOtp() {
Verification Code
</FormLabel>
<FormControl>
- <Input
- placeholder="123456"
- type="text"
- inputMode="numeric"
- pattern="[0-9]*"
+ <InputOTP
maxLength={6}
- autoComplete="one-time-code"
- className="text-center text-2xl tracking-[0.5em] font-mono"
- {...field}
- />
+ value={field.value}
+ onChange={field.onChange}
+ onBlur={field.onBlur}
+ pattern={REGEXP_ONLY_DIGITS}
+ >
+ <InputOTPGroup className="w-full justify-center items-center">
+ <InputOTPSlot className="size-10" index={0} />
+ <InputOTPSlot className="size-10" index={1} />
+ <InputOTPSlot className="size-10" index={2} />
+ <InputOTPSlot className="size-10" index={3} />
+ <InputOTPSlot className="size-10" index={4} />
+ <InputOTPSlot className="size-10" index={5} />
+ </InputOTPGroup>
+ </InputOTP>
</FormControl>
<FormMessage>{fieldState.error?.message}</FormMessage>
</FormItem>
diff --git a/packages/email/src/templates/otp.tsx b/packages/email/src/templates/otp.tsx
index 79e521b25..cab46c64d 100644
--- a/packages/email/src/templates/otp.tsx
+++ b/packages/email/src/templates/otp.tsx
@@ -23,15 +23,12 @@ const OtpEmail = ({ otp }: OtpEmailProps) => (
<Body style={main}>
<Preview>Your verification code for Kaneo</Preview>
<Container style={container}>
- <Heading style={heading}>Your verification code</Heading>
+ <Heading style={heading}>
+ <strong>Your login code for Kaneo is:</strong>
+ </Heading>
<Section style={body}>
- <Text style={paragraph}>
- Enter this one-time code to sign in to Kaneo:
- </Text>
<Text style={codeStyle}>{otp}</Text>
- <Text style={paragraph}>
- This code will only be valid for the next 5 minutes.
- </Text>
+ <Text style={paragraph}>The code is valid for 15 minutes.</Text>
<Text style={paragraph}>
If you didn't request this, please ignore this email.
</Text>
@@ -69,19 +66,19 @@ const body = {
};
const paragraph = {
- fontSize: "16px",
- lineHeight: "26px",
+ fontSize: "14px",
+ lineHeight: "1.6",
+ margin: "12px 0",
+ color: "#666",
};
const codeStyle = {
- fontSize: "32px",
- fontWeight: "bold",
- letterSpacing: "6px",
+ fontSize: "30px",
+ fontWeight: "bold" as const,
+ letterSpacing: "4px",
textAlign: "center" as const,
- margin: "24px 0",
- padding: "16px",
- backgroundColor: "#f4f4f5",
- borderRadius: "8px",
+ margin: "12px 0",
+ color: "#333",
};
const footerParagraph = {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 6bd1e618a..4238365dd 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -338,6 +338,9 @@ importers:
immer:
specifier: ^11.1.0
version: 11.1.3
+ input-otp:
+ specifier: ^1.4.2
lucide-react:
specifier: ^0.562.0
version: 0.562.0([email protected])
@@ -4557,6 +4560,12 @@ packages:
resolution: {integrity: sha512-Nb2ctOyNR8DqQoR0OwRG95uNWIC0C1lCgf5Naz5H6Ji72KZ8OcFZLz2P5sNgwlyoJ8Yif11oMuYs5pBQa86csA==}
+ resolution: {integrity: sha512-l3jWwYNvrEa6NTCt7BECfCm48GvwuZzkoeG3gBL2w4CHeOXW3eKFmf9UNYkNfYc3mxMrthMnxjIE07MT0zLBQA==}
+ peerDependencies:
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0.0 || ^19.0.0-rc
+ react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0.0 || ^19.0.0-rc
+
resolution: {integrity: sha512-FWyyY60MeTNyeSRpkM2Iry0G9hpr7/9kD40mD/cGQEuilcZYS4okz8SN2Q6rLCJ8gbCt6fN+rC+6tMGS99LaxQ==}
@@ -10672,6 +10681,11 @@ snapshots:
+ dependencies:
+ react: 19.2.3
+ react-dom: 19.2.3([email protected])
+