Guest User

Untitled

a guest
Oct 22nd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. const Joi = require('joi');
  2. const HttpStatus = require('http-status-codes');
  3. const bcrypt = require('bcryptjs');
  4. const jwt = require('jsonwebtoken');
  5.  
  6. const User = require('../models/userModels');
  7.  
  8. const Helpers = require('../helpers/helpers');
  9. const dbConfig = require('../config/secret');
  10.  
  11. module.exports = {
  12. async CreateUser(req, res) {
  13. const schema = Joi.object().keys({
  14. username: Joi.string().min(5).max(15).required(),
  15. email: Joi.string().email().required(),
  16. password: Joi.string().min(5).max(15).required()
  17. });
  18.  
  19. const {
  20. error,
  21. value
  22. } = Joi.validate(req.body, schema);
  23.  
  24. if (error && error.details) {
  25. return res.status(HttpStatus.BAD_REQUEST).json({
  26. message: error.details
  27. })
  28. }
  29.  
  30. const userEmail = await User.findOne({
  31. email: Helpers.lowerCase(req.body.email)
  32. });
  33.  
  34. if (userEmail) {
  35. return res.status(HttpStatus.CONFLICT).json({
  36. message: 'Email already exist'
  37. });
  38. }
  39.  
  40. const userName = await User.findOne({
  41. username: Helpers.firstUpper(req.body.username)
  42. });
  43.  
  44. if (userName) {
  45. return res.status(HttpStatus.CONFLICT).json({
  46. message: 'Username alreay exists'
  47. });
  48. }
  49.  
  50. return bcrypt.hash(value.password, 10, (err, hash) => {
  51. if (err) {
  52. return res.status(HttpStatus.CONFLICT).json({
  53. message: 'Error hashing password'
  54. });
  55. }
  56.  
  57. const body = {
  58. username: Helpers.firstUpper(value.username),
  59. email: Helpers.lowerCase(value.email),
  60. password: hash
  61. };
  62.  
  63. User.create(body).then((user) => {
  64. const token = jwt.sign({
  65. data: user
  66. }, dbConfig.secret, {
  67. expiresIn: 120
  68. });
  69.  
  70. res.cookie('auth', token);
  71.  
  72. res.status(HttpStatus.CREATED).json({
  73. message: 'User created successfully',
  74. user,
  75. token
  76. });
  77. }).catch(error => {
  78. res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
  79. message: 'Error ocurred ' + error
  80. });
  81. });
  82. })
  83. }
  84. };
  85.  
  86. const express = require('express');
  87. const router = express.Router();
  88.  
  89. const AuthController = require('../controllers/auth');
  90.  
  91. router.post('/register', AuthController.CreateUser);
  92.  
  93. module.exports = router;
  94.  
  95. const express = require('express');
  96. const mongoose = require('mongoose');
  97. const cookieParser = require('cookie-parser');
  98. const cors = require('cors');
  99. //const logger = require('morgan');
  100.  
  101. const app = express();
  102.  
  103. app.use(cors());
  104.  
  105. const dbConfig = require('./config/secret');
  106.  
  107. app.use((req, res, next) => {
  108. res.header('Access-Control-Allow-Origin', '*');
  109. res.header('Access-Control-Allow-Credentials', 'true');
  110. res.header('Access-Control-Allow-Methods', 'GET', 'POST', 'DELETE', 'PUT', 'OPTIONS');
  111. res.header(
  112. 'Access-Control-Allow-Headers',
  113. 'Origin, X-Requested-Width, Content-Type, Accept, Authorization'
  114. );
  115. next();
  116. });
  117.  
  118. app.use(express.json({
  119. limit: '50mb'
  120. }));
  121.  
  122. app.use(express.urlencoded({
  123. extended: true,
  124. limit: '50mb'
  125. }));
  126.  
  127. app.use(cookieParser);
  128.  
  129. //app.use(logger('dev'));
  130.  
  131. mongoose.Promise = global.Promise;
  132.  
  133. mongoose.connect(dbConfig.url, {
  134. useNewUrlParser: true
  135. });
  136.  
  137. const auth = require('./routes/authRoutes');
  138.  
  139. app.use('/api/chatapp', auth); // For example http://localhost:3000/api/chatapp/register;
  140.  
  141. app.listen(3000, () => {
  142. console.log('Running on port 3000');
  143. });
Add Comment
Please, Sign In to add comment