forked from hagopj13/node-express-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
137 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const httpStatus = require('http-status'); | ||
const tokenService = require('./token.service'); | ||
const userService = require('./user.service'); | ||
const Token = require('../models/token.model'); | ||
const ApiError = require('../utils/ApiError'); | ||
|
||
const loginUserWithEmailAndPassword = async (email, password) => { | ||
const user = await userService.getUserByEmail(email); | ||
if (!user || !(await user.isPasswordMatch(password))) { | ||
throw new ApiError(httpStatus.UNAUTHORIZED, 'Incorrect email or password'); | ||
} | ||
return user; | ||
}; | ||
|
||
const refreshAuth = async (refreshToken) => { | ||
try { | ||
const refreshTokenDoc = await tokenService.verifyToken(refreshToken, 'refresh'); | ||
const user = await userService.getUserById(refreshTokenDoc.user); | ||
if (!user) { | ||
throw new Error(); | ||
} | ||
await refreshTokenDoc.remove(); | ||
return tokenService.generateAuthTokens(user); | ||
} catch (error) { | ||
throw new ApiError(httpStatus.UNAUTHORIZED, 'Please authenticate'); | ||
} | ||
}; | ||
|
||
const resetPassword = async (resetPasswordToken, newPassword) => { | ||
try { | ||
const resetPasswordTokenDoc = await tokenService.verifyToken(resetPasswordToken, 'resetPassword'); | ||
const user = await userService.getUserById(resetPasswordTokenDoc.user); | ||
if (!user) { | ||
throw new Error(); | ||
} | ||
await Token.deleteMany({ user: user.id, type: 'resetPassword' }); | ||
await userService.updateUserById(user.id, { password: newPassword }); | ||
} catch (error) { | ||
throw new ApiError(httpStatus.UNAUTHORIZED, 'Password reset failed'); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
loginUserWithEmailAndPassword, | ||
refreshAuth, | ||
resetPassword, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
module.exports.authService = require('./auth.service'); | ||
module.exports.emailService = require('./email.service'); | ||
module.exports.tokenService = require('./token.service'); | ||
module.exports.userService = require('./user.service'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const httpStatus = require('http-status'); | ||
const { User } = require('../models'); | ||
const ApiError = require('../utils/ApiError'); | ||
|
||
const createUser = async (userBody) => { | ||
if (await User.isEmailTaken(userBody.email)) { | ||
throw new ApiError(httpStatus.BAD_REQUEST, 'Email already taken'); | ||
} | ||
const user = await User.create(userBody); | ||
return user; | ||
}; | ||
|
||
const getUsers = async (filter, options) => { | ||
const users = await User.find(filter, null, options); | ||
return users; | ||
}; | ||
|
||
const getUserById = async (id) => { | ||
return User.findById(id); | ||
}; | ||
|
||
const getUserByEmail = async (email) => { | ||
return User.findOne({ email }); | ||
}; | ||
|
||
const updateUserById = async (userId, updateBody) => { | ||
const user = await getUserById(userId); | ||
if (!user) { | ||
throw new ApiError(httpStatus.NOT_FOUND, 'User not found'); | ||
} | ||
if (updateBody.email && (await User.isEmailTaken(updateBody.email, userId))) { | ||
throw new ApiError(httpStatus.BAD_REQUEST, 'Email already taken'); | ||
} | ||
Object.assign(user, updateBody); | ||
await user.save(); | ||
return user; | ||
}; | ||
|
||
const deleteUserById = async (userId) => { | ||
const user = await getUserById(userId); | ||
if (!user) { | ||
throw new ApiError(httpStatus.NOT_FOUND, 'User not found'); | ||
} | ||
await user.remove(); | ||
return user; | ||
}; | ||
|
||
module.exports = { | ||
createUser, | ||
getUsers, | ||
getUserById, | ||
getUserByEmail, | ||
updateUserById, | ||
deleteUserById, | ||
}; |