11import { WebPlugin } from '@capacitor/core' ;
22import { GoogleAuthPlugin } from './definitions' ;
3+ import { User , Authentication } from './user' ;
4+
35// @ts -ignore
46import config from '../../../../../capacitor.config.json' ;
57
68export 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
107135const GoogleAuth = new GoogleAuthWeb ( ) ;
0 commit comments