Last active
March 16, 2022 19:22
-
-
Save exhesham/952bd3f5b6e2df01cbd2ae6f80c74cac to your computer and use it in GitHub Desktop.
Google sign in process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implementation 'com.google.android.gms:play-services-auth:19.0.0' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
... | |
classpath 'com.google.gms:google-services:4.3.4' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private GoogleSignInClient mGoogleSignInClient; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | |
.requestIdToken(getString(R.string.server_client_id)) | |
.requestEmail() | |
.build(); | |
mGoogleSignInClient = getClient(this, gso); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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