Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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