Android ile Firebase Authentication Kullanımı

Android ile Firebase Authentication Kullanımı

Android ile Firebase Authentication Kullanımı

Firebase ile Android Projesine Mail Adresi ile Kayıt ve Giriş sistemini anlattım.

Android ile Firebase Authentication Kullanımı

LoginRegisterActivity.java

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class LoginRegisterActivity extends AppCompatActivity {

    EditText etMail,etPassword;
    Button btnGiris,btnKayit;
    private FirebaseAuth mAuth;
    String name,mail,uid;
    Uri photoUrl;
    boolean emailVerified;

    public void kayitOl(String email, String password) {
        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
                        if (task.isSuccessful()) {
                            FirebaseUser user = mAuth.getCurrentUser();

                            if (user != null) {
                                name = user.getDisplayName();
                                mail = user.getEmail();
                                photoUrl = user.getPhotoUrl();
                                emailVerified = user.isEmailVerified();
                                uid = user.getUid();

                                Intent intent = new Intent(getApplicationContext(),LoggedActivity.class);
                                intent.putExtra("name",name);
                                intent.putExtra("mail",mail);
                                intent.putExtra("photoUrl",photoUrl);
                                intent.putExtra("emailVerified",emailVerified);
                                intent.putExtra("uid",uid);
                                startActivity(intent);
                            }

                        } else {
                            Toast.makeText(LoginRegisterActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();

                        }


                    }
                });
    }

    public void girisYap(String email,String password){
        mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
                        if (task.isSuccessful()) {
                            Log.d("", "signInWithEmail:success");
                            FirebaseUser user = mAuth.getCurrentUser();

                            if (user != null) {
                                name = user.getDisplayName();
                                mail = user.getEmail();
                                photoUrl = user.getPhotoUrl();
                                emailVerified = user.isEmailVerified();
                                uid = user.getUid();

                                Intent intent = new Intent(getApplicationContext(),LoggedActivity.class);
                                intent.putExtra("name",name);
                                intent.putExtra("mail",mail);
                                intent.putExtra("photoUrl",photoUrl);
                                intent.putExtra("emailVerified",emailVerified);
                                intent.putExtra("uid",uid);
                                startActivity(intent);

                            }
                        } else {
                            Toast.makeText(LoginRegisterActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAuth = FirebaseAuth.getInstance();

        btnGiris = findViewById(R.id.btnGiris);
        btnKayit = findViewById(R.id.btnKayit);
        etMail = findViewById(R.id.etMailAdresi);
        etPassword = findViewById(R.id.etMailSifresi);

        btnGiris.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                girisYap(etMail.getText().toString(),etPassword.getText().toString());
            }
        });

        btnKayit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                kayitOl(etMail.getText().toString(),etPassword.getText().toString());
            }
        });


    }

    @Override
    public void onStart() {
        super.onStart();
        FirebaseUser currentUser = mAuth.getCurrentUser();
    }
}

activity_loginregister.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginRegisterActivity">

    <EditText
        android:id="@+id/etMailAdresi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="62dp"
        android:ems="10"
        android:hint="Mail Adresi"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/etMailSifresi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="125dp"
        android:ems="10"
        android:hint="Şifreniz"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btnGiris"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/etMailAdresi"
        android:layout_alignParentTop="true"
        android:layout_marginTop="185dp"
        android:text="Giriş yap" />

    <Button
        android:id="@+id/btnKayit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/btnGiris"
        android:layout_alignEnd="@+id/etMailAdresi"
        android:text="Kayıt" />
</RelativeLayout>

Bu sayfa haricinde giriş veya kayıt işlemi başarılı olduğu takdirde, LoggedActivity sınıfına gitmek isteyecektir. İlgili Activity sayfasını da üretiniz.

Firebase Authentication aynı zamanda Firebase Database, Firebase Storage sistemlerinde de izin-yetkilendirme kurallarında da kullanılmaktadır.


İlgili kelimeler;
firebase auth authorization authentication
İlgili kategori;
Android Studio Dersleri
Makaleyi paylaşın; Google+FacebookTwitterTumblrPinterestLinkedIn

Benzer makaleler mi eklemeliyim ? Bana LinkedIn ve Github adreslerimden üzerinden ulaşabilirsiniz.