init
This commit is contained in:
59
src/controllers/auth.controller.js
Normal file
59
src/controllers/auth.controller.js
Normal file
@@ -0,0 +1,59 @@
|
||||
const httpStatus = require('http-status');
|
||||
const catchAsync = require('../utils/catchAsync');
|
||||
const { authService, userService, tokenService, emailService } = require('../services');
|
||||
|
||||
const register = catchAsync(async (req, res) => {
|
||||
const user = await userService.createUser(req.body);
|
||||
const tokens = await tokenService.generateAuthTokens(user);
|
||||
res.status(httpStatus.CREATED).send({ user, tokens });
|
||||
});
|
||||
|
||||
const login = catchAsync(async (req, res) => {
|
||||
const { email, password } = req.body;
|
||||
const user = await authService.loginUserWithEmailAndPassword(email, password);
|
||||
const tokens = await tokenService.generateAuthTokens(user);
|
||||
res.send({ user, tokens });
|
||||
});
|
||||
|
||||
const logout = catchAsync(async (req, res) => {
|
||||
await authService.logout(req.body.refreshToken);
|
||||
res.status(httpStatus.NO_CONTENT).send();
|
||||
});
|
||||
|
||||
const refreshTokens = catchAsync(async (req, res) => {
|
||||
const tokens = await authService.refreshAuth(req.body.refreshToken);
|
||||
res.send({ ...tokens });
|
||||
});
|
||||
|
||||
const forgotPassword = catchAsync(async (req, res) => {
|
||||
const resetPasswordToken = await tokenService.generateResetPasswordToken(req.body.email);
|
||||
await emailService.sendResetPasswordEmail(req.body.email, resetPasswordToken);
|
||||
res.status(httpStatus.NO_CONTENT).send();
|
||||
});
|
||||
|
||||
const resetPassword = catchAsync(async (req, res) => {
|
||||
await authService.resetPassword(req.query.token, req.body.password);
|
||||
res.status(httpStatus.NO_CONTENT).send();
|
||||
});
|
||||
|
||||
const sendVerificationEmail = catchAsync(async (req, res) => {
|
||||
const verifyEmailToken = await tokenService.generateVerifyEmailToken(req.user);
|
||||
await emailService.sendVerificationEmail(req.user.email, verifyEmailToken);
|
||||
res.status(httpStatus.NO_CONTENT).send();
|
||||
});
|
||||
|
||||
const verifyEmail = catchAsync(async (req, res) => {
|
||||
await authService.verifyEmail(req.query.token);
|
||||
res.status(httpStatus.NO_CONTENT).send();
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
register,
|
||||
login,
|
||||
logout,
|
||||
refreshTokens,
|
||||
forgotPassword,
|
||||
resetPassword,
|
||||
sendVerificationEmail,
|
||||
verifyEmail,
|
||||
};
|
||||
2
src/controllers/index.js
Normal file
2
src/controllers/index.js
Normal file
@@ -0,0 +1,2 @@
|
||||
module.exports.authController = require('./auth.controller');
|
||||
module.exports.userController = require('./user.controller');
|
||||
43
src/controllers/user.controller.js
Normal file
43
src/controllers/user.controller.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const httpStatus = require('http-status');
|
||||
const pick = require('../utils/pick');
|
||||
const ApiError = require('../utils/ApiError');
|
||||
const catchAsync = require('../utils/catchAsync');
|
||||
const { userService } = require('../services');
|
||||
|
||||
const createUser = catchAsync(async (req, res) => {
|
||||
const user = await userService.createUser(req.body);
|
||||
res.status(httpStatus.CREATED).send(user);
|
||||
});
|
||||
|
||||
const getUsers = catchAsync(async (req, res) => {
|
||||
const filter = pick(req.query, ['name', 'role']);
|
||||
const options = pick(req.query, ['sortBy', 'limit', 'page']);
|
||||
const result = await userService.queryUsers(filter, options);
|
||||
res.send(result);
|
||||
});
|
||||
|
||||
const getUser = catchAsync(async (req, res) => {
|
||||
const user = await userService.getUserById(req.params.userId);
|
||||
if (!user) {
|
||||
throw new ApiError(httpStatus.NOT_FOUND, 'User not found');
|
||||
}
|
||||
res.send(user);
|
||||
});
|
||||
|
||||
const updateUser = catchAsync(async (req, res) => {
|
||||
const user = await userService.updateUserById(req.params.userId, req.body);
|
||||
res.send(user);
|
||||
});
|
||||
|
||||
const deleteUser = catchAsync(async (req, res) => {
|
||||
await userService.deleteUserById(req.params.userId);
|
||||
res.status(httpStatus.NO_CONTENT).send();
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
createUser,
|
||||
getUsers,
|
||||
getUser,
|
||||
updateUser,
|
||||
deleteUser,
|
||||
};
|
||||
Reference in New Issue
Block a user