Google Firebase Authentication을 이용해 로그인&회원가입 구현
초기 로그인 액티비티에서 회원가입 버튼을 눌러 회원가입 액티비티를 띄우고, 회원가입 화면에서 이메일과 비밀번호를 입력해 회원가입을 완료한 뒤, 다시 로그인 화면으로 돌아와 가입한 정보로 로그인에 성공하게되면 홈 액티비티로 이동하도록 구현했습니다.
[activity_signup.xml]
x
<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="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
tools:context=".SignUpActivity">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="40dp"
android:layout_marginTop="50dp"
app:srcCompat="@drawable/logo_amadium" />
<TextView
android:id="@+id/textView_signUpTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="회원가입"
android:textColor="@android:color/black"
android:textSize="25sp" />
<View
android:id="@+id/view_line"
android:layout_width="250dp"
android:layout_height="1.2dp"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:background="@android:color/black" />
<EditText
android:id="@+id/editText_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:hint="E-mail"
android:inputType="textEmailAddress"
android:textColor="@android:color/black" />
<EditText
android:id="@+id/editText_passWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textColor="@android:color/black" />
<EditText
android:id="@+id/editText_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:textColor="@android:color/black" />
<Button
android:id="@+id/btn_join"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/white"
android:text="Join"
android:textColor="@android:color/black" />
</LinearLayout>
[SignUpActivity.java] (회원가입 화면)
xxxxxxxxxx
package com.pubak.econovation.amadium;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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;
public class SignUpActivity extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
private EditText editTextEmail;
private EditText editTextPassword;
private EditText editTextName;
private Button buttonJoin;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
firebaseAuth = FirebaseAuth.getInstance();
editTextEmail = (EditText) findViewById(R.id.editText_email);
editTextPassword = (EditText) findViewById(R.id.editText_passWord);
editTextName = (EditText) findViewById(R.id.editText_name);
buttonJoin = (Button) findViewById(R.id.btn_join);
buttonJoin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!editTextEmail.getText().toString().equals("") && !editTextPassword.getText().toString().equals("")) {
// 이메일과 비밀번호가 공백이 아닌 경우
createUser(editTextEmail.getText().toString(), editTextPassword.getText().toString(), editTextName.getText().toString());
} else {
// 이메일과 비밀번호가 공백인 경우
Toast.makeText(SignUpActivity.this, "계정과 비밀번호를 입력하세요.", Toast.LENGTH_LONG).show();
}
}
});
}
private void createUser(String email, String password, String name) {
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
public void onComplete( Task<AuthResult> task) {
if (task.isSuccessful()) {
// 회원가입 성공시
Toast.makeText(SignUpActivity.this, "회원가입 성공", Toast.LENGTH_SHORT).show();
finish();
} else {
// 계정이 중복된 경우
Toast.makeText(SignUpActivity.this, "이미 존재하는 계정입니다.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
[activity_login.xml]
xxxxxxxxxx
<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:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LogInActivity">
<ImageView
android:id="@+id/imageView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginBottom="80dp"
app:srcCompat="@drawable/logo_amadium" />
<EditText
android:id="@+id/edittext_email"
android:layout_width="wrap_content"
android:layout_height="46dp"
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="E-mail"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/edittext_password"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#ffffff"
android:text="로그인" />
<Button
android:id="@+id/btn_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#ffffff"
android:text="회원가입" />
</LinearLayout>
[LogInActivity.java] (로그인 화면)
xxxxxxxxxx
package com.pubak.econovation.amadium;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 LogInActivity extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener firebaseAuthListener;
private EditText editTextEmail;
private EditText editTextPassword;
private Button buttonLogIn;
private Button buttonSignUp;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
firebaseAuth = FirebaseAuth.getInstance();
editTextEmail = (EditText) findViewById(R.id.edittext_email);
editTextPassword = (EditText) findViewById(R.id.edittext_password);
buttonSignUp = (Button) findViewById(R.id.btn_signup);
buttonSignUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// SignUpActivity 연결
Intent intent = new Intent(LogInActivity.this, SignUpActivity.class);
startActivity(intent);
}
});
buttonLogIn = (Button) findViewById(R.id.btn_login);
buttonLogIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!editTextEmail.getText().toString().equals("") && !editTextPassword.getText().toString().equals("")) {
loginUser(editTextEmail.getText().toString(), editTextPassword.getText().toString());
} else {
Toast.makeText(LogInActivity.this, "계정과 비밀번호를 입력하세요.", Toast.LENGTH_LONG).show();
}
}
});
firebaseAuthListener = new FirebaseAuth.AuthStateListener() {
public void onAuthStateChanged( FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
Intent intent = new Intent(LogInActivity.this, HomeActivity.class);
startActivity(intent);
finish();
} else {
}
}
};
}
public void loginUser(String email, String password) {
firebaseAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
public void onComplete( Task<AuthResult> task) {
if (task.isSuccessful()) {
// 로그인 성공
Toast.makeText(LogInActivity.this, "로그인 성공", Toast.LENGTH_SHORT).show();
firebaseAuth.addAuthStateListener(firebaseAuthListener);
} else {
// 로그인 실패
Toast.makeText(LogInActivity.this, "아이디 또는 비밀번호가 일치하지 않습니다.", Toast.LENGTH_SHORT).show();
}
}
});
}
protected void onStart() {
super.onStart();
}
protected void onStop() {
super.onStop();
if (firebaseAuthListener != null) {
firebaseAuth.removeAuthStateListener(firebaseAuthListener);
}
}
}
[구현 결과]
Firebase Authentication에 회원가입한 이메일 정보가 추가되는 것을 확인할 수 있었습니다.
반응형