Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 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. passport.use(new LocalStrategy(
  79. (username, password, done) => {
  80. console.log("strategy username");
  81. console.log(username);
  82. console.log("strategy password");
  83. console.log(password);
  84. console.log("strategy done");
  85. console.log(done);
  86. databaseManagement.selectUser(username, password)
  87. .then(user => _.isEmpty(user)
  88. ? done(null, false)
  89. : done(null, { username: username, password: password }));
  90. }
  91. ));
  92.  
  93. app.post('/login', (req, res, next) => {
  94. console.log("REQ BODY");
  95. console.log(req.body);
  96. passport.authenticate('local', (err, user, info) => {
  97. console.log("REQ err");
  98. console.log(err);
  99. console.log("REQ user");
  100. console.log(user);
  101. console.log("REQ info");
  102. console.log(info);
  103. if(err) { return res.status(500).json(err);}
  104. if(!user) { return res.status(401).json(info);}
  105. req.logIn(user,(err) => {
  106. if (err) { return next(err); }
  107. return res.json({detail: info});
  108. });
  109. })(req, res, next);
  110. });
  111.  
  112. REQ BODY
  113. { username: 'someemail@gmail.com', password: 'Potato123' }
  114. strategy username
  115. someemail@gmail.com
  116. strategy password
  117. Potato123
  118. strategy done
  119. [Function: verified]
  120. REQ err
  121. null
  122. REQ user
  123. { username: 'someemail@gmail.com', password: 'Potato123' }
  124. REQ info
  125. undefined
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement