Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. var passport = require('passport');
  2. var LocalStrategy = require('passport-local').Strategy;
  3. var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
  4. var expressValidator = require('express-validator');
  5. var bodyParser = require('body-parser');
  6. var User = require('./user');
  7. var configAuth = require('./auth');
  8.  
  9. // Login
  10. passport.use('login', new LocalStrategy({
  11. usernameField: 'email',
  12. passwordField: 'password',
  13. passReqToCallback: true
  14. },
  15. function (req, email, password, done) {
  16. console.log("Im here now!");
  17. if (email) {
  18. console.log("toLowerCase")
  19. email = email.toLowerCase();
  20. }
  21.  
  22. process.nextTick(function () {
  23. User.findOne({ 'email': email }, function (err, user) {
  24. if (err) {
  25. return done(err);
  26. }
  27. if (!user) {
  28. console.log('User Not Found with email ' + email);
  29. return done(null, false);
  30. }
  31. if (!user.validPassword(password)) {
  32. console.log('Invalid Password');
  33. return done(null, false);
  34. }
  35. return done(null, user);
  36. });
  37. });
  38. }));
  39.  
  40. router.post('/login', passport.authenticate('login', {
  41. successRedirect: '/auth/success',
  42. failureRediret: '/auth/failure'
  43. }));
  44.  
  45. <form ng-submit="doLogin()">
  46. <div class="list">
  47. <label class="item item-input">
  48. <span class="input-label">Username</span>
  49. <input type="email" data-type="text" ng-model="loginData.email" name="email">
  50. </label>
  51. <label class="item item-input">
  52. <span class="input-label">Password</span>
  53. <input type="password" ng-model="loginData.password" name="password">
  54. </label>
  55. <label class="item">
  56. <button class="button button-block button-positive" type="submit">Log in</button>
  57. </label>
  58. </div>
  59. </form>
  60.  
  61. $scope.doLogin = function() {
  62. var email= $scope.loginData.email;
  63. var password = $scope.loginData.password;
  64. console.log('Doing login', $scope.loginData);
  65. $http({
  66. method: 'POST',
  67. url: 'http://localhost:8000/auth/login',
  68. data: {email: email, password: password},
  69. headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Access-
  70. Control-Allow-Origin': '*'}
  71. });
  72. };
  73.  
  74. app.use('/auth', routes);
  75.  
  76. app.use(passport.initialize());
  77. app.use(passport.session());
  78.  
  79. router.post('/login', passport.authenticate('login', {
  80. successRedirect: '/auth/success',
  81. failureRediret: '/auth/failure'
  82. }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement