728x90
반응형
1. Firebase 연동
'Tools' - 'Firebase' - 'Authentication' - 'Authentication using a custom authentication system'
'Tools' - 'Firebase' - 'Realtime Database' - 'Get started with Realtime Database'
를 통해서 연동한다.
또한 Firebase 페이지에서
<Project> - 'Authentication' - 'Sign-in method'에서 이메일로 로그인하는 것을 설정해준다.
2. activity_register.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout ... >
<EditText
android:hint="email"
android:id="@+id/et_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
...
/>
<EditText
android:hint="password"
android:id="@+id/et_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
...
/>
<Button
android:id="@+id/btn_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="가입 완료"
...
/>
</androidx.constraintlayout.widget.ConstraintLayout>
|
cs |
회원가입을 위한 Activity를 만들어줬다.
9, 18 - inputType을 설정해줘서 간단하게 입력 형태를 정해줄 수 있다.
3. UserAccount.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package com.example.firebaseemailaccount;
public class UserAccount {
private String email;
private String password;
private String idToken;
public void setIdToken(String idToken) {
this.idToken = idToken;
}
public String getIdToken() {
return idToken;
}
public UserAccount() { } //빈 생성자가 필요 (firebase 관련)
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public void setEmail(String email) {
this.email = email;
}
public void setPassword(String password) {
this.password = password;
}
}
|
cs |
Database에 저장할 정보를 담을 class를 만들어준다.
4. RegisterActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package com.example.firebaseemailaccount;
import ...
public class RegisterActivity extends AppCompatActivity {
private FirebaseAuth mFirebaseAuth; //파이어베이스 인증 관련
private DatabaseReference mDatabaseReference; // 데이터베이스 관련
private EditText et_email, et_pwd;
private Button btn_register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mFirebaseAuth = FirebaseAuth.getInstance();
mDatabaseReference = FirebaseDatabase.getInstance().getReference("FirebaseEmailAccount");
et_email = findViewById(R.id.et_email);
et_pwd = findViewById(R.id.et_pwd);
btn_register = findViewById(R.id.btn_register);
btn_register.setOnClickListener(v -> {
String strEmail = et_email.getText().toString();
String strPwd = et_pwd.getText().toString();
mFirebaseAuth.createUserWithEmailAndPassword(strEmail, strPwd).addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) { //유저가 다 만들어졌을 때
if(task.isSuccessful()) {
FirebaseUser firebaseUser =mFirebaseAuth.getCurrentUser(); //로그인을 성공해서 가능한 것
UserAccount account = new UserAccount();
account.setEmail(firebaseUser.getEmail());
account.setPassword(strPwd);
account.setIdToken(firebaseUser.getUid());
//database에 저장
mDatabaseReference.child("userAccount").child(firebaseUser.getUid()).setValue(account);
Toast.makeText(RegisterActivity.this, "회원가입에 성공하셨습니다.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(RegisterActivity.this, "회원가입에 실패하셨습니다.", Toast.LENGTH_SHORT).show();
}
}
});
});
}
}
|
cs |
28~46 - 회원가입을 해주고 그에 후에 대한 이벤트를 정해준다.
39 - "FirebaseEmailAccount"/"userAccount"/<Uid>에 account를 저장한다.
5. activity_login.xml
로그인을 할 activity또한 만들어준다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout ... >
<EditText
android:hint="email"
android:id="@+id/et_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
... />
<EditText
android:hint="password"
android:id="@+id/et_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
... />
<Button
android:id="@+id/btn_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="로그인"
... />
<Button
android:backgroundTint="@color/black"
android:id="@+id/btn_register_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="회원가입"
... />
</androidx.constraintlayout.widget.ConstraintLayout>
|
cs |
6. LoginActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package com.example.firebaseemailaccount;
import ...
public class LoginActivity extends AppCompatActivity {
private Button btn_register_1;
private FirebaseAuth mFirebaseAuth; //파이어베이스 인증 관련
private EditText et_email, et_pwd;
private Button btn_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btn_register_1 = findViewById(R.id.btn_register_1);
btn_register_1.setOnClickListener(v -> {
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
});
mFirebaseAuth = FirebaseAuth.getInstance();
et_email = findViewById(R.id.et_email);
et_pwd = findViewById(R.id.et_pwd);
btn_login = findViewById(R.id.btn_login);
btn_login.setOnClickListener(v -> {
String strEmail = et_email.getText().toString();
String strPwd = et_pwd.getText().toString();
mFirebaseAuth.signInWithEmailAndPassword(strEmail, strPwd).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginActivity.this, "로그인 실패", Toast.LENGTH_SHORT).show();
}
}
});
});
}
}
|
cs |
34~45 - 로그인을 시도해주고 그 후의 이벤트를 설정할 수 있다.
7. activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
|
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout ...>
<Button
android:id="@+id/btn_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="로그아웃"
.../>
</androidx.constraintlayout.widget.ConstraintLayout>
|
cs |
8. MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.example.firebaseemailaccount;
import ...
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mFirebaseAuth; //파이어베이스 인증 관련
private DatabaseReference mDatabaseReference; // 데이터베이스 관련
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirebaseAuth =FirebaseAuth.getInstance();
findViewById(R.id.btn_logout).setOnClickListener(v -> {
mFirebaseAuth.signOut();
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
finish();
});
}
}
|
cs |
16 - 로그아웃을 해준다.
728x90
반응형
'Android App' 카테고리의 다른 글
Room Database in Android Studio (0) | 2021.11.27 |
---|---|
RecyclerView with Firebase in Android Studio (0) | 2021.11.27 |
Kakao login in Android Studio (0) | 2021.11.12 |
Google login in Android Studio (0) | 2021.11.08 |
selector로 버튼 꾸미기 in Android Studio (0) | 2021.10.02 |