Skip to content

Commit 2656943

Browse files
committed
extract to getGoogleIdTokenByChromeCustomTab() method
1 parent c642f81 commit 2656943

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

app/src/main/java/com/anotherdev/sample/firebase/auth/LoginActivity.java

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,12 @@
5050
import net.openid.appauth.AuthorizationServiceConfiguration;
5151
import net.openid.appauth.ClientSecretBasic;
5252
import net.openid.appauth.ResponseTypeValues;
53-
import net.openid.appauth.TokenResponse;
5453

5554
import butterknife.BindView;
5655
import hu.akarnokd.rxjava3.bridge.RxJavaBridge;
57-
import io.reactivex.Observable;
5856
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
5957
import io.reactivex.rxjava3.core.Completable;
58+
import io.reactivex.rxjava3.core.Observable;
6059
import io.reactivex.rxjava3.core.Single;
6160
import io.reactivex.rxjava3.functions.Consumer;
6261
import io.reactivex.rxjava3.internal.functions.Functions;
@@ -372,54 +371,59 @@ public void onSuccess(LoginResult loginResult) {
372371
});
373372
}
374373

374+
private Observable<String> getGoogleIdTokenByChromeCustomTab() {
375+
AuthorizationServiceConfiguration asc = new AuthorizationServiceConfiguration(
376+
Uri.parse("https://accounts.google.com/o/oauth2/v2/auth"),
377+
Uri.parse("https://www.googleapis.com/oauth2/v4/token")
378+
);
379+
380+
String clientId = getString(R.string.default_web_client_id);
381+
Uri redirectUri = new Uri.Builder()
382+
.scheme(getString(R.string.auth_google_scheme))
383+
.authority(getString(R.string.auth_google_host))
384+
.path(getString(R.string.auth_google_path))
385+
.build();
386+
AuthorizationRequest.Builder builder = new AuthorizationRequest.Builder(asc, clientId, ResponseTypeValues.CODE, redirectUri)
387+
.setScopes("profile email")
388+
.setPrompt(AuthorizationRequest.Prompt.LOGIN);
389+
390+
AuthorizationService service = getAppAuthService();
391+
return new RxInlineActivityResult(this)
392+
.request(service.getAuthorizationRequestIntent(builder.build()))
393+
.flatMap(result -> {
394+
Intent data = result.getData();
395+
Throwable cause = result.getCause();
396+
return data != null ? io.reactivex.Observable.just(data) : io.reactivex.Observable.error(cause != null ? cause : new UnknownError());
397+
})
398+
.map(AuthorizationResponse::fromIntent)
399+
.flatMap(authResponse -> io.reactivex.Observable.<String>create(emitter -> service.performTokenRequest(
400+
authResponse.createTokenExchangeRequest(),
401+
new ClientSecretBasic(BuildConfig.GOOGLE_CLIENT_SECRET + "wtf"),
402+
(tokenResponse, e) -> {
403+
if (tokenResponse != null) {
404+
emitter.onNext(tokenResponse.idToken);
405+
} else {
406+
emitter.onError(e);
407+
}
408+
})))
409+
.as(RxJavaBridge.toV3Observable());
410+
}
411+
375412
private void setupSignInWithGoogleButton(FirebaseAuth firebaseAuth) {
376413
setupButton(firebaseAuth,
377414
signInWithGoogleButton,
378415
v -> {
379416
if (true) {
380-
AuthorizationServiceConfiguration asc = new AuthorizationServiceConfiguration(
381-
Uri.parse("https://accounts.google.com/o/oauth2/v2/auth"),
382-
Uri.parse("https://www.googleapis.com/oauth2/v4/token")
383-
);
384-
385-
String clientId = getString(R.string.default_web_client_id);
386-
Uri redirectUri = new Uri.Builder()
387-
.scheme(getString(R.string.auth_google_scheme))
388-
.authority(getString(R.string.auth_google_host))
389-
.path(getString(R.string.auth_google_path))
390-
.build();
391-
AuthorizationRequest.Builder builder = new AuthorizationRequest.Builder(asc, clientId, ResponseTypeValues.CODE, redirectUri)
392-
.setScopes("profile email")
393-
.setPrompt(AuthorizationRequest.Prompt.LOGIN);
394-
395-
AuthorizationService service = getAppAuthService();
396-
Intent authIntent = service.getAuthorizationRequestIntent(builder.build());
397-
398-
onDestroy.add(RxJavaBridge.toV3Disposable(new RxInlineActivityResult(this)
399-
.request(authIntent)
400-
.flatMap(result -> {
401-
Intent data = result.getData();
402-
Throwable cause = result.getCause();
403-
return data != null ? Observable.just(data) : Observable.error(cause != null ? cause : new UnknownError());
404-
})
405-
.map(AuthorizationResponse::fromIntent)
406-
.flatMap(authResponse -> Observable.<TokenResponse>create(emitter -> service.performTokenRequest(
407-
authResponse.createTokenExchangeRequest(),
408-
new ClientSecretBasic(BuildConfig.GOOGLE_CLIENT_SECRET),
409-
(tokenResponse, e) -> {
410-
if (tokenResponse != null) {
411-
emitter.onNext(tokenResponse);
412-
} else {
413-
emitter.onError(e);
414-
}
415-
})))
417+
onDestroy.add(getGoogleIdTokenByChromeCustomTab()
416418
.doOnNext(tokenResponse -> Log.d("http", "tokenResponse: " + FarGson.get().toJson(tokenResponse)))
417-
.flatMapSingle(tokenResponse -> RxJavaBridge.toV2Single(
418-
firebaseAuth.signInWithCredential(
419-
GoogleAuthProvider.getCredential(tokenResponse.idToken))))
419+
.flatMapSingle(idToken -> firebaseAuth.signInWithCredential(GoogleAuthProvider.getCredential(idToken)))
420420
.doOnNext(signInResponse -> Log.d("http", "signInResponse: " + FarGson.get().toJson(signInResponse)))
421421
.doOnError(e -> Log.e("http", "error: " + e, e))
422-
.subscribe(io.reactivex.internal.functions.Functions.emptyConsumer(), RxUtil.ON_ERROR_LOG_V2)));
422+
.doOnError(e -> {
423+
dialog(e);
424+
googleSignInClient.signOut();
425+
})
426+
.subscribe(Functions.emptyConsumer(), RxUtil.ON_ERROR_LOG_V3));
423427
return;
424428
}
425429

0 commit comments

Comments
 (0)