node.js - Where to pass username and password passport local strategy -
i using express js in application. need authentication have selected passport want use it's local strategy
have placed code in app.js file
code is:
//application dependencies var express = require('express'); var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); var expressvalidator = require("express-validator"); var mysql = require('mysql'); var multer = require('multer'); var passport = require('passport'); var localstrategy = require('passport-local').strategy; //express js routes var routes = require('./routes/index'); var users = require('./routes/users/users'); var app = express(); //passport configuration passport.use(new localstrategy( function(username, password, done) { user.findone({ username: username }, function(err, user) { if (err) { return done(err); } if (!user) { return done(null, false, { message: 'incorrect username.' }); } if (!user.validpassword(password)) { return done(null, false, { message: 'incorrect password.' }); } return done(null, user); }); } )); app.get('/', passport.authenticate('local'), function (req, res) { // if function gets called, authentication successful. // `req.user` contains authenticated user. console.log("i success callback"); res.send("ok"); }); app.use(function (req, res, next) { models(function (err, db) { if (err) return next(err); req.models = db.models; req.db = db; return next(); }); }); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); // app.use('/', routes); app.use('/users', users); app.use(express.static(__dirname + '/app')); //add npm_modules can node module files app.use('/node_modules', express.static(__dirname + '/node_modules')); app.use('/common', express.static(__dirname + '/common')); // catch 404 , forward error handler app.use(function(req, res, next) { var err = new error('not found'); err.status = 404; next(err); }); // error handlers // development error handler // print stacktrace if (app.get('env') === 'development') { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); } // production error handler // no stacktraces leaked user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); }); module.exports = app;
when running application showing bad request
. know passport looking username , password , not getting showing bad request
error don't know pass username , password passport. can see in passport configuration have line user.findone({ username: username }
finding don't know have idea?
Comments
Post a Comment