Skip to content

Instantly share code, notes, and snippets.

@exhesham
Last active March 16, 2022 19:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exhesham/952bd3f5b6e2df01cbd2ae6f80c74cac to your computer and use it in GitHub Desktop.
Save exhesham/952bd3f5b6e2df01cbd2ae6f80c74cac to your computer and use it in GitHub Desktop.
Google sign in process
<application>
....
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
implementation 'com.google.android.gms:play-services-auth:19.0.0'
dependencies {
...
classpath 'com.google.gms:google-services:4.3.4'
}
private GoogleSignInClient mGoogleSignInClient;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.server_client_id))
.requestEmail()
.build();
mGoogleSignInClient = getClient(this, gso);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
updateUI(account);
} catch (ApiException e) {
e.printStackTrace();
updateUI(null);
}
}
@Override
public void onStart() {
super.onStart();
GoogleSignInAccount account = getLastSignedInAccount(this);
updateUI(account);
}
private void updateUI(@Nullable GoogleSignInAccount account) {
if (account != null) {
String idToken = account.getIdToken();
// call function with the token
callGcpFunction(idToken);
} else {
signIn();
}
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment