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

Fixing online token creation #1413

Merged
merged 5 commits into from
Apr 27, 2022
Merged

Fixing online token creation #1413

merged 5 commits into from
Apr 27, 2022

Conversation

paulomarg
Copy link
Contributor

What this PR does

We unintentionally dropped some important functionality from this gem as part of our updates of shopify_api: the OAuth flow was expected to create both an offline and an online tokens when using user storage, but the updated flow was only doing one of them.

This PR walks that back by adding back the behaviour of first getting an offline token, then an online token, then completing the OAuth process, but only in cases where online tokens are required - offline flows remain the same (only a shop is added to the DB).

Reviewer's guide to testing

  1. Create a new app with the CLI
  2. Run rails generate shopify_app:user_model
  3. Add config.user_session_repository = 'User' in shopify_app.rb
  4. shopify app serve => go through login

Things to focus on

  1. Does oauth work for online tokens?
  2. Does oauth work for offline tokens?

Checklist

Before submitting the PR, please consider if any of the following are needed:

  • Update CHANGELOG.md if the changes would impact users

@paulomarg paulomarg marked this pull request as ready for review April 13, 2022 21:54
@hannachen
Copy link
Contributor

When I add the line config.user_session_repository = 'User' into shopify_app.rb, initial install takes me back to the app listing page with this error message after a few redirects:
Screen Shot 2022-04-14 at 12 00 32 PM

And the app doesn't seem to load any products once installed 🤔
Screen Shot 2022-04-14 at 12 07 08 PM

I'm installing the app using the ngrok URL displayed in the serve command: https://6764-2607-fea8-a71f-f5fc-54cd-5d00-d13f-9c69.ngrok.io/login?shop=sabotender-shop.myshopify.com

@paulomarg paulomarg force-pushed the fix_online_token_creation branch 3 times, most recently from 3f4c5ea to 934e29d Compare April 14, 2022 20:47
Copy link
Contributor

@hannachen hannachen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎩 👍
I created an app with this template using the CLI, it's now working and no more redirect loops!


def start_user_token_flow?(shopify_session)
return false unless ShopifyApp::SessionRepository.user_storage.present?
return false if shopify_session.online?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does shopify_session.online? check whether the response if for an online token?

Copy link
Contributor

@hannachen hannachen Apr 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe is_online is defined on shopify-api. I would keep online? as I'm getting an undefined error when tophatting.

Copy link
Contributor

@hannachen hannachen Apr 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I misread it! @paulomarg, do you have additional context for this part? From my understanding, if the app is requesting an online token, then the merchant will be taken through the auth process twice starting with an offline token. If the last token received is an online token, then we don't initiate a redirect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I believe that is the original behaviour here - if we want online tokens, we go through OAuth twice in a row to get both of them.

@paulomarg
Copy link
Contributor Author

@hannachen I noticed we weren't properly cleaning up all of the configuration values when setting up for tests, which led to this inconsistent behaviour. I've now changed the setup config so that it always resets all values (that I could find).

I also isolated a bit of code for mocking a session which was what actually broke in the tests as part of my changes.

Copy link
Contributor

@rezaansyed rezaansyed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎩 'd and LGTM!

@paulomarg paulomarg merged commit c7e61d5 into main Apr 27, 2022
@paulomarg paulomarg deleted the fix_online_token_creation branch April 27, 2022 12:37
@shopify-shipit shopify-shipit bot temporarily deployed to rubygems April 27, 2022 14:29 Inactive
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 this pull request may close these issues.

None yet

3 participants