Spaces:
Sleeping
Sleeping
File size: 1,557 Bytes
18b71c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import { NextFunction, Request, Response } from "express";
import { authService } from "../services/auth.service";
import { AppError } from "../utils/appError";
const requireString = (value: unknown, field: string) => {
if (typeof value !== "string" || !value.trim()) {
throw new AppError(`${field} is required.`);
}
return value.trim();
};
export const authController = {
async signup(request: Request, response: Response, next: NextFunction) {
try {
const name = requireString(request.body.name, "Name");
const email = requireString(request.body.email, "Email");
const password = requireString(request.body.password, "Password");
const data = await authService.signup(name, email, password);
response.status(201).json({ success: true, data });
} catch (error) {
next(error);
}
},
async login(request: Request, response: Response, next: NextFunction) {
try {
const email = requireString(request.body.email, "Email");
const password = requireString(request.body.password, "Password");
const data = await authService.login(email, password);
response.json({ success: true, data });
} catch (error) {
next(error);
}
},
me(request: Request, response: Response, next: NextFunction) {
try {
if (!request.authUserId) {
throw new AppError("Unauthorized.", 401);
}
const data = authService.getMe(request.authUserId);
response.json({ success: true, data });
} catch (error) {
next(error);
}
},
};
|