Firebase에 들어가서 이메일 사용 설정은 이미 했다고 가정.
activity_sign_up.xml 코드
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center"
android:text="회원가입"
android:textSize="24sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<EditText
android:id="@+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:ems="10"
android:hint="이메일"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:ems="10"
android:hint="비밀번호"
android:inputType="textPassword" />
<EditText
android:id="@+id/passwordCheckEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:ems="10"
android:hint="비밀번호 확인"
android:inputType="textPassword" />
<Button
android:id="@+id/signUpButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="회원가입" />
</LinearLayout>
</LinearLayout>
SignUpActivity.java 코드
package com.onestation.bookshare;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
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 SignUpActivity extends AppCompatActivity {
private static final String TAG = "SignUpActivity";
private FirebaseAuth mAuth;
private Button sign_up;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
mAuth = FirebaseAuth.getInstance();
sign_up = findViewById(R.id.signUpButton);
sign_up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signUp();
}
});
}
// When initializing your Activity, check to see if the user is currently signed in.
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
}
private void signUp() {
// 이메일
EditText emailEditText = findViewById(R.id.emailEditText);
String email = emailEditText.getText().toString();
// 비밀번호
EditText passwordEditText = findViewById(R.id.passwordEditText);
String password = passwordEditText.getText().toString();
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
startActivity(intent);
Toast.makeText(SignUpActivity.this, "등록 완료", Toast.LENGTH_SHORT).show();
finish();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(SignUpActivity.this, "등록 에러", Toast.LENGTH_SHORT).show();
return;
}
}
});
}
Intent intent = getIntent();
}
이 코드는 회원가입시 이메일 사용자 인증같은건 안 됨.
helloworld@naveeeerrrr.comcomcom 이런식으로 이상한 메일로 해도 가입 됨.
그래도 Firebase 관리로 아이디와 비밀번호 값이 넘어가긴 합니다.
반응형
최근댓글