121 lines
3.3 KiB
JavaScript
121 lines
3.3 KiB
JavaScript
// userController.js
|
|
const catchAsync = require('../../globalErrorHandler/catchAsync')
|
|
const userService = require('./userService')
|
|
const AppError = require("../../globalErrorHandler/AppError");
|
|
|
|
exports.signup = catchAsync(async (req, res, next) => {
|
|
const { user, token, cookieOptions } = await userService.signup(
|
|
req.body.name,
|
|
req.body.email,
|
|
req.body.password,
|
|
req.body.passwordConfirm
|
|
)
|
|
res.cookie('jwt', token, cookieOptions)
|
|
res.status(200).json({ status: 'success ', data: { user, token } })
|
|
})
|
|
|
|
exports.loginUser = catchAsync(async (req, res, next) => {
|
|
const { email, password } = req.body
|
|
const { token, user } = await userService.loginUser(email, password)
|
|
const maxAge = 60 * 60 * 24 * 90
|
|
|
|
res.setHeader('Authorization', `Bearer ${token}`)
|
|
|
|
res.cookie('belonging_token', token, {
|
|
httpOnly: true,
|
|
maxAge: maxAge * 1000,
|
|
path: '/',
|
|
})
|
|
res.cookie('bm-token', token, {
|
|
httpOnly: false,
|
|
maxAge: maxAge * 1000,
|
|
path: '/',
|
|
})
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
data: { user, token },
|
|
})
|
|
})
|
|
|
|
exports.logoutUser = catchAsync(async (req, res, next) => {
|
|
const isProduction = process.env.NODE_ENV === 'production'
|
|
|
|
// Clear default host cookies
|
|
res.clearCookie('belonging_token', { path: '/' })
|
|
res.clearCookie('bm-token', { path: '/' })
|
|
res.clearCookie('jwt', { path: '/' })
|
|
|
|
// Clear domain cookies if in production or mbmanager domain
|
|
if (isProduction || req.hostname.includes('mbmanager.ir')) {
|
|
res.clearCookie('belonging_token', { domain: '.mbmanager.ir', path: '/' })
|
|
res.clearCookie('bm-token', { domain: '.mbmanager.ir', path: '/' })
|
|
res.clearCookie('jwt', { domain: '.mbmanager.ir', path: '/' })
|
|
}
|
|
|
|
res.cookie('belonging_token', '', { maxAge: 0, expires: new Date(0), path: '/' })
|
|
res.cookie('bm-token', '', { maxAge: 0, expires: new Date(0), path: '/' })
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
message: 'کاربر با موفقیت خارج شد',
|
|
})
|
|
})
|
|
|
|
exports.getAllUsers = catchAsync(async (req, res, next) => {
|
|
const users = await userService.getAllUsers(req.query)
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
results: users.length,
|
|
data: users,
|
|
})
|
|
})
|
|
|
|
|
|
exports.userGetSelf = catchAsync(async (req, res, next) => {
|
|
const user = req.user
|
|
if (!user) {
|
|
throw new AppError('NotLoggedIn', 403, {})
|
|
}
|
|
return res.status(200).json({ status: 'success ', data: { user } })
|
|
})
|
|
|
|
|
|
|
|
exports.getUser = catchAsync(async (req, res, next) => {
|
|
const user = await userService.getUser(req.params.userId)
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
data: user,
|
|
})
|
|
})
|
|
|
|
exports.createUser = catchAsync(async (req, res, next) => {
|
|
const user = await userService.createUser(req.body)
|
|
|
|
res.status(201).json({
|
|
status: 'success',
|
|
data: user,
|
|
})
|
|
})
|
|
|
|
exports.updateUser = catchAsync(async (req, res, next) => {
|
|
const user = await userService.updateUser(req.params.userId, req.body)
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
data: user,
|
|
})
|
|
})
|
|
|
|
exports.deleteUser = catchAsync(async (req, res, next) => {
|
|
await userService.deleteUser(req.params.userId)
|
|
|
|
res.status(204).json({
|
|
status: 'success',
|
|
data: null,
|
|
})
|
|
})
|