nuxt-users
Version:
A comprehensive user management module for Nuxt 3 and Nuxt 4 applications with authentication, authorization, database support, and CLI tools
33 lines (32 loc) • 1.25 kB
JavaScript
import { defineEventHandler, readBody, createError, getHeader } from "h3";
import { sendPasswordResetLink } from "../../../services/password.js";
import { useRuntimeConfig } from "#imports";
export default defineEventHandler(async (event) => {
const { nuxtUsers } = useRuntimeConfig();
const options = nuxtUsers;
const body = await readBody(event);
const { email } = body;
if (!email || typeof email !== "string") {
throw createError({
statusCode: 400,
statusMessage: "Email is required and must be a string."
});
}
try {
const host = getHeader(event, "host");
const protocol = getHeader(event, "x-forwarded-proto") || "http";
const baseUrl = host ? `${protocol}://${host}` : void 0;
await sendPasswordResetLink(email, options, baseUrl);
return { message: "If a user with that email exists, a password reset link has been sent." };
} catch (error) {
if (error instanceof Error) {
console.error("[Nuxt Users] Error in forgot-password endpoint:", error.message);
} else {
console.error("[Nuxt Users] Error in forgot-password endpoint:", error);
}
throw createError({
statusCode: 500,
statusMessage: "An internal server error occurred."
});
}
});