-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add user, auth, token services
- Loading branch information
1 parent
a92dd01
commit 26736f7
Showing
7 changed files
with
146 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import httpStatus from 'http-status'; | ||
import Token from '../Tokens/token.model'; | ||
import ApiError from '../utils/ApiError'; | ||
import tokenTypes from '../Tokens/token.types'; | ||
import { getUserByEmail, getUserById, updateUserById } from '../Users/user.service'; | ||
import { IUserDoc } from '../Users/user.interfaces'; | ||
import { generateAuthTokens, verifyToken } from '../Tokens/token.service'; | ||
import { AccessAndRefreshTokens } from '../Tokens/token.interfaces'; | ||
|
||
/** | ||
* Login with username and password | ||
* @param {string} email | ||
* @param {string} password | ||
* @returns {Promise<IUserDoc>} | ||
*/ | ||
export const loginUserWithEmailAndPassword = async (email: string, password: string): Promise<IUserDoc> => { | ||
const user = await getUserByEmail(email); | ||
if (!user || !(await user.isPasswordMatch(password))) { | ||
throw new ApiError(httpStatus.UNAUTHORIZED, 'Incorrect email or password'); | ||
} | ||
return user; | ||
}; | ||
|
||
/** | ||
* Logout | ||
* @param {string} refreshToken | ||
* @returns {Promise<void>} | ||
*/ | ||
export const logout = async (refreshToken: string): Promise<void> => { | ||
const refreshTokenDoc = await Token.findOne({ token: refreshToken, type: tokenTypes.REFRESH, blacklisted: false }); | ||
if (!refreshTokenDoc) { | ||
throw new ApiError(httpStatus.NOT_FOUND, 'Not found'); | ||
} | ||
await refreshTokenDoc.remove(); | ||
}; | ||
|
||
/** | ||
* Refresh auth tokens | ||
* @param {string} refreshToken | ||
* @returns {Promise<AccessAndRefreshTokens>} | ||
*/ | ||
export const refreshAuth = async (refreshToken: string): Promise<AccessAndRefreshTokens> => { | ||
try { | ||
const refreshTokenDoc = await verifyToken(refreshToken, tokenTypes.REFRESH); | ||
const user = await getUserById(refreshTokenDoc.user); | ||
if (!user) { | ||
throw new Error(); | ||
} | ||
await refreshTokenDoc.remove(); | ||
return await generateAuthTokens(user); | ||
} catch (error) { | ||
throw new ApiError(httpStatus.UNAUTHORIZED, 'Please authenticate'); | ||
} | ||
}; | ||
|
||
/** | ||
* Reset password | ||
* @param {string} resetPasswordToken | ||
* @param {string} newPassword | ||
* @returns {Promise<void>} | ||
*/ | ||
export const resetPassword = async (resetPasswordToken: string, newPassword: string): Promise<void> => { | ||
try { | ||
const resetPasswordTokenDoc = await verifyToken(resetPasswordToken, tokenTypes.RESET_PASSWORD); | ||
const user = await getUserById(resetPasswordTokenDoc.user); | ||
if (!user) { | ||
throw new Error(); | ||
} | ||
await updateUserById(user.id, { password: newPassword }); | ||
await Token.deleteMany({ user: user.id, type: tokenTypes.RESET_PASSWORD }); | ||
} catch (error) { | ||
throw new ApiError(httpStatus.UNAUTHORIZED, 'Password reset failed'); | ||
} | ||
}; | ||
|
||
/** | ||
* Verify email | ||
* @param {string} verifyEmailToken | ||
* @returns {Promise<void>} | ||
*/ | ||
export const verifyEmail = async (verifyEmailToken: string): Promise<void> => { | ||
try { | ||
const verifyEmailTokenDoc = await verifyToken(verifyEmailToken, tokenTypes.VERIFY_EMAIL); | ||
const user = await getUserById(verifyEmailTokenDoc.user); | ||
if (!user) { | ||
throw new Error(); | ||
} | ||
await Token.deleteMany({ user: user.id, type: tokenTypes.VERIFY_EMAIL }); | ||
await updateUserById(user.id, { isEmailVerified: true }); | ||
} catch (error) { | ||
throw new ApiError(httpStatus.UNAUTHORIZED, 'Email verification failed'); | ||
} | ||
}; |
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
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