-
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.
- Loading branch information
1 parent
09f0b1a
commit 50e6ffa
Showing
8 changed files
with
70 additions
and
110 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 |
---|---|---|
@@ -1,63 +1,52 @@ | ||
import httpStatus from 'http-status'; | ||
import { Request, Response } from 'express'; | ||
import catchAsync from '../utils/catchAsync'; | ||
import { registerUser } from '../user/user.service'; | ||
import { | ||
generateAuthTokens, | ||
generateResetPasswordToken, | ||
generateVerifyEmailToken, | ||
} from '../token/token.service'; | ||
import { loginUserWithEmailAndPassword, logout, refreshAuth, resetPassword, verifyEmail } from './auth.service'; | ||
import { sendResetPasswordEmail, sendVerificationEmail } from '../email/email.service'; | ||
import config from '../../config/config'; | ||
import { AccessAndRefreshTokens } from '../token/token.interfaces'; | ||
|
||
export const sendTokens = (res: Response, tokens: AccessAndRefreshTokens) => { | ||
res.cookie('accessToken', tokens.access, config.jwt.cookieOptions); | ||
res.cookie('refreshToken', tokens.refresh, config.jwt.cookieOptions); | ||
}; | ||
|
||
export const registerController = catchAsync(async (req: Request, res: Response) => { | ||
const user = await registerUser(req.body); | ||
const tokens = await generateAuthTokens(user); | ||
import * as tokenService from '@/modules/token/token.service'; | ||
import * as userService from '@/modules/user/user.service'; | ||
import * as authService from '@/modules/auth/auth.service'; | ||
import * as emailService from '@/modules/email/email.service'; | ||
|
||
export const register = catchAsync(async (req: Request, res: Response) => { | ||
const user = await userService.registerUser(req.body); | ||
const tokens = await tokenService.generateAuthTokens(user); | ||
res.status(httpStatus.CREATED).send({ user, tokens }); | ||
}); | ||
|
||
export const loginController = catchAsync(async (req: Request, res: Response) => { | ||
export const login = catchAsync(async (req: Request, res: Response) => { | ||
const { email, password } = req.body; | ||
const user = await loginUserWithEmailAndPassword(email, password); | ||
const tokens = await generateAuthTokens(user); | ||
const user = await authService.loginUserWithEmailAndPassword(email, password); | ||
const tokens = await tokenService.generateAuthTokens(user); | ||
res.send({ user, tokens }); | ||
}); | ||
|
||
export const logoutController = catchAsync(async (req: Request, res: Response) => { | ||
await logout(req.body.refreshToken); | ||
export const logout = catchAsync(async (req: Request, res: Response) => { | ||
await authService.logout(req.body.refreshToken); | ||
res.status(httpStatus.NO_CONTENT).send(); | ||
}); | ||
|
||
export const refreshTokensController = catchAsync(async (req: Request, res: Response) => { | ||
const tokens = await refreshAuth(req.body.refreshToken); | ||
export const refreshTokens = catchAsync(async (req: Request, res: Response) => { | ||
const tokens = await authService.refreshAuth(req.body.refreshToken); | ||
res.send({ ...tokens }); | ||
}); | ||
|
||
export const forgotPasswordController = catchAsync(async (req: Request, res: Response) => { | ||
const resetPasswordToken = await generateResetPasswordToken(req.body.email); | ||
await sendResetPasswordEmail(req.body.email, resetPasswordToken); | ||
export const forgotPassword = catchAsync(async (req: Request, res: Response) => { | ||
const resetPasswordToken = await tokenService.generateResetPasswordToken(req.body.email); | ||
await emailService.sendResetPasswordEmail(req.body.email, resetPasswordToken); | ||
res.status(httpStatus.NO_CONTENT).send(); | ||
}); | ||
|
||
export const resetPasswordController = catchAsync(async (req: Request, res: Response) => { | ||
await resetPassword(req.query['token'], req.body.password); | ||
export const resetPassword = catchAsync(async (req: Request, res: Response) => { | ||
await authService.resetPassword(req.query['token'], req.body.password); | ||
res.status(httpStatus.NO_CONTENT).send(); | ||
}); | ||
|
||
export const sendVerificationEmailController = catchAsync(async (req: Request, res: Response) => { | ||
const verifyEmailToken = await generateVerifyEmailToken(req.user); | ||
await sendVerificationEmail(req.user.email, verifyEmailToken, req.user.name); | ||
export const sendVerificationEmail = catchAsync(async (req: Request, res: Response) => { | ||
const verifyEmailToken = await tokenService.generateVerifyEmailToken(req.user); | ||
await emailService.sendVerificationEmail(req.user.email, verifyEmailToken, req.user.name); | ||
res.status(httpStatus.NO_CONTENT).send(); | ||
}); | ||
|
||
export const verifyEmailController = catchAsync(async (req: Request, res: Response) => { | ||
await verifyEmail(req.query['token']); | ||
export const verifyEmail = catchAsync(async (req: Request, res: Response) => { | ||
await authService.verifyEmail(req.query['token']); | ||
res.status(httpStatus.NO_CONTENT).send(); | ||
}); |
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
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