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

Create concern to authenticate deep links using Turbolinks #1118

Merged
merged 3 commits into from
Dec 14, 2020

Conversation

NabeelAhsen
Copy link
Contributor

@NabeelAhsen NabeelAhsen commented Dec 7, 2020

Related #1092

This is related to supporting session tokens using Turbolinks. This PR introduces an optional controller concern that helps authenticate multi-page apps when accessing a link directly.

Currently, with third party cookies disabled, a user is unable to access protected endpoints like /products or /widgets. When using Turbolinks, the recommended way of issuing a valid session token via App Bridge is by creating a splash page as the root resource of the app.

More info: https://shopify.dev/tutorials/authenticate-server-side-rendered-apps-with-session-tokens-app-bridge-turbolinks

This concern redirects an app to the root controller along with a return_to and shop parameter if it detects that an action is unauthenticated (i.e it fails to detect a valid session token session). Once a session token has been issued by App Bridge, Turbolinks can use this to redirect the app back to the return_to parameter in shopify_app.js:

  // Wait for a session token before trying to load an authenticated page
  await retrieveToken(app);

  // Redirect to the requested page
  Turbolinks.visit(data.loadPath);

To use this concern, add it to the sample AuthenticatedController that gets generated by the Shopify App gem:

# frozen_string_literal: true

class AuthenticatedController < ApplicationController
  include ShopifyApp::TurbolinksAuthenticated # <= Add this here
  include ShopifyApp::Authenticated
end
  • Show this works using app extensions and direct app-links
  • Update CHANGELOG.md if the changes would impact users
  • Update README.md, if appropriate.
  • Update any relevant pages in docs/, if necessary
  • For security fixes, the Disclosure Policy must be followed.

@NabeelAhsen NabeelAhsen requested a review from a team as a code owner December 7, 2020 19:29
@NabeelAhsen NabeelAhsen self-assigned this Dec 7, 2020
# frozen_string_literal: true

module ShopifyApp
module TurbolinksAuthenticated
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not entirely sure what to call this concern yet, I'm open to suggestions!

@thecodepixi
Copy link
Contributor

Hey @NabeelAhsen, this looks awesome, but it looks like there are 2 small linter errors at the moment.

@NabeelAhsen
Copy link
Contributor Author

NabeelAhsen commented Dec 7, 2020

Hey @NabeelAhsen, this looks awesome, but it looks like there are 2 small linter errors at the moment.

Oh yeah I've been trying to run bundle exec rubocop locally to fix these, but to no avail! It seems that there's a mismatch in the rubocop versions in the CI vs my local environment 😬

Do you have any recommendations around this?

Edit: Oh it seems that this will most likely be fixed by https://github.com/Shopify/shopify_app/tree/melaniew/fix_rubocop_error

@mllemango
Copy link
Contributor

Sorry that's on me 😅, PR's' up!

@thecodepixi
Copy link
Contributor

I just noticed the same. It looks like that rubocop error exists on master, so you might want to rebase on master when that fix is merged.

As for why rubocop isn't working for you locally, it looks like the CI just runs the latest version of the gems (rubocop and rubocop-shopify) so you may just need to update them locally as well.

@thecodepixi
Copy link
Contributor

Sorry that's on me 😅, PR's' up!

Approved ✅

@thecodepixi
Copy link
Contributor

@NabeelAhsen You should be good to rebase master to get rid of that rubocop error 👍

@andyw8
Copy link
Contributor

andyw8 commented Dec 7, 2020

I opened #1120 which should help to avoid some types of RuboCop version mismatch.

@NabeelAhsen NabeelAhsen force-pushed the add-deep-linking-auth-support branch from f2e9e5a to a50f7df Compare December 7, 2020 20:46
Copy link
Contributor

@ragalie ragalie left a comment

Choose a reason for hiding this comment

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

Makes sense to me!

