Guest User

Untitled

a guest
Sep 30th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. module.exports.attach = function (scServer, socket) {
  2. var tokenExpiresInSeconds = 10 * 60;
  3. var tokenRenewalIntervalInMilliseconds = Math.round(
  4. 1000 * tokenExpiresInSeconds / 3
  5. );
  6.  
  7. // Keep renewing the token (if there is one) at a predefined interval to make
  8. // sure that it doesn't expire while the connection is active.
  9. var renewAuthTokenInterval = setInterval(function () {
  10. var currentToken = socket.getAuthToken();
  11. if (currentToken) {
  12. currentToken.exp = Math.round(Date.now() / 1000) + tokenExpiresInSeconds;
  13. socket.setAuthToken(currentToken);
  14. }
  15. }, tokenRenewalIntervalInMilliseconds);
  16.  
  17. socket.once('disconnect', function () {
  18. clearInterval(renewAuthTokenInterval);
  19. });
  20.  
  21. var validateLoginDetails = function (loginDetails, respond) {
  22. scServer.thinky.r.table('User')
  23. .filter({username: loginDetails.username})
  24. .run(function (err, results) {
  25. if (results && results[0] && results[0].password === loginDetails.password) {
  26. var token = {
  27. username: loginDetails.username
  28. };
  29. socket.setAuthToken(token, {expiresIn: tokenExpiresInSeconds});
  30. respond();
  31. } else {
  32. // This is not really an error.
  33. // We are simply rejecting the login - So we will
  34. // leave the first (error) argument as null.
  35. respond(null, 'Invalid username or password');
  36. }
  37. });
  38. };
  39.  
  40. socket.on('login', validateLoginDetails);
  41. };
Add Comment
Please, Sign In to add comment