본문 바로가기

Android

Firebase를 이용한 이메일 로그인

Firebase 사용하기

Tools>

Firebase>

Authentication 클릭>

Email and password authentication>

Launch in browser>

Connect your app to Firebase>Add Firebase Authtication t.....

.

.

.

MainActivity.java

package com.example.ex11;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
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 MainActivity extends AppCompatActivity {
    EditText edtemail, edtpass;
    FirebaseAuth mAuth;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAuth=FirebaseAuth.getInstance();

        edtemail = findViewById(R.id.edtemail);
        edtpass = findViewById(R.id.edtpass);
    }

    public void mClick(View v) {
        String stremail=edtemail.getText().toString();
        String strpass=edtpass.getText().toString();
        switch (v.getId()) {
            case R.id.btnregister:
                mAuth.createUserWithEmailAndPassword(stremail,strpass)
                        .addOnCompleteListener(this,
                        new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if(task.isSuccessful()){
                                    FirebaseUser user=mAuth.getCurrentUser();
                                    Toast.makeText(MainActivity.this,"가입완료",Toast.LENGTH_SHORT).show();
                                    edtemail.setText("");
                                    edtpass.setText("");
                                }else{
                                    Toast.makeText(MainActivity.this,"가입실패",Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                break;
            case R.id.btnlogin:
                mAuth.signInWithEmailAndPassword(stremail, strpass)
                        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if(task.isSuccessful()){
                                    Toast.makeText(MainActivity.this,"로그인 성공",Toast.LENGTH_SHORT).show();
                                }else{
                                    AlertDialog.Builder box=new AlertDialog.Builder(MainActivity.this);
                                    box.setMessage("아이디/비밀번호가 틀립니다.");
                                    box.setNegativeButton("확인",null);
                                    box.show();
                                }
                            }
                        });
                break;
            case R.id.btncancel:
                break;
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:layout_gravity="center_vertical"
    android:padding="30sp">
    <EditText
        android:id="@+id/edtemail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email"
        android:inputType="text"/>
    <EditText
        android:id="@+id/edtpass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:inputType="textPassword"/>
    <Button
        android:background="#ffffff"
        android:onClick="mClick"
        android:id="@+id/btnregister"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Register"/>
    <Button
        android:background="#ffffff"
        android:onClick="mClick"
        android:id="@+id/btnlogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"/>
    <Button
        android:background="#ffffff"
        android:onClick="mClick"
        android:id="@+id/btncancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Cancel"/>
</LinearLayout>