Skip to content

Commit 5c33270

Browse files
Merge pull request #795 from marucjmar/protect-google-ref
Skip call to google ref when is undefined
2 parents 322bf97 + 63ecb2e commit 5c33270

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

projects/lib/src/directives/google-signin-button.directive.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Directive, ElementRef, Input } from '@angular/core';
22
import { take } from 'rxjs/operators';
33
import { SocialAuthService } from '../socialauth.service';
4+
import { isGoogleAccountsDefined } from '../utils/google';
45

56
@Directive({
67
// eslint-disable-next-line @angular-eslint/directive-selector
@@ -39,7 +40,7 @@ export class GoogleSigninButtonDirective {
3940
'Please note .. max-width 400 , min-width 200 ' +
4041
'(https://developers.google.com/identity/gsi/web/tools/configurator)'
4142
);
42-
} else {
43+
} else if (isGoogleAccountsDefined()) {
4344
google.accounts.id.renderButton(el.nativeElement, {
4445
type: this.type,
4546
size: this.size,

projects/lib/src/providers/google-login-provider.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SocialUser } from '../entities/social-user';
33
import { EventEmitter } from '@angular/core';
44
import { BehaviorSubject } from 'rxjs';
55
import { filter, skip, take } from 'rxjs/operators';
6+
import { getGoogleAccountsOrThrow, isGoogleAccountsDefined } from '../utils/google';
67

78
export interface GoogleInitOptions {
89
/**
@@ -69,6 +70,8 @@ export class GoogleLoginProvider extends BaseLoginProvider {
6970
GoogleLoginProvider.PROVIDER_ID,
7071
this.getGoogleLoginScriptSrc(lang),
7172
() => {
73+
if (!isGoogleAccountsDefined()) return;
74+
7275
google.accounts.id.initialize({
7376
client_id: this.clientId,
7477
auto_select: autoLogin,
@@ -134,7 +137,7 @@ export class GoogleLoginProvider extends BaseLoginProvider {
134137

135138
refreshToken(): Promise<SocialUser | null> {
136139
return new Promise((resolve, reject) => {
137-
google.accounts.id.revoke(this._socialUser.value.id, (response) => {
140+
getGoogleAccountsOrThrow().id.revoke(this._socialUser.value.id, (response) => {
138141
if (response.error) reject(response.error);
139142
else resolve(this._socialUser.value);
140143
});
@@ -169,7 +172,7 @@ export class GoogleLoginProvider extends BaseLoginProvider {
169172
} else if (!this._accessToken.value) {
170173
reject('No access token to revoke');
171174
} else {
172-
google.accounts.oauth2.revoke(this._accessToken.value, () => {
175+
getGoogleAccountsOrThrow().oauth2.revoke(this._accessToken.value, () => {
173176
this._accessToken.next(null);
174177
resolve();
175178
});
@@ -186,7 +189,7 @@ export class GoogleLoginProvider extends BaseLoginProvider {
186189
}
187190

188191
async signOut(): Promise<void> {
189-
google.accounts.id.disableAutoSelect();
192+
getGoogleAccountsOrThrow().id.disableAutoSelect();
190193
this._socialUser.next(null);
191194
}
192195

projects/lib/src/utils/google.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
export const isGoogleAccountsDefined = (): boolean => {
3+
return typeof window.google?.accounts !== 'undefined';
4+
}
5+
6+
export const assertGoogleAccountsDefined = (): void => {
7+
if (!isGoogleAccountsDefined()) {
8+
throw new Error('Google Accounts API is undefined');
9+
}
10+
}
11+
12+
export const getGoogleAccountsOrThrow = (): typeof google.accounts=> {
13+
assertGoogleAccountsDefined();
14+
15+
return window.google.accounts;
16+
}

0 commit comments

Comments
 (0)