Guest User

Untitled

a guest
Oct 27th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. const express = require('express')
  2. const mongoose = require('mongoose')
  3. const Schema = mongoose.Schema
  4.  
  5. let UserSchema = new Schema ({
  6. username: {
  7. type: String,
  8. unique: [true, "This username is taken."],
  9. trim: true
  10. },
  11. email: {
  12. type: String,
  13. unique: [true, "This email is already being used."],
  14. trim: true
  15. },
  16. password: {
  17. type: String,
  18. trim: true
  19. }
  20. }, {
  21. timestamps: true,
  22. favorites: []
  23. })
  24.  
  25. // first parameter is the name of the collection! If does not exist, will be created!
  26. const User = mongoose.model('users', UserSchema)
  27. module.exports = User
  28.  
  29. router.post('/signup/users', (req, res, next) => {
  30. req.checkBody('username', 'Username field cannot be empty.').notEmpty()
  31. req.checkBody('username', 'Username must be between 4-30 characters long.').len(4, 30)
  32. req.checkBody('email', 'The email you entered is invalid, please try again.').isEmail()
  33. req.checkBody('email', 'Email address must be between 4-100 characters long, please try again.').len(4, 100)
  34. req.checkBody('password', 'Password must be between 8-100 characters long.').len(8, 100)
  35. // req.checkBody('password', 'Password must include one lowercase character, one uppercase character, a number, and a special character.').matches(/^(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?!.* )(?=.*[^a-zA-Z0-9]).{8,}$/, 'i')
  36. req.checkBody('passwordMatch', 'Password must be between 8-100 characters long.').len(8, 100)
  37. req.checkBody('passwordMatch', 'Passwords do not match, please try again.').equals(req.body.password)
  38.  
  39. // Additional validation to ensure username is alphanumeric with underscores and dashes
  40. req.checkBody('username', 'Username can only contain letters, numbers, or underscores.').matches(/^[A-Za-z0-9_-]+$/, 'i')
  41.  
  42. const errors = req.validationErrors()
  43.  
  44. if(errors) {
  45. res.render('signup', {
  46. errors: errors
  47. })
  48. } else {
  49. let password = req.body.password
  50. bcrypt.hash(password, saltRounds, (err, hash) => {
  51. user = new User()
  52. user.username = req.body.username
  53. user.email = req.body.email
  54. user.password = hash
  55.  
  56. user.save((err, result) => {
  57. if(err) {
  58. console.log(err)
  59. } else {
  60. User.find({}).sort({ _id:-1 }).limit(1)
  61. .exec((err, newuser) => {
  62. if (err) {
  63. console.log(err)
  64. } else {
  65. userId = ObjectId(newuser.id)
  66.  
  67. // logins user through passport function
  68. req.login(userId, (err) => {
  69. if (err) {
  70. console.log("Login error: " + err)
  71. } else {
  72. res.redirect('/home')
  73. }
  74. })
  75. }
  76. })
  77. .catch(next)
  78. }
  79. })
  80. })
  81. }
  82. })
  83.  
  84. kill -2 `pgrep mongo`
Add Comment
Please, Sign In to add comment