@NabeelAhsen NabeelAhsen force-pushed the add-deep-linking-auth-support branch from a50f7df to 9336b4a Compare December 8, 2020 15:01
@NabeelAhsen NabeelAhsen force-pushed the add-deep-linking-auth-support branch from 9336b4a to cab35c4 Compare December 8, 2020 15:02
@NabeelAhsen
Copy link
Contributor Author

I've added a section in README.md to explain this concern a little better. I'm still having a hard time naming this thing -- does EnsureAuthenticatedLinks sound too confusing?

Copy link
Contributor

@paulomarg paulomarg left a comment

Choose a reason for hiding this comment

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

I'm not very good at naming things, but I kind of like what we have. The README text LGTM.

README.md Outdated

The `ShopifyApp::EnsureAuthenticatedLinks` concern helps authenticate users that access protected pages of your app directly.

Include this concern in your app's `AuthenticatedController` if your app uses session tokens with [Turbolinks](https://shopify.dev/tutorials/authenticate-server-side-rendered-apps-with-session-tokens-app-bridge-turbolinks). It adds a `before_action` filter that detects whether a session token is present or not. If a session is not found, the app is redirected to your app's splash page path (`root_path`) along with `return_to` and `shop` parameters.
Copy link
Contributor

Choose a reason for hiding this comment

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

"the app is redirected" - should that say "the user is redirected" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch!

README.md Outdated
Example `AuthenticatedController`:

```rb
# frozen_string_literal: true
Copy link
Contributor

@andyw8 andyw8 Dec 9, 2020

Choose a reason for hiding this comment

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

I don't think we need to include the frozen_string_literal line in the example.

@NabeelAhsen NabeelAhsen force-pushed the add-deep-linking-auth-support branch from b0698c3 to a6c11fc Compare December 9, 2020 15:35
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.

LGTM 👍

Copy link
Contributor

@greatestape greatestape left a comment

Choose a reason for hiding this comment

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

This is great! Nicely done.

@NabeelAhsen NabeelAhsen merged commit 6dccdb4 into master Dec 14, 2020
@NabeelAhsen NabeelAhsen deleted the add-deep-linking-auth-support branch December 14, 2020 19:30
@NabeelAhsen NabeelAhsen mentioned this pull request Dec 14, 2020
4 tasks
@NabeelAhsen NabeelAhsen temporarily deployed to rubygems December 15, 2020 14:57 Inactive
@netwire88
Copy link

Hi @NabeelAhsen, I noticed that the order of the concerns matter.

This doesn't work. If you go to a deep link in an incognito browser window (or any browser window not logged into Shopify), you get an ShopifyDomainNotFound error.

class AuthenticatedController < ApplicationController
  include ShopifyApp::EnsureAuthenticatedLinks
  include ShopifyApp::Authenticated
end

However, if I change it to this, it works. It'll ask for myshopify URL first and then login in + redirect.

class AuthenticatedController < ApplicationController
  include ShopifyApp::Authenticated
  include ShopifyApp::EnsureAuthenticatedLinks
end

Can you confirm?

@NabeelAhsen
Copy link
Contributor Author

Hi @NabeelAhsen, I noticed that the order of the concerns matter.

This doesn't work. If you go to a deep link in an incognito browser window (or any browser window not logged into Shopify), you get an ShopifyDomainNotFound error.

class AuthenticatedController < ApplicationController
  include ShopifyApp::EnsureAuthenticatedLinks
  include ShopifyApp::Authenticated
end

However, if I change it to this, it works. It'll ask for myshopify URL first and then login in + redirect.

class AuthenticatedController < ApplicationController
  include ShopifyApp::Authenticated
  include ShopifyApp::EnsureAuthenticatedLinks
end

Can you confirm?

@netwire88 Thank you for bringing this to our attention! This is definitely an oversight, and is being fixed with this PR: #1158

The PR i linked here will allow the order of EnsureAuthenticatedLinks and Authenticated to stay the same.

@netwire88
Copy link

Thanks @NabeelAhsen !

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.

10 participants