Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- server.register(Cookie, function(err) {
- if(err) {
- console.error(err);
- throw err;
- }
- server.auth.strategy('session', 'cookie', {
- password: 'fuckthebritisharmytooralooralooraloo',
- isSecure: false,
- cookie: 'session',
- ttl: 24*60*60*1000
- });
- server.route({
- method: 'POST',
- path: '/login',
- config: {
- auth: {
- mode: 'try',
- strategy: 'session'
- },
- plugins: {
- 'hapi-auth-cookie': {
- redirectTo: false
- }
- },
- handler: function(req, res) {
- if (req.auth.isAuthenticated) {
- console.info('Already!');
- req.cookieAuth.clear(); // Delete
- return res.redirect('/');
- }
- var username = req.payload.username;
- db.get('user_' + req.payload.username).then(function(data) {
- var user = data;
- var pass = data.password;
- if(!user) {
- return console.error('Can`t find user!');
- }
- var password = req.payload.password;
- return Bcrypt.compare(password, pass, function(err, isValid) {
- if(isValid) {
- req.server.log('Boom, okay!');
- req.cookieAuth.set(user);
- return res.redirect('/');
- }
- return res.redirect('/login');
- })
- })
- .catch((err) => {
- if (err) {
- console.error(err);
- throw err;
- }
- });
- }
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment