Advertisement
Guest User

Untitled

a guest
Aug 11th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unction validateInput(data, otherValidation) {
  2.     let { errors } = otherValidation(data);
  3.     console.log(errors);
  4.     console.log('sprawdzam');
  5.     const {
  6.         username,
  7.         firstName,
  8.         lastName,
  9.         email,
  10.         password,
  11.         passwordConfirmation,
  12.     } = data;
  13.  
  14.     return User.find({
  15.         $or: [
  16.             {
  17.                 userName: username,
  18.             },
  19.             {
  20.                 email: email,
  21.             },
  22.         ],
  23.     }).then(user => {
  24.         console.log(user);
  25.         if (user.length) {
  26.             if (user[0].userName === username) {
  27.                 errors.username = 'Przepraszamy, Login jest już zajęty.';
  28.                 console.log('Login zajety');
  29.             }
  30.             if (user[0].email === email) {
  31.                 errors.email = 'E-mail został juz wykorzystany';
  32.                 console.log('email zajety');
  33.             }
  34.         }
  35.         console.log('nie ma takiego usera');
  36.         return {
  37.             errors,
  38.             isValid: isEmpty(errors),
  39.         };
  40.     });
  41. }
  42.  
  43.  
  44.  
  45. passport.use(
  46.     'local-signup',
  47.     new LocalStrategy(
  48.         {
  49.             usernameField: 'email',
  50.             passwordField: 'password',
  51.             passReqToCallback: true,
  52.         },
  53.         function(req, email, password, done) {
  54.             process.nextTick(function() {
  55.  
  56.                 validateInput(req.body, validator.validateInput).then(
  57.                     ({ errors, isValid }) => {
  58.                         //tu mi nie wchodzi w ogóle
  59.                         console.log(isValid);
  60.                         if (isValid) {
  61.                             const {
  62.                                 username,
  63.                                 firstName,
  64.                                 lastName,
  65.                                 email,
  66.                                 password,
  67.                                 passwordConfirmation,
  68.                             } = req.body;
  69.  
  70.                             const user = new User({
  71.                                 email: email,
  72.                                 password: user.generateHash(password),
  73.                                 firstName: firstName,
  74.                                 lastName: lastName,
  75.                                 userName: username,
  76.                                 role: 'student',
  77.                             });
  78.  
  79.                             user.save(function(err) {
  80.                                 if (err) {
  81.                                     console.log(err);
  82.                                 }
  83.  
  84.                                 return done(null, user);
  85.                             });
  86.                         } else {
  87.                             return done(errors);
  88.                         }
  89.                     }
  90.                 );
  91.             });
  92.         }
  93.     )
  94. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement