-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathweb.ts
More file actions
163 lines (133 loc) · 4.22 KB
/
Copy pathweb.ts
File metadata and controls
163 lines (133 loc) · 4.22 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { WebPlugin } from '@capacitor/core';
import { GoogleAuthPlugin, InitOptions, User } from './definitions';
export class GoogleAuthWeb extends WebPlugin implements GoogleAuthPlugin {
gapiLoaded: Promise<void>;
options: InitOptions;
constructor() {
super();
}
loadScript() {
if (typeof document === 'undefined') {
return;
}
const scriptId = 'gapi';
const scriptEl = document?.getElementById(scriptId);
if (scriptEl) {
return;
}
const head = document.getElementsByTagName('head')[0];
const script = document.createElement('script');
script.type = 'text/javascript';
script.defer = true;
script.async = true;
script.id = scriptId;
script.onload = this.platformJsLoaded.bind(this);
script.src = 'https://apis.google.com/js/platform.js';
head.appendChild(script);
}
initialize(
_options: Partial<InitOptions> = {
clientId: '',
scopes: [],
grantOfflineAccess: false,
}
) {
if (typeof window === 'undefined') {
return;
}
const metaClientId = (document.getElementsByName('google-signin-client_id')[0] as any)?.content;
const clientId = _options.clientId || metaClientId || '';
if (!clientId) {
console.warn('GoogleAuthPlugin - clientId is empty');
}
this.options = {
clientId,
grantOfflineAccess: _options.grantOfflineAccess ?? false,
scopes: _options.scopes || [],
};
this.gapiLoaded = new Promise((resolve) => {
// HACK: Relying on window object, can't get property in gapi.load callback
(window as any).gapiResolve = resolve;
this.loadScript();
});
this.addUserChangeListener();
}
platformJsLoaded() {
gapi.load('auth2', () => {
const clientConfig: gapi.auth2.ClientConfig = {
client_id: this.options.clientId,
};
if (this.options.scopes.length) {
clientConfig.scope = this.options.scopes.join(' ');
}
gapi.auth2.init(clientConfig);
(window as any).gapiResolve();
});
}
async signIn() {
return new Promise<User>(async (resolve, reject) => {
try {
let serverAuthCode: string;
const needsOfflineAccess = this.options.grantOfflineAccess ?? false;
if (needsOfflineAccess) {
const offlineAccessResponse = await gapi.auth2.getAuthInstance().grantOfflineAccess();
serverAuthCode = offlineAccessResponse.code;
} else {
await gapi.auth2.getAuthInstance().signIn();
}
const googleUser = gapi.auth2.getAuthInstance().currentUser.get();
if (needsOfflineAccess) {
// HACK: AuthResponse is null if we don't do this when using grantOfflineAccess
await googleUser.reloadAuthResponse();
}
const user = this.getUserFrom(googleUser);
user.serverAuthCode = serverAuthCode;
resolve(user);
} catch (error) {
reject(error);
}
});
}
async addScopes(scopes: string[]) {
// TODO: web
return;
}
async removeScopes(scopes: string[]) {
// TODO: web
return;
}
async refresh() {
const authResponse = await gapi.auth2.getAuthInstance().currentUser.get().reloadAuthResponse();
return {
accessToken: authResponse.access_token,
idToken: authResponse.id_token,
refreshToken: '',
};
}
async signOut() {
return gapi.auth2.getAuthInstance().signOut();
}
private async addUserChangeListener() {
await this.gapiLoaded;
gapi.auth2.getAuthInstance().currentUser.listen((googleUser) => {
this.notifyListeners('userChange', googleUser.isSignedIn() ? this.getUserFrom(googleUser) : null);
});
}
private getUserFrom(googleUser: gapi.auth2.GoogleUser) {
const user = {} as User;
const profile = googleUser.getBasicProfile();
user.email = profile.getEmail();
user.familyName = profile.getFamilyName();
user.givenName = profile.getGivenName();
user.id = profile.getId();
user.imageUrl = profile.getImageUrl();
user.name = profile.getName();
const authResponse = googleUser.getAuthResponse(true);
user.authentication = {
accessToken: authResponse.access_token,
idToken: authResponse.id_token,
refreshToken: '',
};
return user;
}
}