Last active
May 15, 2017 18:42
-
-
Save emilsedgh/561da3566260a3218188071fdbab3275 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).then(token => { | |
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) | |
}) | |
}) | |
.catch(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]) | |
const e = new Error(`Token ${token} not found`) | |
if (res.rows.length < 1) | |
throw e | |
return res.rows[0] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment