-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
123 lines (115 loc) · 3.16 KB
/
Copy pathindex.js
File metadata and controls
123 lines (115 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const iframeHtml = ({ clientId, js }) => `
<html lang="en">
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="${clientId}.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js"></script>
<style>
body {
margin: 0px;
padding: 0px;
border: 0px;
}
</style>
</head>
<body>
<div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
<script>
${js}
</script>
</body>
</html>
`;
const signInJs = `
window.signIns = new EventTarget();
function onSignIn(googleUser) {
signIns.dispatchEvent(new CustomEvent("signIn", { detail: googleUser }));
};
`;
const signOutJs = `
window.signOuts = new EventTarget();
document.body.onload = trySignOut;
function trySignOut() {
if(gapi !== undefined && gapi.auth2.getAuthInstance().isSignedIn !== undefined) {
gapi.auth2
.getAuthInstance()
.signOut()
.then(() => signOuts.dispatchEvent(new CustomEvent("signOut")))
.catch(e => setTimeout(trySignOut, 100));
} else {
setTimeout(trySignOut, 100);
}
}
`;
class GoogleSignInButton extends HTMLElement {
constructor() {
super()
this._clientId = null;
this._profile = null;
this._buttonKind = null;
// Create a shadow root
const shadow = this.attachShadow({mode: 'open'});
// Create spans
this.iframe = document.createElement('iframe');
this.iframe.width = "120px";
this.iframe.height = "36px";
this.iframe.style = "border: 0px; padding: 0px; margins: 0px;";
shadow.appendChild(this.iframe);
}
connectedCallback() {
this.iframe.contentWindow.document.open();
this.iframe.contentWindow.document.write(iframeHtml({
clientId: this._clientId,
js: signInJs,
}));
this.iframe.contentWindow.document.close();
this.iframe.contentWindow.onload = () => {
this.iframe.contentWindow.signIns.addEventListener("signIn", ({ detail: googleUser }) => {
let profile = googleUser.getBasicProfile();
this._profile = {
id: profile.getId(),
idToken: googleUser.getAuthResponse().id_token,
name: profile.getName(),
givenName: profile.getGivenName(),
familyName: profile.getFamilyName(),
imageUrl: profile.getImageUrl(),
email: profile.getEmail(),
};
this.dispatchEvent(new CustomEvent("signIn"))
});
};
}
get profile() {
return this._profile;
}
set clientId(id) {
this._clientId = id;
}
get clientId() {
return this._clientId;
}
}
customElements.define("google-signin-button", GoogleSignInButton);
module.exports = {
signOut: ({ port, clientId }) => {
let iframe = document.createElement('iframe');
iframe.width = "120px";
iframe.height = "36px";
iframe.style = "border: 0px; width: 0px; height: 0px; padding: 0px; margins: 0px;";
document.body.appendChild(iframe);
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(iframeHtml({
clientId: clientId,
js: signOutJs,
}));
iframe.contentWindow.document.close();
tryListen();
function tryListen() {
if(iframe.contentWindow.signOuts !== undefined) {
iframe.contentWindow.signOuts.addEventListener("signOut", () => port.send(clientId));
} else {
setTimeout(tryListen, 50)
}
};
}
};