Files
Palmr/apps/server/src/modules/auth/controller.ts
Daniel Luiz Alves 75d6049b87 feat: enhance pre-push validation and update ESLint configurations
- Updated the Husky pre-push hook to validate all applications (web, docs, and server) before pushing changes, improving code quality checks.
- Modified ESLint configurations for the docs app to include additional ignored directories, ensuring cleaner linting results.
- Refactored the HomePage component in the docs app to improve structure and readability, while reintroducing the Highlight component for better content presentation.
- Added a .prettierignore file in the server app to exclude specific directories from formatting, enhancing development workflow.
- Updated various import statements across multiple files for consistency and clarity.
2025-07-02 14:53:23 -03:00

78 lines
2.5 KiB
TypeScript

import { FastifyReply, FastifyRequest } from "fastify";
import { env } from "../../env";
import { createResetPasswordSchema, LoginSchema, RequestPasswordResetSchema } from "./dto";
import { AuthService } from "./service";
export class AuthController {
private authService = new AuthService();
async login(request: FastifyRequest, reply: FastifyReply) {
try {
const input = LoginSchema.parse(request.body);
const user = await this.authService.login(input);
const token = await request.jwtSign({
userId: user.id,
isAdmin: user.isAdmin,
});
reply.setCookie("token", token, {
httpOnly: true,
path: "/",
secure: env.SECURE_SITE === "true" ? true : false,
sameSite: env.SECURE_SITE === "true" ? "lax" : "strict",
});
return reply.send({ user });
} catch (error: any) {
return reply.status(400).send({ error: error.message });
}
}
async logout(request: FastifyRequest, reply: FastifyReply) {
reply.clearCookie("token", { path: "/" });
return reply.send({ message: "Logout successful" });
}
async requestPasswordReset(request: FastifyRequest, reply: FastifyReply) {
try {
const { email, origin } = RequestPasswordResetSchema.parse(request.body);
await this.authService.requestPasswordReset(email, origin);
return reply.send({
message: "If an account exists with this email, a password reset link will be sent.",
});
} catch (error: any) {
return reply.status(400).send({ error: error.message });
}
}
async resetPassword(request: FastifyRequest, reply: FastifyReply) {
try {
const schema = await createResetPasswordSchema();
const input = schema.parse(request.body);
await this.authService.resetPassword(input.token, input.password);
return reply.send({ message: "Password reset successfully" });
} catch (error: any) {
return reply.status(400).send({ error: error.message });
}
}
async getCurrentUser(request: FastifyRequest, reply: FastifyReply) {
try {
const userId = (request as any).user?.userId;
if (!userId) {
return reply.status(401).send({ error: "Unauthorized: a valid token is required to access this resource." });
}
const user = await this.authService.getUserById(userId);
if (!user) {
return reply.status(404).send({ error: "User not found" });
}
return reply.send({ user });
} catch (error: any) {
return reply.status(400).send({ error: error.message });
}
}
}