angular 2 does not trigger callback function after successful google login -
import {component,directive,oninit,ngzone} 'angular2/core'; declare const gapi:any; declare const $:any; @component({ selector: 'mysite', templateurl:'./app/template.html' }) export class test{ userauthtoken; userdisplayname; constructor(private zone: ngzone){ gapi.load('auth2',this.initnow); this.zone.run(() => { console.log(this); $.proxy(this.ongoogleloginsuccess, this); }); } initnow(){ gapi.auth2.init({client_id:'9511021809-qqke9m46imnmrged8u7u66ilj168bi9t.apps.googleusercontent.com'}); } ngafterviewinit() { gapi.signin2.render( this.googleloginbuttonid,{ "onsuccess": this.ongoogleloginsuccess, "scope": "profile", "theme": "dark" }); } public ongoogleloginsuccess(loggedinuser) { this.userauthtoken = loggedinuser.getauthresponse().id_token; this.userdisplayname = loggedinuser.getbasicprofile().getname(); console.log("ongoogleloginsuccess called: ",this.userauthtoken,this.userdisplayname); } }
template.html
<div id="{{googleloginbuttonid}}"></div>
ongoogleloginsuccess function not getting called. can suggest, missing in code?
i trying integrate google login website. nothing getting called after google login page disappear. suresh
you forgot set id in googleloginbuttonid
. set , button should work.
export class test{ userauthtoken; userdisplayname; googleloginbuttonid = 'google_login_button_id'; ... }
but, if issue, button shouldn't appear @ all!! so, if did set googleloginbuttonid
forgot add in question. problem in line
$.proxy(this.ongoogleloginsuccess, this);
i not jquery
guy, looked proxy
, says in description:
description: takes function , returns new 1 have particular context.
so, need save new returned function , pass onsuccess
callback:
proxyedsigninsuccess; constructor(private zone: ngzone){ this.zone.run(() => { this.proxyedsigninsuccess = $.proxy(this.ongoogleloginsuccess, this); }); } ... ngafterviewinit() { gapi.signin2.render( this.googleloginbuttonid,{ "onsuccess": this.proxyedsigninsuccess, "scope": "profile", "theme": "dark" }); }
or, make sure. maybe try instead:
ngafterviewinit() { gapi.signin2.render( this.googleloginbuttonid,{ "onsuccess": (user) => this.zone.run(() => this.ongoogleloginsuccess(user)), "scope": "profile", "theme": "dark" }); }
Comments
Post a Comment