Skip to content

updated session docs#1172

Merged
nelsonwittwer merged 5 commits intomainfrom
nelsonwittwer/better_session_docs
Jul 19, 2023
Merged

updated session docs#1172
nelsonwittwer merged 5 commits intomainfrom
nelsonwittwer/better_session_docs

Conversation

@nelsonwittwer
Copy link
Contributor

Description

As noted in #1163, our docs for sessions are out of date. These changes better outline how sessions work within the ruby libraries.

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.

Just a nit!

For *non-embedded* apps, you can pass the cookies into `ShopifyAPI::Utils::SessionUtils.current_session_id(nil, cookies, true)` for online (user) sessions or `ShopifyAPI::Utils::SessionUtils.current_session_id(nil, cookies, false)` for offline (store) sessions.

#### Getting Session ID From Embedded Requests
For *embedded* apps, you can pass the auth header into `ShopifyAPI::Utils::SessionUtils.current_session_id(auth_header, nil, true)` for online (user) sessions or `ShopifyAPI::Utils::SessionUtils.current_session_id(auth_header, nil, false)` for offline (store) sessions.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it worth mentioning how to get the bearer token (e.g. calling authenticatedFetch from app bridge) here?

@vfonic
Copy link
Contributor

vfonic commented Jun 29, 2023

Where in the docs I can read more about auth_header variable that appears in several places in the docs out of nowhere?

@nelsonwittwer
Copy link
Contributor Author

Where in the docs I can read more about auth_header variable that appears in several places in the docs out of nowhere?

This is the HTTP_AUTHORIZATION header. You can see it used in the shopify_app.

@nelsonwittwer nelsonwittwer merged commit f341d99 into main Jul 19, 2023
@nelsonwittwer nelsonwittwer deleted the nelsonwittwer/better_session_docs branch July 19, 2023 15:31
@vfonic
Copy link
Contributor

vfonic commented Jul 19, 2023

@nelsonwittwer do you think this would be something worth including somewhere in the docs? Thanks!

This might be better line of code to link to:
https://github.com/Shopify/shopify_app/blob/main/lib/shopify_app/controller_concerns/login_protection.rb#L53

As someone who has been actively building Shopify apps for the past several years, it is still not clear how I can make Shopify API calls with the latest version of shopify_api gem outside of the request-response scope (in bg jobs for example).

EDIT: I have the code written somewhere that works. If you need it, I can dig it up and share it here so that you can add it to documentation?

@kyreeves
Copy link

@vfonic I'm having trouble making requests in background jobs as well. Have you had any luck with this since your last comment?

@vfonic
Copy link
Contributor

vfonic commented Aug 10, 2023

Yes, I have it fully working. You can do:

shop.with_shopify_session do
  # any code that makes requests to Shopify REST or GraphQL API
end

or

session = ShopifyAPI::Auth::Session.new(shop: shop.shopify_domain, access_token: shop.shopify_token, scope: shop.access_scopes)
# and then
client = ShopifyAPI::Clients::Graphql::Admin.new(session: session)
client.query(query:, variables:).body
# or
ShopifyAPI::Context.activate_session(session)
ShopifyAPI::Webhook.all.as_json
# or whatever you need

Something along these lines should work. In both cases shop is an ActiveRecord instance of Shop model as per ShopifyApp gem.

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.

4 participants