node.js - How to return a JSON object using NodeJS, Passport, mongoose, & MongoDB -
i'm new nodejs, passport, , mongo. i'm trying set basic local authentication, successful, however, when try return entire object returns empty array.
my node setup:
var express = require('express'); var routes = require('./routes'); var user = require('./routes/user'); var http = require('http'); var path = require('path'); var passport = require('passport'); var localstrategy = require('passport-local').strategy; var mongoose = require('mongoose/'); mongoose.connect('mongodb://localhost:27017/eidata'); var schema = mongoose.schema; var userdetail = new schema({ username: string, password: string, fullname: string }, { collection: 'userlist' }); var userdetails = mongoose.model('userlist', userdetail); var user_obj = user; var app = express(); // environments app.set('port', process.env.port || 3000); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.json()); app.use(express.urlencoded()); app.use(express.methodoverride()); app.use( express.cookieparser() ); app.use(express.session({ secret: 'plsrespond' })); app.use(passport.initialize()); app.use(passport.session()); app.use(app.router); app.use(express.static(path.join(__dirname, 'public')));
so, tactic set global variable named user_obj, , set user object passport returns. however, feel might scope problem, since global variable empty when call it.
here's passport authentication logic:
passport.use(new localstrategy(function(username, password, done) { process.nexttick(function() { userdetails.findone({ 'username': username, }, function(err, user) { if (err) { return done(err); } if (!user) { return done(null, false); } if (user.password != password) { return done(null, false); } return done(null, user); }); }); }));
lastly, success/failure redirects, , attempt call user_obj global variable:
app.post('/login', passport.authenticate('local', { successredirect: '/loginsuccess', failureredirect: '/loginfailure' }) ); app.get('/loginfailure', function(req, res, next) { res.send('invalid code!'); }); app.get('/loginsuccess', function(req, res, next) { res.json(user_obj) });
Comments
Post a Comment