import Resend from '@auth/core/providers/resend'; import { Resend as ResendAPI } from 'resend'; import { RandomReader, generateRandomString } from '@oslojs/crypto/random'; export const ResendPasswordOTP = Resend({ id: 'resend-otp', apiKey: process.env.AUTH_RESEND_KEY, async generateVerificationToken() { console.log('Generating verification token...'); const random: RandomReader = { read(bytes) { crypto.getRandomValues(bytes); }, }; const length = 6; const alphabet = '0123456789'; const token = generateRandomString(random, alphabet, length); console.log('Generated token:', token); return token; }, async sendVerificationRequest({ identifier: email, provider, token }) { console.log('Sending verification email to:', email, 'with token:', token); if (!provider.apiKey) { console.error('Missing AUTH_RESEND_KEY environment variable'); throw new Error('Email service not configured'); } const resend = new ResendAPI(provider.apiKey); try { const { data, error } = await resend.emails.send({ from: 'BNA UI ', to: [email], subject: `Sign in to BNA UI`, text: `Your verification code is: ${token}`, html: `

Sign in to BNA UI

Your verification code is:

${token}

This code will expire in 10 minutes.

If you didn't request this code, please ignore this email.

`, }); if (error) { console.error('Resend API error:', error); throw new Error(`Could not send email: ${error.message}`); } console.log('Email sent successfully:', data); } catch (error) { console.error('Failed to send verification email:', error); throw new Error('Could not send verification email'); } }, });