Search code examples
clojurescriptgoogle-closure-compilergoogle-authentication

Google auth2 clojurescript externs


So, I've managed to get google sign-in to work in my development cljs build by including https://apis.google.com/js/platform.js in my index.html and simply using javascript interop to emulate the instructions given here for an ordinary javascript client: https://developers.google.com/identity/sign-in/web/sign-in. However I can't get a production build with :optimizations :advanced to work.

In all my code that interacts with the gapi.auth2 object, I've used (aget object "propertyName") which seems to get me as far as calling calling the cljs equivalent of gapi.load("auth2") all the way to accessing the 'listen' function on gapi.auth2.getAuthInstance().currentUser.

When I try to call this 'listen' function, I get a "cannot read property 'add' of undefined" error. I don't know what object this 'add' property is supposed to exist on, if I need to create an extern for this object, or how to do so.

Am I approaching gapi.auth2 integration in a sane way in the first place?


Solution

  • I've made a small custom google sign-in with https://apis.google.com/js/platform.js.

    Here is what I need for my externs.js file.

    var gapi = {};
    gapi.load = function (){};
    gapi.auth2 = {};
    gapi.auth2.init = function(){};
    gapi.auth2.getAuthInstance = function (){};
    

    Basically, there's a global js object gapi and based on the sample codes on that google website, gapi has method load and attribute auth2. Object referenced by gapi.auth has init and getAuthInstance methods.

    I don't recommend using aget to get a object's property. aget is meant to be used on js array. For JavaScript objects, use goog.object/get or the multi-arity goog.object/getValueByKeys.

    For object whose type I don't know like your gapi.auth2.getAuthInstance().currentUser, I call the listen on it using js-invoke function.

     (let [callback-fn (fn [_] ...)
           auth-inst  (.getAuthInstance js/gapi.auth2)
           currentUser (goog.object/get auth-inst "currentUser")]
      (js-invoke currentUser "listen" callback-fn))