0

What I really need is to load google apis client library for javascript when a user is logged in using google service. If a user is logged in using password or other external services, the library is not loaded.

Is this possible by any means?

Thanks.

Fei
  • 769
  • 2
  • 7
  • 13

2 Answers2

1

If you want to load the library from an external URL after the user has logged in, you could try something like this (put this anywhere in your client code):

Tracker.autorun(function(c) {
  var user = Meteor.user();

  // replace this with the appropriate check for a google account
  if (user && user.profile && user.profile.isGoogle) {
    // stop the autorun now that the user is logged in
    c.stop();

    // load the api library
    $.ajax({
      url: '//url-to-fancy-google-api.google.com',
      dataType: 'script',
      cache: true
    });
  }
});
David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • Nice - 2 questions: 1. What if a user logs in with password, out and in with google - will the tracker catch that? (c.stop()) 2. $.getscript vs $.ajax - any thoughts? – Rune Jeppesen Sep 15 '14 at 08:27
  • 1) That case is covered because the `stop` doesn't fire until the user logs in with a google account. 2) `getScript` is shorthand for `ajax`, however with `getScript`, it defaults to `cache: false` (usually that isn't what I want but it could be in your case). – David Weldon Sep 15 '14 at 17:55
  • Thanks. In my case, the user variable only contains an _id. The user.profile field is undefined. Any idea why? – Fei Sep 16 '14 at 05:56
  • Oh, I override the onCreateUser hook. Now I get the default profile field, but it still doesn't contain isGoogle. Is that something I should add? Thanks. – Fei Sep 16 '14 at 06:33
  • I just made up `isGoogle` as an example of a way you could identify users who used google to authenticate. I'm not sure if that's something you'd need to set yourself during account creation or something you could just figure out on the fly. – David Weldon Sep 16 '14 at 17:03
0

It is possible, use anti:modules package. First add it to the app:

meteor add anti:modules

Then create /layers folder inside your project and place your optional files in its subfolder:

/
  layers
    googleUser
      someFile.module.js
      anotherFile.module.js
      ...

Then in your code create a global module:

theApp = Module('$global').as('myApp');

and load it when necessary:

theApp.require('googleUser', function () {
  console.log('googleUser code loaded');
});
Hubert OG
  • 19,314
  • 7
  • 45
  • 73