Search code examples
javascriptjqueryapigmail

Troubles with Google API sessionParams not working mixed JavaScript & JQuery


Im trying make this code work, but it's not working, so I need your help. I want it to check if someone is logged to Gmail if yes then force him to use Gmail as a mailto client if not then use default mailto client. My previous version is there, and it's working more or less: http://www.pazaak.nazwa.pl/projekty/gmail/index2.html

But I was asked to rewrite it to jQuery. I create something like this:

(function () {
      var po = document.createElement("script");
      po.type = "text/javascript"; po.async = true;
      po.src = "https://apis.google.com/js/client:plusone.js?onload=OnLoadCallback";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(po, s);
})();

$( document ).ready(function(){
var sessionParams = {
       'client_id': 'myclientID',
       'session_state': null
   }
  $( ".mail2" ).click(function(e){
    console.log('test');
  gapi.auth.checkSessionState(sessionParams, $(function(stateMatched) {
    if (stateMatched == true) {
      console.log('jfgh');
      return true;
      } else {
        e.preventDefault();
        console.log('g');
        window.open("https://mail.google.com/mail/u/0/#inbox?compose=new");
        return false;
      }
    }));
});
});

Yet something is still wrong. I suspect that main problem is that SessionParams is not working properly. I find another code with SessionParams online, but cant post it cos I need more reputation to put 2 links. As I said above I need to have some of my code in jQuery.


Solution

  • You're wrapping the callback in $(), which is incorrect:

    gapi.auth.checkSessionState(sessionParams, function (stateMatched) {
                if (stateMatched == true) {
                    console.log('jfgh');
                    return true;
                } else {
                    e.preventDefault();
                    console.log('g');
                    window.open("https://mail.google.com/mail/u/0/#inbox?compose=new");
                    return false;
                }
            });