Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. $(document).ready(function(){
  2. $('#btn-login').click(function() {
  3. var email = $('#loginEmail').val();
  4. var password = $('#loginPassword').val();
  5. var emailValidation = new RegExp(/^([w-]+(?:.[w-]+)*)@((?:[w-]+.)*w[w-]{0,66}).([a-z]{2,6}(?:.[a-z]{2})?)$/i);
  6. if(!email | !password) {
  7. $('#loginLabel').text('Email and password are required.');
  8. }
  9. else if(!emailValidation.test(email)) {
  10. $('#loginLabel').text('Email format is invalid.');
  11. }
  12. else {
  13. var userData = {
  14. username: email,
  15. password: password
  16. };
  17.  
  18. var request = $.ajax({
  19. type: 'POST',
  20. url: '/login',
  21. data: userData
  22. });
  23.  
  24. request.done(function (response, textStatus, jqXHR) {
  25. window.location.href = '/profile';
  26. }).fail(function (jqXHR, exception) {
  27. $('#loginLabel').text('Invalid email and/or password.');
  28. });
  29. }
  30. });
  31. });
  32.  
  33. activateUserAccount(payload, res) {
  34. const token = payload.token;
  35. signUpService.validateEmailToken(token)
  36. .then(isVerified => {
  37. if(isVerified[0]) {
  38.  
  39. const userData = JSON.stringify({
  40. username: isVerified[1],
  41. password: isVerified[2]
  42. });
  43.  
  44. const options = {
  45. hostname: 'localhost',
  46. port: 3000,
  47. path: '/login',
  48. agent: false,
  49. method: 'POST',
  50. headers: {
  51. 'Content-Type': 'application/json',
  52. }
  53. };
  54.  
  55. const req = http.request(options, (response) => {
  56. let responseString = '';
  57.  
  58. response.on('data', data => {
  59. responseString += data;
  60. });
  61. response.on('end', (data) => {
  62. res.redirect('profile');
  63. });
  64.  
  65.  
  66. });
  67.  
  68. req.write(userData);
  69. req.end();
  70. }
  71. else {
  72. res.redirect('error-page' + '?status=errit');
  73. }
  74. })
  75. .catch(err => console.log(err));
  76. }
  77.  
  78. const expiryDate = new Date(Date.now() + 60 * 60 * 1000);
  79. app.use(session({
  80. secret: 'XeGcW4Vb23',
  81. resave: false,
  82. saveUninitialized: false,
  83. httpOnly: true,
  84. expires: expiryDate
  85. }));
  86.  
  87. app.use(passport.initialize());
  88. app.use(passport.session());
  89.  
  90. passport.serializeUser((user, done) => {
  91. done(null, user);
  92. });
  93.  
  94. passport.deserializeUser((user, done) => {
  95. done(null, user);
  96. });
  97.  
  98. const restrictedArea = (req, res, next) => {
  99. if (req.isAuthenticated()) {
  100. next();
  101. } else {
  102. res.redirect('/login');
  103. }
  104. };
  105.  
  106. passport.use(new LocalStrategy(
  107. (username, password, done) => {
  108. console.log("strategy username");
  109. console.log(username);
  110. console.log("strategy password");
  111. console.log(password);
  112. console.log("strategy done");
  113. console.log(done);
  114. databaseManagement.selectUser(username, password)
  115. .then(user => _.isEmpty(user)
  116. ? done(null, false)
  117. : done(null, { username: username, password: password }));
  118. }
  119. ));
  120.  
  121. app.post('/login', (req, res, next) => {
  122. console.log("REQ BODY");
  123. console.log(req.body);
  124. passport.authenticate('local', (err, user, info) => {
  125. console.log("REQ err");
  126. console.log(err);
  127. console.log("REQ user");
  128. console.log(user);
  129. console.log("REQ info");
  130. console.log(info);
  131. if(err) { return res.status(500).json(err);}
  132. if(!user) { return res.status(401).json(info);}
  133. req.logIn(user,(err) => {
  134. if (err) { return next(err); }
  135. return res.json({detail: info});
  136. });
  137. })(req, res, next);
  138. });
  139.  
  140. app.get('/profile', restrictedArea, (req, res) => {
  141. profile.renderProfile(res);
  142. });
  143.  
  144. REQ BODY
  145. { username: 'someemail@gmail.com', password: 'Potato123' }
  146. strategy username
  147. someemail@gmail.com
  148. strategy password
  149. Potato123
  150. strategy done
  151. [Function: verified]
  152. REQ err
  153. null
  154. REQ user
  155. { username: 'someemail@gmail.com', password: 'Potato123' }
  156. REQ info
  157. undefined
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement