안드로이드 파이어베이스 로그인&회원가입(이메일,비밀번호)

4599 단어 androidandroid

build.gradle의 dependencies에 firebase 라이브러리 종속 항목 선언해주기

implementation platform('com.google.firebase:firebase-bom:29.3.0')
implementation 'com.google.firebase:firebase-auth'

이메일, 비밀번호를 입력받아 회원가입하기

public void createUser(String email, String password) {
        // Exception to allow all Edittexts to be entered
        String name = sign_name.getText().toString();
        String birth = String.format("%d-%d-%d", dayPicker.getYear(),dayPicker.getMonth()+1, dayPicker.getDayOfMonth());
        if (name.equals("")) { Toast.makeText(JoinMemberActivity.this, "이름을 입력해 주세요.", Toast.LENGTH_SHORT).show(); return; }
        if (email.equals("")) { Toast.makeText(JoinMemberActivity.this, "이메일을 입력해 주세요.", Toast.LENGTH_SHORT).show(); return; }
        if (password.equals("")) { Toast.makeText(JoinMemberActivity.this, "비밀번호를 입력해 주세요.", Toast.LENGTH_SHORT).show(); return; }
        if (is_checked == false) { Toast.makeText(JoinMemberActivity.this, "비밀번호 확인을 실행해주세요", Toast.LENGTH_SHORT).show(); return; }
        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            final String uid = task.getResult().getUser().getUid();     // UID(Unified ID) 생성
                            UserModel userModel = new UserModel(name, birth, uid);
                            mDatabase.child("users").child(uid).setValue(userModel);    // 데이터베이스에 (UID,이름,생년월일) 저장
                            mDatabase.child("findData").child(name+birth).setValue(email+"!"+password);
                            Toast.makeText(JoinMemberActivity.this, "회원가입을 완료하였습니다.", Toast.LENGTH_SHORT).show();
                            finish();
                            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(JoinMemberActivity.this, "회원가입에 실패하였습니다.", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

아래는 회원가입 성공시 Firebase Authentication에 추가된 모습이다.

현재 사용자가 로그인 되어있는지 확인하기

public void onStart() {
    super.onStart();
    FirebaseUser currentUser = mAuth.getCurrentUser();
    if(currentUser != null){
        reload();
    }
}

이메일, 비밀번호를 입력받아 로그인하기

public void loginUser(String email, String password) {
        if (email.equals("")) { Toast.makeText(MainActivity.this, "이메일을 입력해 주세요.", Toast.LENGTH_SHORT).show(); return; }
        if (password.equals("")) { Toast.makeText(MainActivity.this, "비밀번호를 입력해 주세요.", Toast.LENGTH_SHORT).show(); return; }

        mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {![](https://velog.velcdn.com/images/eric2057/post/bf2985ee-ecd2-49c3-95c7-74ae5b833110/image.PNG)

                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(MainActivity.this, "로그인 되었습니다.", Toast.LENGTH_SHORT).show();
                            finish();
                            Intent intent = new Intent(getApplicationContext(), MenuActivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(MainActivity.this, "가입되지 않은 계정입니다.", Toast.LENGTH_SHORT).show();
                        }
                    }
                })

좋은 웹페이지 즐겨찾기