Skip to content

Commit

Permalink
Android refresh method (#314)
Browse files Browse the repository at this point in the history
* add android refresh function to return accessToken and idToken

* update android refresh token to return accessToken and idToken

* add documentation for Android refresh method

* format android refresh documentation

* delete dead space from android refresh documentation

* add space in android refresh documentation
  • Loading branch information
CaolanCode authored Oct 20, 2023
1 parent b04f0ae commit e5975f3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ plugin first use `androidClientId` if not found use `clientId` if not found use
</resources>
```

**Refresh method**

This method should be called when the app is initialized to establish if the user is currently logged in. If true, the method will return an accessToken, idToken and an empty refreshToken.
```ts
checkLoggedIn() {
GoogleAuth.refresh()
.then((data) => {
if (data.accessToken) {
this.currentTokens = data;
}
})
.catch((error) => {
if (error.type === 'userLoggedOut') {
this.signin()
}
});
}
```

## Configure

| Name | Type | Description |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,23 @@ protected void signInResult(PluginCall call, ActivityResult result) {

@PluginMethod()
public void refresh(final PluginCall call) {
call.reject("I don't know how to refresh token on Android");
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(getContext());
if (account == null) {
call.reject("User not logged in.");
} else {
try {
JSONObject accessTokenObject = getAuthToken(account.getAccount(), true);

JSObject authentication = new JSObject();
authentication.put("idToken", account.getIdToken());
authentication.put(FIELD_ACCESS_TOKEN, accessTokenObject.get(FIELD_ACCESS_TOKEN));
authentication.put("refreshToken", "");
call.resolve(authentication);
} catch(Exception e){
e.printStackTrace();
call.reject("Something went wrong while retrieving access token", e);
}
}
}

@PluginMethod()
Expand Down

0 comments on commit e5975f3

Please sign in to comment.