Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

authData:facebook vs _auth_data_facebook? #755

Closed
michaelbina opened this issue Mar 2, 2016 · 11 comments · Fixed by #952
Closed

authData:facebook vs _auth_data_facebook? #755

michaelbina opened this issue Mar 2, 2016 · 11 comments · Fixed by #952

Comments

@michaelbina
Copy link

Before migrating to parse-server on heroku, all of the Facebook auth data was stored in the authData field for a User object like so:

    "authData": {
        "facebook":  {
            "id": "10104620262907903",
            "access_token": "...",
            "expiration_date": "4001-01-01T00:00:00.000Z"
        },
    }

After migrating, I am seeing the field _auth_data_facebook like so:

    "_auth_data_facebook": {
        "id": "10104620262907903",
        "access_token": "...",
        "expiration_date": "4001-01-01T00:00:00.000Z"
    },

First, why was this changed?

Second, I am having problems accessing the _auth_data_facebook from Parse Cloud.

A search for a user in parse cloud, returns the user object without the _auth_data_facebook field and with the facebook field in authData set to nil. Also my call to Parse.FacebookUtils.isLinked(user) returns false even thought its true and I have no way to retrieve the access token from the user object.

Any help would be appreciated in figuring out what I'm doing wrong or if this is a bug. Thanks!

@michaelbina
Copy link
Author

Looks like the change was made in this pull: #310 by @flessard

@flovilmart
Copy link
Contributor

If you see the field in MongoDB that's totally normal, the auth data gets flatten for performance reasons. Can you properly login with Facebook?

@michaelbina
Copy link
Author

I can properly login, yes, but it's causing the other problems I mentioned, namely an invalid value for Parse.FacebookUtils.isLinked(user) and the ability to get the token from Parse Cloud. Any thoughts on those problems?

@gfosco
Copy link
Contributor

gfosco commented Mar 7, 2016

Can you provide the code you're using in cloud code?

@michaelbina
Copy link
Author

Coffeescript:

  user.get(userObjectId).then((user) ->
    console.log 'UserID: ' + user.id
    if Parse.FacebookUtils.isLinked(user)
      console.log 'token:' + user.get('authData').facebook.access_token

The second console is what would have gotten me the token before but doesn't work anymore.

@gfosco
Copy link
Contributor

gfosco commented Mar 7, 2016

Instead of Parse.FacebookUtils, do you get the right result if you query with the master key and check the authData key directly?

user.get(userObjectId, { useMasterKey: true }).then((user) ->
  if user.get('authData') && user.get('authData').facebook
    console.log user.get('authData').facebook.access_token

@michaelbina
Copy link
Author

That does not solve it (I posted old code and was already calling useMasterKey).

Even when the user is connected to Facebook, the authData field is showing up like this:

"authData":{"facebook":null}

When I check mongo directly, I see the data is being stored in the "_auth_data_facebook" field and I don't seem to have any way to access that field as it's not showing up in the returned user object.

@DrBeak1
Copy link

DrBeak1 commented Mar 7, 2016

I may be experiencing a related issue. When I log in with facebook, it creates a new user every time and that user cannot be modified; for example, if I try to update the username it reverts back to the random string provided by facebook. If I log out then back in with the same facebook account, it generates a brand new user, brand new random string.

** EDIT **
Actually, this may have been related to a beforeSave that I had in place. Still investigating, but won't hijack your thread with a separate issue if it turns out that way.

@flovilmart
Copy link
Contributor

@michaelbina in your code, user.get(userObjectId) does that refers to a userQuery?

Is the user currently logged in?
Can you share more of that snipet so we have more context?

@michaelbina
Copy link
Author

Yes. It's a Parse.Query(Parse.User) object. I'm not sure it should make a difference if the user is logged in since it's running from cloud code. Should I be doing something with a session token or something to get more user data or something? Here's the entire function:

Parse.Cloud.afterSave "AnimalTimelineEntry", (request, response) ->
  console.log("timeline entry created: " + JSON.stringify(request))
  console.log("shareToFacebook: " + request.object.get("shareToFacebook"))
  console.log("shareToTwitter: " + request.object.get("shareToTwitter"))

  if(request.object.get("shareToFacebook"))
    console.log("sharing to Facebook for: " + request.object.get("createdBy").id)
    userObjectId = request.object.get("createdBy").id
    entryText = request.object.get("text")

    userQuery = new Parse.Query(Parse.User)
    userQuery.get(userObjectId,
      useMasterKey: true
    ).then((user) ->
      console.log 'User: ' + JSON.stringify(user)
      # if Parse.FacebookUtils.isLinked(user)

      # if user.get('_auth_data_facebook')
      #   console.log 'token:' + user.get('_auth_data_facebook').access_token

      if user.get('authData') && user.get('authData').facebook
        console.log 'token: ' + user.get('authData').facebook.access_token

        Parse.Cloud.httpRequest(
          method: 'POST'
          url: 'https://graph.facebook.com/me/feed'
          params:
            access_token: user.get('authData').facebook.access_token
            message: entryText + "\n\nCheck out Phoebe on White Rabbit Apps"
            link: "http://www.whiterabbitapps.net/cat/phoebe_the_bug"
        ).then ((httpResponse) ->
          console.log("back from http request")
        ), (error) ->
          console.log("error with http request: " + error.data.error.message)
          return response.error(error.data.error.message)
      else
        return Parse.Promise.error('user not linked to fb account')
      return
    ).then ((result) ->
      console.log "result from post: " + JSON.stringify(result)
      return response.success 'Post'
    ), (error) ->
      console.log error
      console.error error
      return response.error("Error posting")
    return

@flovilmart
Copy link
Contributor

This should be fixed by #952

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants