在跟随@JeremyDanyow的示例走了几个弯路后,我想出了这个方法
对于简单的用法,它可以正常工作,但需要帮助...
当有另一个使用Google登录的窗口打开时,iframe中Google添加的某些内容会出现错误加载(这似乎不会破坏它)
监听器最多只能工作几次登录/注销
希望有人能改进这个方法。
google-signin-button.js
import { inject, noView, bindable } from 'aurelia-framework';
import { LogManager } from 'aurelia-framework';
const Console = LogManager.getLogger('google-signin-button');
function preparePlatform(): Promise<Function> {
const script = document.createElement('script');
script.src = `https://apis.google.com/js/platform.js?onload=gapi_ready`;
script.async = true;
script.defer = true;
document.head.appendChild(script);
return new Promise(resolve => window['gapi_ready'] = resolve);
}
@noView
@inject(Element)
export class GoogleSigninButton {
@bindable authenticated = (signedIn: Boolean) => { };
@bindable authorized = (GoogleUser: any) => { };
@bindable scope = 'profile email';
@bindable clientId = 'none';
@bindable theme = 'dark';
@bindable width = 240;
@bindable height = 50;
public element: Element;
constructor(element) {
this.element = element;
}
public wasAuthenticated: Boolean;
sendAuthenticated(signedIn: Boolean) {
if (signedIn !== this.wasAuthenticated) {
this.authenticated(signedIn);
this.wasAuthenticated = signedIn;
}
}
public wasAuthorized: any;
sendAuthorized(googleUser: any) {
if (googleUser !== this.wasAuthorized) {
this.authorized(googleUser);
this.wasAuthorized = googleUser;
this.sendAuthenticated(true);
}
}
attached() {
preparePlatform()
.then(() => {
window['gapi'].load('auth2', () => {
window['gapi'].auth2.init({
client_id: this.clientId
})
.then(
(googleAuth: any) => {
googleAuth.isSignedIn.listen((signedIn: Boolean) => {
this.sendAuthenticated(signedIn);
});
googleAuth.currentUser.listen((googleUser: any) => {
this.sendAuthorized(googleUser);
});
window['gapi'].signin2.render(this.element, {
scope: this.scope,
width: this.width,
height: this.height,
longtitle: true,
theme: this.theme,
onsuccess: (googleUser: any) => {
this.sendAuthorized(googleUser);
},
onfailure: (error: any) => {
Console.error('gapi.signin2.render failure', error);
}
});
},
(errObj: any) => {
Console.error('gapi.auth2.init -> errObj', errObj);
}
);
});
});
}
}
some-usage.js
import environment from '../environment';
import { LogManager } from 'aurelia-framework';
const Console = LogManager.getLogger('Login');
import { inject } from 'aurelia-framework';
import { AuthService } from 'aurelia-authentication';
import { EventAggregator } from 'aurelia-event-aggregator';
import './login.scss';
@inject(AuthService, EventAggregator)
export class Login {
public authService: AuthService;
public eventAggregator: EventAggregator;
public googleSigninClientID: string = 'none';
constructor(authService: AuthService, eventAggregator: EventAggregator) {
this.eventAggregator = eventAggregator;
this.authService = authService;
this.googleSigninClientID = environment.googleSigninClientID;
};
isAuthenticated(signedIn: Boolean) {
Console.warn('isAuthenticated', signedIn);
}
isAuthorized(googleUser: any) {
Console.warn('isAuthorized', googleUser);
}
}
some-usage.html
<template>
<require from="../resources/elements/google-signin-button"></require>
<section>
<div class="container-fluid">
<center>
<google-signin-button client-id.bind="googleSigninClientID" authenticated.bind="isAuthenticated" authorized.bind="isAuthorized"> </google-signin-button>
</center>
</div>
</section>
</template>