Merhaba arkadaşlar bu gün ki makalemde firebase’in bize sunmuş olduğu uygulamaya gmail ile login olmayı anlatacağım.
Bir önceki dersimizde email ve password ile login olmayı görmüştük. Buradan okuyabilirsiniz.
//singin butonuna tıklandıgında
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
}}
);
//firebase
// Write a message to the database
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() != null) {
startActivity(new Intent(MainActivity.this, Icim_Rahat.class));}}
};
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */,
new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(MainActivity.this, "hattaa!!!!!", Toast.LENGTH_SHORT).show();
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
// Configure Google Sign In
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
}
else {
Toast.makeText(this, "hataa", Toast.LENGTH_SHORT).show();
}}}
private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
FirebaseUser user = mAuth.getCurrentUser();
//updateUI(user);
}
else {
// If sign in fails, display a message to the user.
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
//updateUI(null);
}
// ...
}
});
}Yukarıda sign in butonuna tıklanma olayından sonraki kısmın kodları bulunmaktadır. Sing in butonuna tıklandığında signIn() methodu çağrılır ilk olarak. Bu method GoogleSignInApi’sine bağlanarak kullanıcının gmail account’ u ile kayıt olmasını sağlar. Bu kodu kendi uygulamanıza göre uyarlayarak doğrudan kullanabilirsiniz. Kod da bulunan yorum satırları ile hangi kısmın ne amaçla yazıldığını takip edebilirsiniz.
Bir makalenin daha sonuna geldik, esenle kalın..
IT Tutorial IT Tutorial | Oracle DBA | SQL Server, Goldengate, Exadata, Big Data, Data ScienceTutorial