Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- router.post('/user/signup',
- AuthenticationController.signupWithCredentials)
- 'use strict'
- const { Router } = require('express')
- const { TestController } = require('./../controllers/TestController')
- const { AuthenticationController } = require('./../controllers/AuthenticationController')
- const { AuthMiddleware } = require('./../middleware/AuthMiddleware')
- const router = new Router()
- router.get('/test', TestController.test)
- router.post('/user/signup', AuthenticationController.signupWithCredentials)
- module.exports = router
- 'use strict'
- const uuid = require('uuid/v4')
- const bcrypt = require('bcrypt')
- const config = require('./../config')
- const { promiseEjs } = require('./../utils/promiseEjs')
- const { mailSender } = require('./../utils/mailSender')
- const { errorHandler } = require('./../utils/errorHandler')
- const { User } = require('./../schema/user')
- const { authMiddleWare } = require('./../middleware/AuthMiddleware')
- class AuthenticationController {
- /**
- * API 1.0 | POST
- * @example {
- * firstName: String,
- * lastName: String,
- * email: String,
- * password: String
- * }
- * @param {*} req
- * @param {*} res
- */
- static async signupWithCredentials (req, res) {
- try {
- let email = req.body.email
- let user = await User.findOne({ email: email })
- if (user) {
- throw {
- code: 400, message: 'User already exist'
- }
- }
- else {
- let emailVerificationToken = uuid()
- user = new User({
- firstName: req.body.firstName,
- lastName: req.body.lastName,
- email: req.body.email,
- password: bcrypt.hashSync(req.body.password, 8),
- emailVerificationToken: emailVerificationToken
- })
- await user.save()
- try {
- let emailVerificationLink = `${config.SERVER_DEV_URL}/authentication/verify?token=${emailVerificationToken}&user=${user._id}`
- if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'staging') { emailVerificationLink = `${config.SERVER_PRODUCTION_URL}/authentication/verify?token=${emailVerificationToken}&user=${user._id}` }
- let html = await promiseEjs.renderFile('./emails/signup.ejs', { user: user, emailVerificationLink: emailVerificationLink })
- await mailSender.sendMail(user.email, 'Welcome to HandyGems', html)
- }
- catch (error) {
- console.log(error)
- }
- let authToken = authMiddleWare.createJWT(user)
- await User.findOneAndUpdate({ _id: user._id }, { $set: { authToken: authToken } })
- res.send({ authToken: authToken })
- }
- }
- catch (error) {
- errorHandler.sendError(res, error)
- }
- }
- }
- const authenticationController = new AuthenticationController()
- module.exports = { authenticationController }
- class AuthenticationController {
- static async signupWithCredentials (req, res) {
- console.log('you are just calling static method')
- }
- withoutStaticMethod(req, res) {
- console.log('you are just calling non static method using object')
- }
- }
- module.exports = AuthenticationController
- const AuthenticationController = require('./AuthenticationController')
- router.get('/signup', AuthenticationController.signupWithCredentials)
- const obj = new AuthenticationController()
- router.get('/testing2', obj.withoutStaticMethod)
Add Comment
Please, Sign In to add comment