0 Replies - 294 Views - Last Post: 31 October 2017 - 08:29 PM

#1 general656  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 287
  • Joined: 25-March 15

[NodeJS] Passport Local Strategy does not quite work

Posted 31 October 2017 - 08:29 PM

Has anyone experience with Passport Local in NodeJS? I can't make it quite work.

My services/Passport.js:

const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const mongoose = require('mongoose');
const keys = require('../config/keys');
const crypto = require('crypto');

const User = mongoose.model('users');

passport.use(
  new LocalStrategy({
    usernameField : 'username',
    passwordField : 'password',
  },
    async (username, password, done) => {
      const encrypted_pass = password;
      crypto('md5').update(encrypted_pass).digest('hex');
      const user = await User.findOne({ username, encrypted_pass });
      console.log(`User $${username} was not found in the database`);
      const newUser = await User({ID: 2, displayName: 'Thessaloniki Supplies', username, encrypted_pass}).save();
      return done(null, newUser);
      console.log(`User $${username} was found!`);
      console.log(`User: ${user}`);
      done(null, false, {message: 'invalid user'});
  })
);

passport.serializeUser((user, done) => {
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  User.findOne({ID: id})
    .then(user => {
      done(null ,user);
    });
});


My routes/authRoutes.js

onst passport = require('passport');

module.exports = (app) => {
  app.get(
    '/login',
    (req, res) => {
      res.send(JSON.stringify(req.user, null, 2));
    }
  );

  app.post(
    '/login', 
    passport.authenticate('local', {failureRedirect: '/login'}),
    (req, res) => {
      res.redirect('/');
    }
  );


  app.get(
    '/api/logout',
    (req, res) => {
      req.logout();
      res.redirect('/');
    }
  );

  app.get(
    '/api/profile',
    require('connect-ensure-login').ensureLoggedIn(),
    (req, res) => {
      res.send({
        user: req.user
      });
    }
  );
};


server/index.js

const express = require('express');
const keys = require('./config/keys');
const mongoose = require('mongoose');
const passport = require('passport');
const cookieSession = require('cookie-session');
const bodyParser = require('body-parser');
require('./models/User');
require('./services/passport');

const app = express();
mongoose.connect(keys.mongoURI);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(
	cookieSession({
		maxAge: 30 * 24 * 60 * 60 * 1000,
		keys: [keys.cookieKey]
	})
);

app.use(passport.initialize());
app.use(passport.session());

require('./routes/authRoutes')(app);

if (process.env.NODE_ENV === "production") {
	const path = require('path');

	app.use(express.static(path.resolve(__dirname, '../client/build')));
	app.get('*', (req, res) => {
		res.sendFile(path.resolve(__dirname, '../client/build/', 'index.html'));
	});
}

const PORT = process.env.PORT || 5000;
app.listen(PORT);



Can't find what's wrong with it ... It's like passport.use (which sets up the configuration for the passport.authentication) is never called, because it doesn't print out the console log or doesn't register something new in the database.

This post has been edited by general656: 31 October 2017 - 08:30 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1