UNPKG

get-express-starter

Version:

Get production ready express boilerplate with a single command

61 lines (51 loc) 2.13 kB
const httpStatus = require('http-status'); const catchAsync = require('../utils/catchAsync'); const { authService, tokenService, emailService, otpService, userService } = 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 token = await tokenService.generateAuthTokens(user); res.send({ user, token }); }); const logout = catchAsync(async (req, res) => { await authService.logout(req.body.refreshToken); res.status(httpStatus.OK).send({ success: true, message: 'User Logout Successfully!' }); }); 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 refreshTokens = catchAsync(async (req, res) => { const tokens = await authService.refreshAuth(req.body.refreshToken); res.send({ ...tokens }); }); const resetPassword = catchAsync(async (req, res) => { const updatedUser = await authService.resetPassword(req.query.token, req.body.password); await emailService.sendPasswordRestSuccessEmail(updatedUser.email); res.status(httpStatus.NO_CONTENT).send(); }); const sendVerificationEmail = catchAsync(async (req, res) => { const otp = await otpService.generateAndSaveOtp(req.user.id); await emailService.sendVerificationEmail(req.user.email, otp); res.status(httpStatus.NO_CONTENT).send(); }); const verifyEmail = catchAsync(async (req, res) => { await otpService.verifyOtp(req.user.id, req.body.otp); res.status(httpStatus.NO_CONTENT).send(); }); module.exports = { register, login, logout, forgotPassword, refreshTokens, resetPassword, sendVerificationEmail, verifyEmail, };