Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FRONT END
- signIn(){
- var signInData = {
- username : ReactDOM.findDOMNode(this.refs.logInUser).value,
- password : ReactDOM.findDOMNode(this.refs.logInPass).value
- };
- $.ajax({
- type: "POST",
- url: "/loginAuth",
- data: signInData,
- dataType: "json",
- success: function(){
- window.location.href= "./";
- },
- error: function(){
- alert("Either your email or password is wrong!");
- }
- });
- }
- AUTH.JS
- app.post('/loginAuth', function(req, res, next) {
- passport.authenticate('local', function(err, user, info) {
- if (err) {
- return next(err);
- }
- if (!user) {
- return res.status(401);
- }
- req.logIn(user, function(err) {
- if (err) {
- return next(err);
- }
- return res.status(200);
- });
- })(req, res, next);
- });
- passport.use(new LocalStrategy({
- usernameField: 'username',
- passwordField: 'password',
- passReqToCallback: true
- },
- function(req, username, password, done) { // callback with email and password from our form
- connection.query("SELECT * FROM `users` WHERE `username` = '" + username + "'",function(err,rows){
- if (err)
- return done(err);
- if (!rows.length) {
- return done(null, false);
- }
- // if the user is found but the password is wrong
- if (!( rows[0].password == password)) {
- return done(null, false); // create the loginMessage and save it to session as flashdata
- } else {
- // all is well, return successful user
- return done(null, rows[0].username);
- }
- });
- }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement