Skip to content

Instantly share code, notes, and snippets.

@biggora
Last active January 3, 2016 19:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biggora/8509314 to your computer and use it in GitHub Desktop.
Save biggora/8509314 to your computer and use it in GitHub Desktop.
Local Authorization in TrinteJS
/**
* Local Authorization manager
*
*
* Created by init script
* App based on TrinteJS MVC framework
* TrinteJS homepage http://www.trintejs.com
**/
var auth = require( 'trinte-auth' );
var config = require( '../configuration' );
var passport = require( 'passport' );
var LocalStrategy = require( 'passport-local' ).Strategy;
/**
* Example LocalStrategy
*/
passport.use( new LocalStrategy( {
usernameField: 'login',
passwordField: 'password'
}, function(username, password, done) {
if (!username || username === "") {
return done(null, false, {message: 'Required username.'});
}
if (!password || password === "") {
return done(null, false, {message: 'Required password.'});
}
User.findOne( {
username: username
}).exec(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 );
} );
}
) );
passport.serializeUser( function(user, done) {
done( null, user.id );
} );
passport.deserializeUser( function(id, done) {
User.findById( id, function(err, user) {
done( err, user );
} );
} );
/**
* localAuth is authenticate middleware
* @param {String} path
*/
auth.localAuth = function localAuth(path) {
return passport.authenticate( 'local', {
successReturnToOrRedirect: '/',
failureRedirect: path || '/login',
failureFlash: true,
successFlash: 'Welcome!'
} );
};
/**
* isAdmin is admin authenticate middleware
* @param {String} path
*/
auth.isAdmin = function isAdmin(path) {
return function(req, res, next){
// your code here
next();
};
};
auth.passport = passport;
module.exports = auth;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment