Last active
August 29, 2015 13:57
-
-
Save tanepiper/9409235 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var LocalStrategy = require('passport-local').Strategy; | |
var Bcrypt = require('bcrypt'); | |
module.exports = function(Instance) { | |
var plugins = { | |
yar: { | |
cookieOptions: { | |
password: 'adon-platform', // cookie secret | |
isSecure: false // required for non-https applications | |
} | |
}, | |
travelogue: { | |
hostname: Instance.config.server.host, | |
port: Instance.config.server.port, | |
urls: { | |
failureRedirect: '/login', | |
successRedirect: '/dashboard' | |
}, | |
excludePaths: ['/', '/signup'] | |
} | |
}; | |
Instance.server.pack.require(plugins, function (err) { | |
if (err) { | |
console.log(err); | |
process.exit(1); | |
} | |
}); | |
Instance.server.auth.strategy('passport', 'passport'); | |
var Passport = Instance.server.plugins.travelogue.passport; | |
Passport.use(new LocalStrategy( | |
function(email, password, done) { | |
Instance.db.User.find({where: {email: email} }) | |
.success(function(user) { | |
if (!user) { | |
return done(null, false, Instance.error.notFound('User ' + email + ' not found')); | |
} else if (!user.checkPassword(password)) { | |
return done(null, false, Instance.error.unauthorized('Passwords do not match')); | |
} else { | |
done(null, user.values); | |
} | |
}) | |
.error(function(err) { | |
console.log(err); | |
done(err); | |
}); | |
} | |
)); | |
Passport.serializeUser(function (user, done) { | |
done(null, user); | |
}); | |
Passport.deserializeUser(function (obj, done) { | |
done(null, obj); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(Instance) { | |
Instance.server.route({ | |
method: 'GET', | |
path: '/dashboard', | |
config: { | |
auth: 'passport', | |
handler: function(request, reply) { | |
reply.view('dashboard.html'); | |
} | |
} | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(Instance) { | |
var Passport = Instance.server.plugins.travelogue.passport; | |
Instance.server.route({ | |
method: 'GET', | |
path: '/login', | |
config: { | |
auth: false, | |
handler: function(request, reply) { | |
console.log(request.session); | |
if (request.session._isAuthenticated()) { | |
reply().redirect('/dashboard'); | |
} else { | |
reply.view('login.html'); | |
} | |
} | |
} | |
}); | |
Instance.server.route({ | |
method: 'GET', | |
path: '/logout', | |
config: { | |
auth: false, | |
handler: function(request, reply) { | |
request.session._logOut(); | |
reply().redirect('/login'); | |
} | |
} | |
}); | |
Instance.server.route({ | |
method: 'POST', | |
path: '/login', | |
config: { | |
validate: { | |
payload: { | |
username: Instance.Hapi.types.String(), | |
password: Instance.Hapi.types.String() | |
} | |
}, | |
auth: false, | |
handler: function(request, reply) { | |
console.log(Instance.server.plugins.travelogue); | |
Passport.authenticate('local', { | |
successRedirect: Instance.server.plugins.travelogue.settings.urls.successRedirect, | |
failureRedirect: Instance.server.plugins.travelogue.settings.urls.failureRedirect | |
})(request, reply); | |
} | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment