Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. FRONT END
  2.  
  3. signIn(){
  4. var signInData = {
  5. username : ReactDOM.findDOMNode(this.refs.logInUser).value,
  6. password : ReactDOM.findDOMNode(this.refs.logInPass).value
  7. };
  8.  
  9. $.ajax({
  10. type: "POST",
  11. url: "/loginAuth",
  12. data: signInData,
  13. dataType: "json",
  14. success: function(){
  15. window.location.href= "./";
  16. },
  17. error: function(){
  18. alert("Either your email or password is wrong!");
  19. }
  20. });
  21. }
  22.  
  23. AUTH.JS
  24.  
  25.  
  26. app.post('/loginAuth', function(req, res, next) {
  27. passport.authenticate('local', function(err, user, info) {
  28. if (err) {
  29. return next(err);
  30. }
  31. if (!user) {
  32. return res.status(401);
  33. }
  34. req.logIn(user, function(err) {
  35. if (err) {
  36. return next(err);
  37. }
  38. return res.status(200);
  39. });
  40. })(req, res, next);
  41. });
  42.  
  43.  
  44.  
  45. passport.use(new LocalStrategy({
  46. usernameField: 'username',
  47. passwordField: 'password',
  48. passReqToCallback: true
  49. },
  50.  
  51. function(req, username, password, done) { // callback with email and password from our form
  52.  
  53. connection.query("SELECT * FROM `users` WHERE `username` = '" + username + "'",function(err,rows){
  54. if (err)
  55. return done(err);
  56. if (!rows.length) {
  57. return done(null, false);
  58. }
  59. // if the user is found but the password is wrong
  60. if (!( rows[0].password == password)) {
  61. return done(null, false); // create the loginMessage and save it to session as flashdata
  62. } else {
  63. // all is well, return successful user
  64. return done(null, rows[0].username);
  65. }
  66.  
  67. });
  68. }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement