App/Android
안드로이드 // firebase - email 가입 및 로그인
Jaybon
2020. 8. 12. 19:40
처음오셨다면 아래링크를 먼저 확인하세요
https://ondolroom.tistory.com/741
아래 사이트를 먼저 확인
https://firebase.google.com/docs/auth/android/start?authuser=0
--
AndroidManifest.xml
인터넷 권한 획득을 위해 아래코드 추가
<uses-permission android:name="android.permission.INTERNET"/>
--
프로젝트 단위 build.gradle
'com.google.gms:google-services:4.3.3' 코드를 추가(3.0.0도 가능)
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath 'com.google.gms:google-services:4.3.3' // 추가
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
--
앱 단위 build.gradle
맨 바깥에 아래코드 추가
// firebase
apply plugin: 'com.google.gms.google-services'
dependencies 안에 아래코드 추가
//firebase create account
implementation 'com.google.firebase:firebase-auth:19.3.2'
--
activity_main.xml
아래와 같이 뷰를 짜준다
--
MainActivity
더보기
package com.jaybon.firebasecreateaccounttest;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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 {
private static final String TAG = "Main_Activity";
private MainActivity mContext = MainActivity.this;
private FirebaseAuth mAuth;
private TextView tv;
private TextView tv1;
private EditText email1;
private EditText password1;
private Button sign;
private Button login;
private Button logout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
tv = findViewById(R.id.tv);
tv1 = findViewById(R.id.tv1);
email1 = findViewById(R.id.email);
password1 = findViewById(R.id.password);
sign = findViewById(R.id.sign);
sign.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createAccount(email1.getText().toString(), password1.getText().toString());
}
});
login = findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn(email1.getText().toString(), password1.getText().toString());
}
});
logout = findViewById(R.id.logout);
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logout();
}
});
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
}
private void createAccount(String email, String password) {
Log.d(TAG, "createAccount:" + email);
if (!validateForm()) {
return;
}
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "onComplete: 들어옴");
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
tv.setText(user.getEmail());
tv1.setText("회원가입됨");
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(mContext, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
private void signIn(String email, String password) {
Log.d(TAG, "signIn:" + email);
if (!validateForm()) {
return;
}
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "onComplete: 들어옴");
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
tv.setText(user.getEmail());
tv1.setText("로그인됨");
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(mContext, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
private void logout() {
mAuth.signOut();
Log.d(TAG, "signOut: 사인아웃!!!!!!!!!!!!!"+mAuth.getCurrentUser());
}
private boolean validateForm() {
boolean valid = true;
String email = email1.getText().toString();
if (TextUtils.isEmpty(email)) {
email1.setError("Required.");
valid = false;
} else {
email1.setError(null);
}
String password = password1.getText().toString();
if (TextUtils.isEmpty(password)) {
password1.setError("Required.");
valid = false;
} else {
password1.setError(null);
}
return valid;
}
}
--
입력한 이메일과 비번이 공백인지 확인
--
회원가입을 진행하는 함수
--
로그인을 진행하는 함수
--
로그아웃을 하는 로직
--
회원가입 테스트