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

.current_user scope #4

Closed
augustosamame opened this issue Jun 30, 2016 · 1 comment
Closed

.current_user scope #4

augustosamame opened this issue Jun 30, 2016 · 1 comment

Comments

@augustosamame
Copy link
Contributor

I am having issues with setting .current_user

When first signing in, everything works fine. However, if the application starts with a user previously logged in, and therefore Auth.signed_in? = true, then Auth.current_user must be set as it is currently false.
I check for this and set it in app_delegate:

app_delegate.rb

 def on_load(app, options)
    if Auth.signed_in?
      ApiClient.update_authorization_header(Auth.authorization_header)
      if Auth.current_user.nil?
        set_current_user
        mp Auth.current_user     # evaluates to nil !!!
      end
      open_authenticated_root
    else
      open SignInScreen.new(nav_bar: true)
    end
  end

  def open_authenticated_root
    open_tab_bar MatchesScreen.new(nav_bar: true), MatchTemplatesScreen.new(nav_bar: true)
  end

  def set_current_user
      ApiClient.client.get "user" do |response|
        if response.success?
          Auth.current_user = User.new(response.object["data"])
        else
          app.alert "Please sign in again"
          open SignInScreen.new(nav_bar: true)
        end
    end
  end


The problem is that despite setting Auth.current_user this way, it is always nil when evaluating right away and is also nil in the authenticated_root screen. It does return the User object just fine in the REPL though. I don't know what I'm doing wrong.

@andrewhavens
Copy link
Collaborator

Hey @augustosamame,

I'm so sorry that I didn't reply until now. I'm assuming you already figured it out, or found a workaround, but I can see the issue in your code. You are not waiting for the HTTP request to be returned. In your code you have:

      if Auth.current_user.nil?
        set_current_user
        mp Auth.current_user     # evaluates to nil !!!
      end

But that's because your set_current_user method is going to run asynchronously. If I were you, I would rewrite this method to accept a block:

      if Auth.current_user.nil?
        set_current_user do
          mp Auth.current_user     # shouldn't be nil
        end
      end
# ...

  def set_current_user(&success_callback)
      ApiClient.client.get "user" do |response|
        if response.success?
          Auth.current_user = User.new(response.object["data"])
          success_callback.call
        else
          app.alert "Please sign in again"
          open SignInScreen.new(nav_bar: true)
        end
    end
  end

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

No branches or pull requests

2 participants