Skip to content

Commit ef89195

Browse files
committed
fix(google-signin): getTokens
1 parent 6733652 commit ef89195

2 files changed

Lines changed: 53 additions & 44 deletions

File tree

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
{
2-
"name": "@nativescript/google-signin",
3-
"version": "1.0.0-alpha.7",
4-
"description": "Google Sign-in for your NativeScript applications",
5-
"main": "index",
6-
"typings": "index.d.ts",
7-
"nativescript": {
8-
"platforms": {
9-
"ios": "6.0.0",
10-
"android": "6.0.0"
11-
}
12-
},
13-
"repository": {
14-
"type": "git",
15-
"url": "https://github.com/NativeScript/plugins.git"
16-
},
17-
"keywords": [
18-
"NativeScript",
19-
"JavaScript",
20-
"TypeScript",
21-
"iOS",
22-
"Android"
23-
],
24-
"author": {
25-
"name": "NativeScript",
26-
"email": "oss@nativescript.org"
27-
},
28-
"bugs": {
29-
"url": "https://github.com/NativeScript/plugins/issues"
30-
},
31-
"license": "Apache-2.0",
32-
"homepage": "https://github.com/NativeScript/plugins",
33-
"readmeFilename": "README.md",
34-
"bootstrapper": "@nativescript/plugin-seed"
2+
"name": "@nativescript/google-signin",
3+
"version": "1.0.0-alpha.8",
4+
"description": "Google Sign-in for your NativeScript applications",
5+
"main": "index",
6+
"typings": "index.d.ts",
7+
"nativescript": {
8+
"platforms": {
9+
"ios": "6.0.0",
10+
"android": "6.0.0"
11+
}
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/NativeScript/plugins.git"
16+
},
17+
"keywords": [
18+
"NativeScript",
19+
"JavaScript",
20+
"TypeScript",
21+
"iOS",
22+
"Android"
23+
],
24+
"author": {
25+
"name": "NativeScript",
26+
"email": "oss@nativescript.org"
27+
},
28+
"bugs": {
29+
"url": "https://github.com/NativeScript/plugins/issues"
30+
},
31+
"license": "Apache-2.0",
32+
"homepage": "https://github.com/NativeScript/plugins",
33+
"readmeFilename": "README.md",
34+
"bootstrapper": "@nativescript/plugin-seed"
3535
}

packages/google-signin/src-native/android/googlesignin/src/main/java/org/nativescript/plugins/googlesignin/GoogleSignIn.kt

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class GoogleSignIn {
111111
}
112112

113113
parsedOptions.optJSONArray("scopes")?.let {
114-
for(i in 0 until it.length()) {
114+
for (i in 0 until it.length()) {
115115
builder.requestScopes(Scope(it.getString(i)))
116116
}
117117
}
@@ -267,6 +267,8 @@ class GoogleSignIn {
267267
return getAccessToken(context, GoogleSignIn.getLastSignedInAccount(context))
268268
}
269269

270+
private const val GET_TOKENS_ERROR = "getTokens requires a user to be signed in";
271+
270272
private fun getAccessToken(context: Context, account: GoogleSignInAccount?): String? {
271273
if (account?.account != null) {
272274
return GoogleAuthUtil.getToken(
@@ -275,7 +277,7 @@ class GoogleSignIn {
275277
scopesToString(account.grantedScopes)
276278
)
277279
} else {
278-
throw Exception("getTokens requires a user to be signed in")
280+
throw Exception(GET_TOKENS_ERROR)
279281
}
280282
}
281283

@@ -284,23 +286,30 @@ class GoogleSignIn {
284286
executors.submit {
285287
try {
286288
val account = GoogleSignIn.getLastSignedInAccount(context)
287-
?: throw Exception("getTokens requires a user to be signed in")
288-
val token = getAccessToken(context)
289-
val tokens = JSONObject()
290-
tokens.put("idToken", account.idToken)
291-
tokens.put("accessToken", token)
292-
runOnMain {
293-
callback.onSuccess(tokens.toString())
289+
if (account != null) {
290+
val token = getAccessToken(context)
291+
val tokens = JSONObject()
292+
tokens.put("idToken", account.idToken)
293+
tokens.put("accessToken", token)
294+
295+
if (retrieveAccessToken) {
296+
accessToken = token
297+
}
298+
299+
runOnMain {
300+
callback.onSuccess(tokens.toString())
301+
}
302+
} else {
303+
callback.onError(Exception(GET_TOKENS_ERROR))
294304
}
295305
} catch (e: Exception) {
296306
runOnMain {
297-
callback.onError(Exception("getTokens requires a user to be signed in"))
307+
callback.onError(Exception(GET_TOKENS_ERROR))
298308
}
299309
}
300310
}
301311
}
302312

303-
304313
@JvmStatic
305314
fun getSignedInAccountFromIntent(intent: Intent, callback: Callback<GoogleUser>) {
306315
GoogleSignIn.getSignedInAccountFromIntent(intent)

0 commit comments

Comments
 (0)