Last active
May 15, 2017 17:30
-
-
Save emilsedgh/52b136fb096cf2fae3860706e750e3a0 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
function login (socket, access_token, cb) { | |
Token.get(access_token).nodeify(function (err, token) { | |
if (err) | |
return cb(err) | |
if (!token) | |
return cb(Error.Unauthorized('Invalid credentials')) | |
if (token.expire_date) { | |
const expirey = new Date(token.expire_date) | |
if (expirey < (new Date()).getTime()) { | |
return cb(Error.Unauthorized('Token expired')) | |
} | |
} | |
if (token.type !== 'access') | |
return cb(Error.Unauthorized('Invalid credentials')) | |
User.get(token.user_id, function (err, user) { | |
if (err) | |
return cb(err) | |
if (!user) | |
return cb(Error.ResourceNotFound('Unknown user')) | |
process.domain.user = user | |
socket.user = user | |
socket.client_id = token.client_id | |
joinRooms(socket, cb) | |
}) | |
}) | |
} |
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
const nodeify = promise => { | |
let callback | |
const resolve = res => { | |
callback(null, res) | |
} | |
const reject = err => { | |
callback(err) | |
} | |
promise | |
.then(resolve) | |
.catch(reject) | |
return cb => { | |
callback = cb | |
} | |
} | |
Promise.prototype.nodeify = function(cb) { | |
return nodeify(this)(cb) | |
} |
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
Token.get = async token => { | |
const res = await db.query.promise('token/get', [token]) | |
if (res.rows.length < 1) | |
throw Error.ResourceNotFound(`Token ${token} not found`) | |
return res.rows[0] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment