Ad

Meteor-shopify User Creation/ Login After Auth Callback

- 1 answer

Assuming I want to create users upon authorizing the app, how would I grab their email during the onAuth callback...? Looks like the callback assumes the user is already logged in. Am I thinking about it correctly?

I noticed when installing the Fishbowl Prizes app, after auth I can click on the accounts tab and see that all my account info is pre-populated from my shopify store account (name, email, address, etc).

Ad

Answer

I'm not sure if I should go by the title or the content of the post in terms of answering your question, so I'll provide a very simple example of how to get the info from the API and do something with it here.

I have provided a more in depth answer related specifically to grabbing the details from the API for user account creation here: https://github.com/froatsnook/meteor-shopify/issues/15#issuecomment-177413630

Looks like the callback assumes the user is already logged in.

The userId param is undefined if there is no user. If your onAuth operations don't need to do anything with the user, you can just leave it out of the params. In your case you'll just want to handle it conditionally using an if/else block:

if(!userId){
  // do stuff
} else {
  // do other stuff
}

On to the example of grabbing those details from the API:

All the prepopulated information you are seeing is available from the Shopify API in the shop object. You already have the access token when onAuth callbacks are fired, so you can just grab it from the API immediately after you have inserted the shop's Keyset.

For the sake of simplicity, in this example we'll assume the user already exists and is logged in. In your server-side onAuth callback (after you have inserted the keyset) you can do something like this to add those fields to the user's profile object:

Shopify.onAuth(function(access_token, authConfig, userId) {
  var shopUUID = uuid.new(); // Not secure to name keyset same as the shop!
  Shopify.addKeyset(shopUUID, {
    access_token: access_token
  });

  var api = new Shopify.API({
    shop: authConfig.shop,
    keyset: shopUUID
  });
  // get the Shop object from the API
  var shopObj = api.getShop();
  var userInfo = {
    'profile.name': shopObj.shop_owner,
    'profile.email': shopObj.email,
    'profile.phone': shopObj.phone,
    'profile.shopName': shopObj.name
  };
  Meteor.users.update({_id: userId}, {$set: userInfo})
});

Then you can use them in templates like this:

{{currentUser.profile.name}} or {{currentUser.profile.email}}

Or in functions like so:

var realName = Meteor.user().profile.name

or

var userEmail = Meteor.user().profile.email etc

For a more about using this data for user creation, see my explanation here: https://github.com/froatsnook/meteor-shopify/issues/15#issuecomment-177413630

Ad
source: stackoverflow.com
Ad