Skip to content

Commit 84c85cb

Browse files
committed
Web: error handling for signIn #11
1 parent 24a6e1a commit 84c85cb

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

src/web.ts

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,50 @@ export class GoogleAuthWeb extends WebPlugin implements GoogleAuthPlugin {
3939
}
4040

4141
async signIn(): Promise<any> {
42-
return new Promise(async (resolve) => {
43-
const user: any = {};
44-
const needsOfflineAccess = config.plugins.GoogleAuth.serverClientId != null;
45-
46-
if (needsOfflineAccess) {
47-
const offlineAccessResponse = await gapi.auth2.getAuthInstance().grantOfflineAccess();
48-
user.serverAuthCode = offlineAccessResponse.code;
49-
} else {
50-
await gapi.auth2.getAuthInstance().signIn();
42+
return new Promise(async (resolve, reject) => {
43+
try {
44+
const user: any = {};
45+
46+
var needsOfflineAccess = false;
47+
try {
48+
needsOfflineAccess = config.plugins.GoogleAuth.serverClientId != null;
49+
} catch {
50+
51+
}
52+
53+
if (needsOfflineAccess) {
54+
const offlineAccessResponse = await gapi.auth2.getAuthInstance().grantOfflineAccess();
55+
user.serverAuthCode = offlineAccessResponse.code;
56+
} else {
57+
await gapi.auth2.getAuthInstance().signIn();
58+
}
59+
60+
const googleUser = gapi.auth2.getAuthInstance().currentUser.get();
61+
62+
if (needsOfflineAccess) {
63+
// HACK: AuthResponse is null if we don't do this when using grantOfflineAccess
64+
await googleUser.reloadAuthResponse();
65+
}
66+
67+
const authResponse = googleUser.getAuthResponse(true);
68+
69+
const profile = googleUser.getBasicProfile();
70+
user.email = profile.getEmail();
71+
user.familyName = profile.getFamilyName();
72+
user.givenName = profile.getGivenName();
73+
user.id = profile.getId();
74+
user.imageUrl = profile.getImageUrl();
75+
user.name = profile.getName();
76+
77+
user.authentication = {
78+
accessToken: authResponse.access_token,
79+
idToken: authResponse.id_token
80+
}
81+
82+
resolve(user);
83+
} catch (error) {
84+
reject(error);
5185
}
52-
53-
const googleUser = gapi.auth2.getAuthInstance().currentUser.get();
54-
55-
if (needsOfflineAccess) {
56-
// HACK: AuthResponse is null if we don't do this when using grantOfflineAccess
57-
await googleUser.reloadAuthResponse();
58-
}
59-
60-
const authResponse = googleUser.getAuthResponse(true);
61-
62-
const profile = googleUser.getBasicProfile();
63-
user.email = profile.getEmail();
64-
user.familyName = profile.getFamilyName();
65-
user.givenName = profile.getGivenName();
66-
user.id = profile.getId();
67-
user.imageUrl = profile.getImageUrl();
68-
user.name = profile.getName();
69-
70-
user.authentication = {
71-
accessToken: authResponse.access_token,
72-
idToken: authResponse.id_token
73-
}
74-
75-
resolve(user);
7686
});
7787
}
7888

0 commit comments

Comments
 (0)