Guest User

Untitled

a guest
Oct 8th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. server.register(Cookie, function(err) {
  2. if(err) {
  3. console.error(err);
  4. throw err;
  5. }
  6.  
  7. server.auth.strategy('session', 'cookie', {
  8. password: 'fuckthebritisharmytooralooralooraloo',
  9. isSecure: false,
  10. cookie: 'session',
  11. ttl: 24*60*60*1000
  12. });
  13.  
  14. server.route({
  15. method: 'POST',
  16. path: '/login',
  17. config: {
  18. auth: {
  19. mode: 'try',
  20. strategy: 'session'
  21. },
  22. plugins: {
  23. 'hapi-auth-cookie': {
  24. redirectTo: false
  25. }
  26. },
  27. handler: function(req, res) {
  28.  
  29. if (req.auth.isAuthenticated) {
  30. console.info('Already!');
  31. req.cookieAuth.clear(); // Delete
  32. return res.redirect('/');
  33. }
  34. var username = req.payload.username;
  35.  
  36. db.get('user_' + req.payload.username).then(function(data) {
  37. var user = data;
  38. var pass = data.password;
  39. if(!user) {
  40. return console.error('Can`t find user!');
  41. }
  42.  
  43. var password = req.payload.password;
  44. return Bcrypt.compare(password, pass, function(err, isValid) {
  45. if(isValid) {
  46.  
  47. req.server.log('Boom, okay!');
  48.  
  49. req.cookieAuth.set(user);
  50.  
  51. return res.redirect('/');
  52. }
  53. return res.redirect('/login');
  54.  
  55. })
  56. })
  57. .catch((err) => {
  58. if (err) {
  59. console.error(err);
  60. throw err;
  61. }
  62. });
  63. }
  64. }
  65. });
  66. });
Advertisement
Add Comment
Please, Sign In to add comment