2016-02-25 18:24:16 -05:00
|
|
|
errors = require '../commons/errors'
|
|
|
|
wrap = require 'co-express'
|
|
|
|
Promise = require 'bluebird'
|
|
|
|
parse = require '../commons/parse'
|
|
|
|
request = require 'request'
|
2016-04-06 13:56:06 -04:00
|
|
|
User = require '../models/User'
|
2016-02-25 18:24:16 -05:00
|
|
|
|
|
|
|
|
|
|
|
module.exports =
|
|
|
|
fetchByGPlusID: wrap (req, res, next) ->
|
|
|
|
gpID = req.query.gplusID
|
|
|
|
gpAT = req.query.gplusAccessToken
|
|
|
|
next() unless gpID and gpAT
|
|
|
|
|
|
|
|
dbq = User.find()
|
|
|
|
dbq.select(parse.getProjectFromReq(req))
|
|
|
|
url = "https://www.googleapis.com/oauth2/v2/userinfo?access_token=#{gpAT}"
|
|
|
|
[googleRes, body] = yield request.getAsync(url, {json: true})
|
|
|
|
idsMatch = gpID is body.id
|
|
|
|
throw new errors.UnprocessableEntity('Invalid G+ Access Token.') unless idsMatch
|
|
|
|
user = yield User.findOne({gplusID: gpID})
|
|
|
|
throw new errors.NotFound('No user with that G+ ID') unless user
|
2016-04-11 19:51:51 -04:00
|
|
|
res.status(200).send(user.toObject({req: req}))
|
2016-02-25 18:24:16 -05:00
|
|
|
|
|
|
|
fetchByFacebookID: wrap (req, res, next) ->
|
|
|
|
fbID = req.query.facebookID
|
|
|
|
fbAT = req.query.facebookAccessToken
|
|
|
|
next() unless fbID and fbAT
|
|
|
|
|
|
|
|
dbq = User.find()
|
|
|
|
dbq.select(parse.getProjectFromReq(req))
|
|
|
|
url = "https://graph.facebook.com/me?access_token=#{fbAT}"
|
|
|
|
[facebookRes, body] = yield request.getAsync(url, {json: true})
|
|
|
|
idsMatch = fbID is body.id
|
|
|
|
throw new errors.UnprocessableEntity('Invalid Facebook Access Token.') unless idsMatch
|
|
|
|
user = yield User.findOne({facebookID: fbID})
|
|
|
|
throw new errors.NotFound('No user with that Facebook ID') unless user
|
2016-04-11 19:51:51 -04:00
|
|
|
res.status(200).send(user.toObject({req: req}))
|