Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't use passportjs yet but i've just done the same thing you're looking to do. Here's my example configuration:</p> <pre><code>// Example configuration var express = require('express'); var routes = require('./routes'); var app = express(); app.configure(function(){ app.use(express.bodyParser()); app.use(express.cookieParser('shhhh, very secret')); app.use(express.session()); app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.compress()); app.use('/', express.static(expressAppdir)); // look for overrides on express server 1st app.use('/', express.static(appDir)); // app.use(express.methodOverride()); app.use(app.router); app.use(function(req, res, next){ var err = req.session.error , msg = req.session.success; delete req.session.error; delete req.session.success; res.locals.message = ''; if (err) res.locals.message = '&lt;p class="msg error"&gt;' + err + '&lt;/p&gt;'; if (msg) res.locals.message = '&lt;p class="msg success"&gt;' + msg + '&lt;/p&gt;'; next(); }); }); app.configure(function() { // gets app.get('/', routes.root); app.get('/login', routes.login); app.get('/logout', routes.logout); app.get('/restricted/test/:slug', restrict, routes.restrictedGet); // must be last API route, slug is any request on the end of the routes that is requested. app.post('/login', routes.loginPost); }); function restrict(req, res, next) { console.dir('restrict called'); if (req.session.user) { next(); } else { req.session.error = 'Access denied!'; res.redirect('/login'); } } //Routes.js file // my dummy login (in a separate file) var passport = require('passport') , LocalStrategy = require('passport-local').Strategy; 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); }); } )); exports.restrictedGet = function (req, res, next) { console.dir('reached restricted get'); var slug = req.params.slug; console.dir(req.route); if(req.route.path.indexOf('test')!=-1) { namedQuery['testFunction'](req,res,next); } else { res.status(404).send('no route found. Route requested: ' + req.route.path); } // do something with your route here, check what's being appended to the slug and fire off the appropriate function. }; exports.login = function(req, res, next) { res.sendfile(serverBase + "/static/public/login.html"); }; exports.logout = function(req, res, next) { req.session.destroy(function(){ res.redirect('/'); }); }; // this is where you would hook in your passportjs stuff to do hashing of inputted text and compare it to the hash stored in your db etc. // I use my own simple authentication funciton right now as i'm just testing. exports.loginPost = function(req, res, next) { authenticate(req.body.username, req.body.password, function(err, user){ console.log('Reached login user: ', user); if (user) { // Regenerate session when signing in // to prevent fixation req.session.regenerate(function(){ req.session.user = user; req.session.success = 'Authenticated as ' + user.name + ' click to &lt;a href="/logout"&gt;logout&lt;/a&gt;. ' + ' You may now access &lt;a href="/restricted"&gt;/restricted&lt;/a&gt;.'; res.redirect('/'); }); } else { req.session.error = 'Authentication failed, please check your ' + ' username and password.' + ' (use "tj" and "foobar")'; res.json({success: false}); res.redirect('/login'); } }); }; // You could now do this with passport instead: exports.loginPost = function(req, res, next) { passport.authenticate('local'), function(err, user){ console.log('Reached login user: ', user); if (user) { // Regenerate session when signing in // to prevent fixation req.session.regenerate(function(){ req.session.user = user; req.session.success = 'Authenticated as ' + user.name + ' click to &lt;a href="/logout"&gt;logout&lt;/a&gt;. ' + ' You may now access &lt;a href="/restricted"&gt;/restricted&lt;/a&gt;.'; res.redirect('/'); }); } else { req.session.error = 'Authentication failed, please check your ' + ' username and password.' + ' (use "tj" and "foobar")'; res.json({success: false}); res.redirect('/login'); } }; }; function authenticate(name, pass, fn) { var user = { name:name, password: pass } return fn(null,user); }; </code></pre> <p>This is where I got alot of my code from: <a href="http://www.breezejs.com/samples/zza" rel="nofollow">http://www.breezejs.com/samples/zza</a>, <a href="http://passportjs.org/guide/authenticate/" rel="nofollow">http://passportjs.org/guide/authenticate/</a></p> <p>Hope this helps!</p> # <p>EDIT</p> # <p>I forgot to mention, for the angular side I just have a simple form posting back the values for user and password to the login post endpoint, since the getEndpoint is restricted, the express app will handle the rest of the authentication and restriction side of things for you. If I can be of any further help please don't hesitate to ask.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload