muZk/rails5-api-jwt-facebook-auth

Question regarding FacebookService

Closed this issue · 1 comments

Hi Nicolás,

Thanks for this example, I found it very useful since I have the exact same requirement for a personal project.

I just have this question:

In FacebookUserTokenController you are calling #fetch_data if the given access token is valid, so what is the reason for the additional call to the #valid_token? method inside #fetch_data in FacebookService? I assume you are doing this for a reason but I cannot figure out why.

Thanks again.

muZk commented

Hey,

To be honest, there is no particular reason.

I only wanted to kept the @entity ||= block but as you noticed it makes 2 API calls which isn't good at all.

Maybe this is better:

  def entity
    data = FacebookService.fetch_data(auth_params[:access_token])
    @entity ||=
      if data.present?
        User.find_or_create_by uid: data['id'] do |user|
          user.first_name = data['first_name']
          user.last_name = data['last_name']
          user.email = data['email']
        end
      end
  end

Less sexy but only 1 API call 👍