How can I get the credential secret in Meteor, when I do Facebook.requestCredential on the client?

佐手、 提交于 2019-12-01 21:23:24

Turns out you can do it like this at the moment (on the client):

service = "facebook"
Package.facebook.Facebook.requestCredential(
    requestPermissions: Accounts.ui._options.requestPermissions["facebook"]
, (token) ->
    secret = Package.oauth.OAuth._retrieveCredentialSecret(token)
    Meteor.call "userAddOauthCredentials", token, secret, service, (err, resp) ->
        if err?
            Meteor.userError.throwError(err.reason)
)

Then on the server you'll need the secret to access the service data for the user.

userAddOAuthCredentials: (token, secret, service) ->
    services = Meteor.user().services
    serviceSearch = {}
    data = {}
    switch service
        when "facebook"
            if not services.facebook?
                data = Package.facebook.Facebook.retrieveCredential(token, secret)?.serviceData
                services.facebook = data
                serviceSearch = {"services.facebook.id": services.facebook.id}
            else
                throw new Meteor.Error(500, "You already have a linked Facebook account with email #{services.facebook.email}...")
    oldUser = Meteor.users.findOne(serviceSearch)
    if oldUser?
        throw new Meteor.Error(500, "Your #{service} account has already been assigned to another user.")

    Meteor.users.update(@userId, {$set: {services: services}})
    if data.email?
        if not _.contains(Meteor.user().emails, data.email)
            Meteor.users.update(@userId, {$push: {"emails": {address: data.email, verified: true}}})

Those functions will get you the user's service data so you can link multiple accounts, or do whatever you want with them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!