Просмотр исходного кода

switch from deprecated gplus to google login. disable custom button

Andrii Zadorozhnii 8 лет назад
Родитель
Сommit
63917b8e92
3 измененных файлов с 46 добавлено и 35 удалено
  1. 15 16
      priv/google_sdk.dtl
  2. 2 1
      src/avz.erl
  3. 29 18
      src/google.erl

+ 15 - 16
priv/google_sdk.dtl

@@ -1,28 +1,27 @@
 <script type="text/javascript">
+var m = document.createElement('meta');
+m.name = "google-signin-client_id"; m.content = "{{clientid}}";
+document.getElementsByTagName('head')[0].appendChild(m);
+
+window.onbeforeunload = function(e){ gapi.auth2.getAuthInstance().signOut(); }
 
-function plusoneCallback(auth){
-  if (!auth['g-oauth-window']) { console.log('skip autologin'); }
-  else if(auth['access_token']) {
-    gapi.client.load('oauth2', 'v2', function(){
-      gapi.client.oauth2.userinfo.get().execute(function(oauthResp){
-            if(plusLogin) plusLogin(oauthResp);
-     }); });
-  } else if(auth['error']) { console.log('error'); }
-}
 
 function render(){
-  gapi.signin.render('{{loginbtnid}}', {
-    'callback':'plusoneCallback',
-    'clientid':'{{clientid}}',
-    'cookiepolicy': '{{cookiepolicy}}',
-    'requestvisibleactions':'http://schemas.google.com/AddActivity',
-    'scope':'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email '
+  gapi.signin2.render('{{loginbtnid}}', {
+    'width'     : '{{width}}',
+    'height'    : '{{height}}',
+    'longtitle' : '{{longtitle}}',
+    'theme'     : '{{theme}}',
+    // don't pass user.getAuthResponse().id_token
+    // basic profile is enough
+    'onsuccess' : function(user){ if(gLogin) gLogin(user.getBasicProfile()) },
+    'onfailure' : function(err) { if(gLoginFail) gLoginFail(err) }
   });
 }
 
 (function() {
   var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
-  po.src = 'https://apis.google.com/js/client:plusone.js?parsetags=explicit&onload=render';
+  po.src = 'https://apis.google.com/js/platform.js?onload=render';
   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
 })();
 </script>

+ 2 - 1
src/avz.erl

@@ -24,7 +24,8 @@ event({login, #avz_user{}=U, N}) -> Updated = merge(U,N), kvs:put(Updated), logi
 event({Method,Event}) -> Method:event({Method,Event});
 event(Ev) ->  wf:info(?MODULE,"Page Event ~p",[Ev]).
 
-api_event(plusLogin, Args, Term) -> google:api_event(plusLogin, Args, Term);
+api_event(gLogin, Args, Term) -> google:api_event(gLogin, Args, Term);
+api_event(gLoginFail, Args, Term) -> google:api_event(gLoginFail, Args, Term);
 api_event(fbLogin, Args, Term)   -> facebook:api_event(fbLogin, Args, Term);
 api_event(winLogin, Args, Term)  -> microsoft:api_event(winLogin, Args, Term);
 api_event(Name, Args, Term)      -> wf:info(?MODULE,"Unknown API event: ~p ~p ~p",[Name, Args, Term]).

+ 29 - 18
src/google.erl

@@ -6,39 +6,50 @@
 -include_lib("avz/include/avz_user.hrl").
 -compile(export_all).
 -export(?API).
--define(GPLUS_CLIENT_ID,     application:get_env(web, gplus_client_id,    [])).
--define(GPLUS_COOKIE_POLICY, application:get_env(web, gplus_cookiepolicy, [])).
 
-api_event(plusLogin, Args, _)-> {JSArgs} = ?AVZ_JSON:decode(list_to_binary(Args)), avz:login(google, JSArgs).
+-define(G_CLIENT_ID,     application:get_env(avz, g_client_id,    [])).
+-define(G_COOKIE_POLICY, application:get_env(avz, g_cookiepolicy, [])).
+-define(G_BTN_ID,        application:get_env(avz, g_btn_id, "gloginbtn")).
+-define(G_BTN_HEIGHT,    application:get_env(avz, g_btn_height, 50)).
+-define(G_BTN_WIDTH,     application:get_env(avz, g_btn_width, 240)).
+-define(G_BTN_THEME,     application:get_env(avz, g_btn_theme, "light")).
+-define(G_BTN_LONGTITLE, application:get_env(avz, g_btn_longtitle, true)).
+
+api_event(gLogin, Args, _) -> {JSArgs} = ?AVZ_JSON:decode(list_to_binary(Args)), avz:login(google, JSArgs);
+api_event(gLoginFail, Args, _) -> wf:info(?MODULE, "Login failed ~p~n", [Args]).
 
 registration_data(Props, google, Ori)->
-    Id = proplists:get_value(<<"id">>, Props),
-    _Name = proplists:get_value(<<"name">>, Props),
-    Image = proplists:get_value(<<"picture">>, Props),
-    GivenName = proplists:get_value(<<"given_name">>, Props),
-    FamilyName = proplists:get_value(<<"family_name">>, Props),
+    Id = proplists:get_value(<<"Eea">>, Props),
+    Name = proplists:get_value(<<"ig">>, Props),
+    Image = proplists:get_value(<<"Paa">>, Props),
+    GivenName = proplists:get_value(<<"ofa">>, Props),
+    FamilyName = proplists:get_value(<<"wea">>, Props),
     Email = email_prop(Props,google),
-    Ori#avz_user{   id = Email,
-                display_name = proplists:get_value(<<"displayName">>, Props),
+    Ori#avz_user{id = Email,
+                display_name = Name,
                 images = avz:update({google_avatar,Image},Ori#avz_user.images),
                 email = Email,
                 names = GivenName,
                 surnames = FamilyName,
                 tokens = avz:update({google,Id},Ori#avz_user.tokens),
                 register_date = os:timestamp(),
-                sex = proplists:get_value(<<"gender">>, Props),
+                % sex = proplists:get_value(<<"gender">>, Props),
                 status = ok }.
 
-email_prop(Props, _) -> proplists:get_value(<<"email">>, Props).
+email_prop(Props, _) -> proplists:get_value(<<"U3">>, Props).
 
-login_button()-> application:get_env(avz,google_button,#panel{id=plusloginbtn, class=["btn-group"], body=
-    #link{class=[btn, "btn-google-plus", "btn-large","btn-lg"],
-        body=[#i{class=[fa,"fa-google-plus","fa-lg","icon-google-plus", "icon-large"]}, <<"Google">>] }}).
+login_button()-> #panel{id=?G_BTN_ID}.
 
 event(_) -> ok.
 callback() -> ok.
 sdk() ->
-    wf:wire(#api{name=plusLogin, tag=plus}),
+    wf:wire(#api{name=gLogin, tag=plus}),
+    wf:wire(#api{name=gLoginFail, tag=plus}),
     #dtl{bind_script=false, file="google_sdk", ext="dtl", folder="priv/static/js",
-        bindings=[{loginbtnid, plusloginbtn},{clientid, ?GPLUS_CLIENT_ID},{cookiepolicy, ?GPLUS_COOKIE_POLICY}]}.
-
+        bindings=[{loginbtnid, ?G_BTN_ID},
+          {clientid,    ?G_CLIENT_ID},
+          {cookiepolicy,?G_COOKIE_POLICY}, 
+          {height,      ?G_BTN_HEIGHT},
+          {width,       ?G_BTN_WIDTH},
+          {theme,       ?G_BTN_THEME},
+          {longtitle,   ?G_BTN_LONGTITLE} ]}.