Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(auth): switch to OTP based password reset instead of magic link (#6898)

Marc Klingen31b230d3e03e
diff --git a/packages/shared/src/server/services/email/passwordReset/sendResetPasswordVerificationRequest.tsx b/packages/shared/src/server/services/email/passwordReset/sendResetPasswordVerificationRequest.tsx
index 34b3de3b79..2a09c378d0 100644
--- a/packages/shared/src/server/services/email/passwordReset/sendResetPasswordVerificationRequest.tsx
+++ b/packages/shared/src/server/services/email/passwordReset/sendResetPasswordVerificationRequest.tsx
@@ -5,7 +5,6 @@
import * as React from "react";
import {
Body,
- Button,
Container,
Head,
Heading,
@@ -21,11 +20,11 @@ import { render } from "@react-email/render";
import { type SendVerificationRequestParams } from "next-auth/providers/email";
interface ResetPasswordTemplateProps {
- url: string;
+ token: string;
}
-const ResetPasswordTemplate = ({ url }: ResetPasswordTemplateProps) => {
- const previewText = "Reset your Langfuse password";
+const ResetPasswordTemplate = ({ token }: ResetPasswordTemplateProps) => {
+ const previewText = "Your Langfuse reset code";
return (
<Html>
<Head />
@@ -48,17 +47,14 @@ const ResetPasswordTemplate = ({ url }: ResetPasswordTemplateProps) => {
It happens to the best of us.
</Heading>
<Section className="mb-8 mt-8 text-center">
- <Button
- className="rounded bg-black px-5 py-3 text-center text-xs font-semibold text-white no-underline"
- href={url}
- >
- Reset your password
- </Button>
+ <Text className="text-center text-sm font-semibold">
+ Your one time passcode:
+ </Text>
+ <Heading className="text-3xl mt-2">{token}</Heading>
</Section>
<Text className="text-center text-xs leading-6 text-[#666666]">
- The link is valid for 10 minutes. If you do not want to change
- your password or didn&apos;t request a reset, you can ignore and
- delete this email.
+ This code is valid for 3 minutes. If you did not request a reset,
+ you can ignore this email.
</Text>
</Container>
</Body>
@@ -68,16 +64,16 @@ const ResetPasswordTemplate = ({ url }: ResetPasswordTemplateProps) => {
};
export async function sendResetPasswordVerificationRequest(
- params: SendVerificationRequestParams
+ params: SendVerificationRequestParams,
) {
- const { identifier, url, provider } = params;
+ const { identifier, token, provider } = params as SendVerificationRequestParams & { token: string };
const transport = createTransport(provider.server);
- const htmlTemplate = render(<ResetPasswordTemplate url={url} />);
+ const htmlTemplate = render(<ResetPasswordTemplate token={token} />);
const result = await transport.sendMail({
to: identifier,
from: provider.from,
- subject: `Forgot your password?`,
- text: `To reset your Langfuse password, please confirm your email:\n${url}\n\nThe link is valid for 10 minutes. If you do not want to change your password or didn't request a reset, you can ignore and delete this email.`,
+ subject: `Your Langfuse password reset code`,
+ text: `Use the following code to reset your Langfuse password: ${token}\n\nThis code will expire in 3 minutes. If you did not request a reset, you can ignore this email.`,
html: htmlTemplate,
});
const failed = result.rejected.concat(result.pending).filter(Boolean);
diff --git a/web/src/features/auth-credentials/components/ResetPasswordButton.tsx b/web/src/features/auth-credentials/components/ResetPasswordButton.tsx
index 9c16edaca2..b059f4f009 100644
--- a/web/src/features/auth-credentials/components/ResetPasswordButton.tsx
+++ b/web/src/features/auth-credentials/components/ResetPasswordButton.tsx
@@ -1,5 +1,6 @@
import { signIn, useSession } from "next-auth/react";
import { Button } from "@/src/components/ui/button";
+import { Input } from "@/src/components/ui/input";
import { useEffect, useState } from "react";
import { z } from "zod";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
@@ -15,6 +16,7 @@ export function RequestResetPasswordEmailButton({
variant?: "default" | "secondary";
}) {
const [isEmailSent, setIsEmailSent] = useState(false);
+ const [code, setCode] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [isValidEmail, setIsValidEmail] = useState(false);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
@@ -54,21 +56,66 @@ export function RequestResetPasswordEmailButton({
}
};
+ const handleVerify = async (_e: React.MouseEvent<HTMLButtonElement>) => {
+ if (!code) return;
+ setIsLoading(true);
+ setErrorMessage(null);
+ try {
+ const formattedEmail = encodeURIComponent(email.toLowerCase().trim());
+ const formattedCode = encodeURIComponent(code.trim());
+ const callback = encodeURIComponent(
+ `${env.NEXT_PUBLIC_BASE_PATH ?? ""}/auth/reset-password`,
+ );
+ const url = `/api/auth/callback/email?email=${formattedEmail}&token=${formattedCode}&callbackUrl=${callback}`;
+ window.location.href = url;
+ } catch (error) {
+ console.error("Error verifying code:", error);
+ setErrorMessage("An unexpected error occurred. Please try again.");
+ } finally {
+ setIsLoading(false);
+ }
+ };
+
return (
<>
- <Button
- onClick={handleResetPassword}
- className={className}
- loading={isLoading}
- disabled={isEmailSent || !isValidEmail}
- variant={variant}
- >
- {isEmailSent
- ? "Email sent. Please check your inbox"
- : session.status === "authenticated"
+ {isEmailSent ? (
+ <div>
+ <label htmlFor="otp-code" className="mb-2 block text-sm font-medium">
+ Check your inbox for the code
+ </label>
+ <Input
+ id="otp-code"
+ type="number"
+ minLength={6}
+ maxLength={6}
+ value={code}
+ onChange={(e) => setCode(e.target.value)}
+ placeholder="One time passcode"
+ className="mb-8 w-full"
+ />
+ <Button
+ onClick={handleVerify}
+ className={className}
+ loading={isLoading}
+ disabled={!code || code.length !== 6}
+ variant={variant}
+ >
+ Verify code
+ </Button>
+ </div>
+ ) : (
+ <Button
+ onClick={handleResetPassword}
+ className={className}
+ loading={isLoading}
+ disabled={!isValidEmail}
+ variant={variant}
+ >
+ {session.status === "authenticated"
? "Verify email to change password"
: "Request password reset"}
- </Button>
+ </Button>
+ )}
{errorMessage && (
<div className="mt-3 text-center text-sm text-destructive">
{errorMessage}
diff --git a/web/src/server/auth.ts b/web/src/server/auth.ts
index 70a0ea774f..e43cfc6984 100644
--- a/web/src/server/auth.ts
+++ b/web/src/server/auth.ts
@@ -24,6 +24,7 @@ import GitHubProvider from "next-auth/providers/github";
import GitLabProvider from "next-auth/providers/gitlab";
import OktaProvider from "next-auth/providers/okta";
import EmailProvider from "next-auth/providers/email";
+import { randomInt } from "crypto";
import Auth0Provider from "next-auth/providers/auth0";
import CognitoProvider from "next-auth/providers/cognito";
import AzureADProvider from "next-auth/providers/azure-ad";
@@ -169,7 +170,10 @@ if (env.SMTP_CONNECTION_URL && env.EMAIL_FROM_ADDRESS) {
EmailProvider({
server: env.SMTP_CONNECTION_URL,
from: env.EMAIL_FROM_ADDRESS,
- maxAge: 60 * 10, // 10 minutes
+ maxAge: 3 * 60, // 3 minutes
+ async generateVerificationToken() {
+ return randomInt(100000, 1000000).toString();
+ },
sendVerificationRequest: sendResetPasswordVerificationRequest,
}),
);