Skip to content

Commit 0c3e2ee

Browse files
committed
Listener for user state change
1 parent a9a5101 commit 0c3e2ee

File tree

3 files changed

+75
-26
lines changed

3 files changed

+75
-26
lines changed

demo/src/app/app.component.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ export class AppComponent implements OnInit {
1616

1717
ngOnInit() {
1818
SplashScreen.hide();
19+
20+
GoogleAuth.addListener('userChange', (googleUser) => {
21+
console.log("userChange:", googleUser);
22+
});
1923
}
2024

2125
async signIn() {
2226
let googleUser = await GoogleAuth.signIn();
2327
this.username = googleUser.name;
24-
console.log(googleUser);
28+
console.log("signIn:", googleUser);
2529
}
2630

2731
async refreshToken() {
2832
let response = await GoogleAuth.refresh();
29-
console.log(response);
33+
console.log("refresh:", response);
3034
}
3135

3236
async signOut() {

src/user.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export interface User {
2+
id: string;
3+
email: string;
4+
5+
name: string;
6+
familyName: string;
7+
givenName: string;
8+
imageUrl: string;
9+
10+
serverAuthCode: string;
11+
authentication: Authentication;
12+
}
13+
14+
export interface Authentication {
15+
accessToken: string;
16+
idToken: string;
17+
}

src/web.ts

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
import { WebPlugin } from '@capacitor/core';
22
import { GoogleAuthPlugin } from './definitions';
3+
import { User, Authentication } from './user';
4+
35
// @ts-ignore
46
import config from '../../../../../capacitor.config.json';
57

68
export class GoogleAuthWeb extends WebPlugin implements GoogleAuthPlugin {
9+
10+
gapiLoaded: Promise<void>;
11+
12+
get webConfigured(): boolean {
13+
return document.getElementsByName('google-signin-client_id').length > 0;
14+
}
15+
716
constructor() {
817
super({
918
name: 'GoogleAuth',
1019
platforms: ['web']
1120
});
1221

13-
if (this.webConfigured)
22+
if (!this.webConfigured)
23+
return;
24+
25+
this.gapiLoaded = new Promise(resolve => {
26+
// HACK: Relying on window object, can't get property in gapi.load callback
27+
(window as any).gapiResolve = resolve;
1428
this.initialize();
15-
}
29+
});
1630

17-
get webConfigured(): boolean {
18-
return document.getElementsByName('google-signin-client_id').length > 0;
31+
this.addUserChangeListener();
1932
}
2033

2134
initialize() {
@@ -30,7 +43,7 @@ export class GoogleAuthWeb extends WebPlugin implements GoogleAuthPlugin {
3043
}
3144

3245
platformJsLoaded() {
33-
gapi.load('auth2', async () => {
46+
gapi.load('auth2', () => {
3447
const clientConfig: gapi.auth2.ClientConfig = {
3548
client_id: (document.getElementsByName('google-signin-client_id')[0] as any).content
3649
};
@@ -40,15 +53,16 @@ export class GoogleAuthWeb extends WebPlugin implements GoogleAuthPlugin {
4053
}
4154

4255
gapi.auth2.init(clientConfig);
56+
(window as any).gapiResolve();
4357
});
4458
}
4559

4660
async signIn(): Promise<any> {
4761
return new Promise(async (resolve, reject) => {
4862
try {
49-
const user: any = {};
50-
63+
var serverAuthCode: string;
5164
var needsOfflineAccess = false;
65+
5266
try {
5367
needsOfflineAccess = config.plugins.GoogleAuth.serverClientId != null;
5468
} catch {
@@ -57,7 +71,7 @@ export class GoogleAuthWeb extends WebPlugin implements GoogleAuthPlugin {
5771

5872
if (needsOfflineAccess) {
5973
const offlineAccessResponse = await gapi.auth2.getAuthInstance().grantOfflineAccess();
60-
user.serverAuthCode = offlineAccessResponse.code;
74+
serverAuthCode = offlineAccessResponse.code;
6175
} else {
6276
await gapi.auth2.getAuthInstance().signIn();
6377
}
@@ -69,29 +83,16 @@ export class GoogleAuthWeb extends WebPlugin implements GoogleAuthPlugin {
6983
await googleUser.reloadAuthResponse();
7084
}
7185

72-
const authResponse = googleUser.getAuthResponse(true);
73-
74-
const profile = googleUser.getBasicProfile();
75-
user.email = profile.getEmail();
76-
user.familyName = profile.getFamilyName();
77-
user.givenName = profile.getGivenName();
78-
user.id = profile.getId();
79-
user.imageUrl = profile.getImageUrl();
80-
user.name = profile.getName();
81-
82-
user.authentication = {
83-
accessToken: authResponse.access_token,
84-
idToken: authResponse.id_token
85-
}
86-
86+
const user = this.getUserFrom(googleUser);
87+
user.serverAuthCode = serverAuthCode;
8788
resolve(user);
8889
} catch (error) {
8990
reject(error);
9091
}
9192
});
9293
}
9394

94-
async refresh(): Promise<any> {
95+
async refresh(): Promise<Authentication> {
9596
const authResponse = await gapi.auth2.getAuthInstance().currentUser.get().reloadAuthResponse()
9697
return {
9798
accessToken: authResponse.access_token,
@@ -102,6 +103,33 @@ export class GoogleAuthWeb extends WebPlugin implements GoogleAuthPlugin {
102103
async signOut(): Promise<any> {
103104
return gapi.auth2.getAuthInstance().signOut();
104105
}
106+
107+
private async addUserChangeListener() {
108+
await this.gapiLoaded;
109+
gapi.auth2.getAuthInstance().currentUser.listen(googleUser => {
110+
this.notifyListeners("userChange", googleUser.isSignedIn() ? this.getUserFrom(googleUser) : null);
111+
});
112+
}
113+
114+
private getUserFrom(googleUser: gapi.auth2.GoogleUser): User {
115+
const user = {} as User;
116+
const profile = googleUser.getBasicProfile();
117+
118+
user.email = profile.getEmail();
119+
user.familyName = profile.getFamilyName();
120+
user.givenName = profile.getGivenName();
121+
user.id = profile.getId();
122+
user.imageUrl = profile.getImageUrl();
123+
user.name = profile.getName();
124+
125+
const authResponse = googleUser.getAuthResponse(true);
126+
user.authentication = {
127+
accessToken: authResponse.access_token,
128+
idToken: authResponse.id_token
129+
}
130+
131+
return user;
132+
}
105133
}
106134

107135
const GoogleAuth = new GoogleAuthWeb();

0 commit comments

Comments
 (0)