Skip to content

Instantly share code, notes, and snippets.

@jondubois
Last active September 30, 2018 21:30
Show Gist options
  • Save jondubois/adb0f5b16e2345acfc59d28d437e5ed2 to your computer and use it in GitHub Desktop.
Save jondubois/adb0f5b16e2345acfc59d28d437e5ed2 to your computer and use it in GitHub Desktop.
sc-crud-sample_authentication.js
module.exports.attach = function (scServer, socket) {
var tokenExpiresInSeconds = 10 * 60;
var tokenRenewalIntervalInMilliseconds = Math.round(
1000 * tokenExpiresInSeconds / 3
);
// Keep renewing the token (if there is one) at a predefined interval to make
// sure that it doesn't expire while the connection is active.
var renewAuthTokenInterval = setInterval(function () {
var currentToken = socket.getAuthToken();
if (currentToken) {
currentToken.exp = Math.round(Date.now() / 1000) + tokenExpiresInSeconds;
socket.setAuthToken(currentToken);
}
}, tokenRenewalIntervalInMilliseconds);
socket.once('disconnect', function () {
clearInterval(renewAuthTokenInterval);
});
var validateLoginDetails = function (loginDetails, respond) {
scServer.thinky.r.table('User')
.filter({username: loginDetails.username})
.run(function (err, results) {
if (results && results[0] && results[0].password === loginDetails.password) {
var token = {
username: loginDetails.username
};
socket.setAuthToken(token, {expiresIn: tokenExpiresInSeconds});
respond();
} else {
// This is not really an error.
// We are simply rejecting the login - So we will
// leave the first (error) argument as null.
respond(null, 'Invalid username or password');
}
});
};
socket.on('login', validateLoginDetails);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment