[GH-ISSUE #372] email OTP sendVerificationOTP() doesn't work properly #8241

Closed
opened 2026-04-13 03:20:10 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @ywyher on GitHub (Oct 30, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/372

Describe the bug
in the docs it says to use this function and write our own logic to use the api we created to send emails

import { betterAuth } from "better-auth"
 
export const auth = betterAuth({
    plugins: [
        emailOTP({
            async sendVerificationOTP({
                email,
                otp,
                type
            }) {
                if(type === "email-verfication") {
                    // Send the OTP for email-verfication
                }
            },
        })
    ]
})

so i assumed that i should create my own api with the email provider i want, i did so using resend
when and added the logic to send the POST request

async sendVerificationOTP({ email, otp, type }) {
        if (type === "email-verification") {
                try {
                        const response = await fetch(`/api/email/otp/send?email=${email}&otp=${otp}`, {
                                method: 'POST',
                                headers: {
                                'Content-Type': 'application/json'
                                },
                                body: JSON.stringify({ email, otp })
                        });

                        if (!response.ok) {
                                throw new Error(`Error: ${response.statusText}`);
                        }

                        const data = await response.json();
                        return data;
                } catch (error) {
                        console.error("Failed to send OTP:", error);
                        throw error;
                }
        }
}

then used

await authClient.emailOtp.sendVerificationOtp({
    email: "user-email@email.com",
    type: "email-verification"
})

i assumed that it should use the code i wrote inside of sendVerificationOTP(){}

but it didn't work nor did it use the custom api route i created which was /api/email/otp/send.

in the console i found it saying http://localhost:3000/api/email-otp/send-verfication-otp error code 500
so i created this route and added the same code from my custom route /api/email/otp/send and it worked even when i deleted the code inside of sendVerficationOTP(){} to call the post request, so i guess that i got its own logic without needing to write one inside of sendVerficationOTP(){}

my issue with it that it gives an option to customize which email to send to, but it doesnt send the OTP, nor is there an optin to send an OTP inside of emailOtp.sendVerificationOtp(){}, how is the user supposed to insert the OTP to verify himself if i he doesnt recive it, how am i supposed to write my own logic to be able to send the OTP ?

Screenshots
image

Desktop (please complete the following information):

  • OS: Arch Linux
  • Browser: Firefox
  • Framework: Nextjs
Originally created by @ywyher on GitHub (Oct 30, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/372 **Describe the bug** in the docs it says to use this function and write our own logic to use the api we created to send emails ```tsx import { betterAuth } from "better-auth" export const auth = betterAuth({ plugins: [ emailOTP({ async sendVerificationOTP({ email, otp, type }) { if(type === "email-verfication") { // Send the OTP for email-verfication } }, }) ] }) ``` so i assumed that i should create my own api with the email provider i want, i did so using resend when and added the logic to send the POST request ```tsx async sendVerificationOTP({ email, otp, type }) { if (type === "email-verification") { try { const response = await fetch(`/api/email/otp/send?email=${email}&otp=${otp}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, otp }) }); if (!response.ok) { throw new Error(`Error: ${response.statusText}`); } const data = await response.json(); return data; } catch (error) { console.error("Failed to send OTP:", error); throw error; } } } ``` then used ```tsx await authClient.emailOtp.sendVerificationOtp({ email: "user-email@email.com", type: "email-verification" }) ``` i assumed that it should use the code i wrote inside of `sendVerificationOTP(){}` but it didn't work nor did it use the custom api route i created which was `/api/email/otp/send`. in the console i found it saying `http://localhost:3000/api/email-otp/send-verfication-otp` error code 500 so i created this route and added the same code from my custom route `/api/email/otp/send` and it worked even when i deleted the code inside of `sendVerficationOTP(){}` to call the post request, so i guess that i got its own logic without needing to write one inside of `sendVerficationOTP(){}` my issue with it that it gives an option to customize which email to send to, but it doesnt send the OTP, nor is there an optin to send an OTP inside of `emailOtp.sendVerificationOtp(){}`, how is the user supposed to insert the OTP to verify himself if i he doesnt recive it, how am i supposed to write my own logic to be able to send the OTP ? **Screenshots** ![image](https://github.com/user-attachments/assets/2d8d28aa-fd53-43e5-a4fa-20d92ff43372) **Desktop (please complete the following information):** - OS: Arch Linux - Browser: Firefox - Framework: Nextjs
GiteaMirror added the locked label 2026-04-13 03:20:10 -05:00
Author
Owner

@ywyher commented on GitHub (Oct 30, 2024):

the issue turned out to be that i was calling fetch(/api/email/otp/send?email=${email}&otp=${otp}) instead of fetch(http://localhost:300/api/email/otp/send?email=${email}&otp=${otp})

based

<!-- gh-comment-id:2448056698 --> @ywyher commented on GitHub (Oct 30, 2024): the issue turned out to be that i was calling fetch(`/api/email/otp/send?email=${email}&otp=${otp}`) instead of fetch(`http://localhost:300/api/email/otp/send?email=${email}&otp=${otp}`) based
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8241