Page 381 - MDP2022-2
P. 381
Register Activity -
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
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;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.regex.Pattern;
public class RegisteActivity extends AppCompatActivity {
private FirebaseAuth mFirebaseAuth;
private DatabaseReference mDatabaseRef;
private EditText mEtEmail, mEtPwd, mEtConfirm;
private Button mBtnRegister;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registe);
//타이틀바 없애기
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
mFirebaseAuth = FirebaseAuth.getInstance();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("prototype");
mEtEmail = findViewById(R.id.email);
mEtPwd = findViewById(R.id.passwd);
mEtConfirm = findViewById(R.id.passwdConfirm);
mBtnRegister = findViewById(R.id.confirm);
mBtnRegister.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String strEmail = mEtEmail.getText().toString();
String strPwd = mEtPwd.getText().toString();
String strConfirm = mEtConfirm.getText().toString();
if (strEmail.equals("") || strPwd.equals("") || strConfirm.equals("")) {
Toast.makeText(RegisteActivity.this, "항목을 전부 작성하지 않았습니다.", Toast.LENGTH_SHORT).show();
}else if(strPwd.length() < 8){
Toast.makeText(RegisteActivity.this, "비밀번호는8자리 이상이 필요합니다", Toast.LENGTH_SHORT).show();
} else if(!Pattern.matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9.-]+$",strEmail)){
Toast.makeText(RegisteActivity.this, "이메일 형식이 아닙니다.", Toast.LENGTH_SHORT).show();
} else {
mFirebaseAuth.createUserWithEmailAndPassword(strEmail, strPwd).addOnCompleteListener(RegisteActivity.this,
new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser firebaseUser = mFirebaseAuth.getCurrentUser();
UserAccount account = new UserAccount();
account.setIdToken(firebaseUser.getUid());
account.setEmailId(firebaseUser.getEmail());
account.setPassword(strPwd);
mDatabaseRef.child("UserAccount").child(firebaseUser.getUid()).setValue(account);
if (strPwd.equals(strConfirm)) {
Toast.makeText(RegisteActivity.this, "회원가입에 성공했습니다.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisteActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
} else {