2

I'm basically trying to pass the token stored in Preferences to the turbo fragment. I figured that being stored, it would be usable in the fragments.

Are there any tutorials on how this is done? I'm not that well versed in android.

For ios => this they say:

"For HEY, we have a slightly different approach. We get the cookies back along with our initial OAuth request and set those cookies directly to the web view and global cookie stores"

How exactly to you set the token/cookies to the web view and is android Preferences the same as ios global cookie store? Much appreciated help!

UPDATE:

Thanks to @yungindigo the answer below it works! I had to modify my way because I have my token stored in a different table but what I used for my application is:

User.rb 
has_many :oauth_tokens, dependent: :destroy

OauthToken.rb
belongs_to :user

application_helper.rb

  def auth_mobile_user
    token = cookies[:oauth_token]
    oauth = OauthToken.find_by_oauth_token(token)
    session[:user_id] = oauth.user.id
  end

application.html.erb

  <% unless mobile? %>
    <%= render 'layouts/header' %>
  <% else %>
    <% auth_mobile_user %>
  <% end %>

The mobile? method just checks the header for name that I assign in the user_agent from android so the app knows if its from a mobile app or web app.

Chrismisballs
  • 167
  • 12

1 Answers1

1

I was struggling with this problem too and when I looked at that documentation again I had the idea that it was so simple you just need to set the cookies and they will show up on the rails backend.

All it took was one line to set the cookie

CookieManager.getInstance().setCookie(BASE_URL, "oauth_token=${authToken}");

then in the rails controller you can access with

def auth_mobile_user
  user = User.find_by_oauth_token(cookies[:oauth_token])
  session[:user_id] = user&.id
end
yungindigo
  • 241
  • 1
  • 6
  • Are you setting the cookies in the `MainSessionNavHostFragment`? Also the `cookie[:oauth_token]` are you setting that on the `session controller` or `application controller?` – Chrismisballs Oct 15 '22 at 03:56
  • I am setting the cookies with CookieManager in the MainActivity I do a native login screen and request with Fuel and then get the authToken after a successful login and password. – yungindigo Oct 15 '22 at 04:57
  • The cookies[:oauth_token] is used in a method in the application_controller – yungindigo Oct 15 '22 at 04:58
  • cool Ill give this a try in the morning – Chrismisballs Oct 15 '22 at 06:20