Firebase 사용하기
Tools>
Firebase>
Authentication 클릭>
Email and password authentication>
Launch in browser>
Connect your app to Firebase>Add Firebase Authtication t.....
.
.
.
MainActivity.java
package com.example.ex11;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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 MainActivity extends AppCompatActivity {
EditText edtemail, edtpass;
FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth=FirebaseAuth.getInstance();
edtemail = findViewById(R.id.edtemail);
edtpass = findViewById(R.id.edtpass);
}
public void mClick(View v) {
String stremail=edtemail.getText().toString();
String strpass=edtpass.getText().toString();
switch (v.getId()) {
case R.id.btnregister:
mAuth.createUserWithEmailAndPassword(stremail,strpass)
.addOnCompleteListener(this,
new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
FirebaseUser user=mAuth.getCurrentUser();
Toast.makeText(MainActivity.this,"가입완료",Toast.LENGTH_SHORT).show();
edtemail.setText("");
edtpass.setText("");
}else{
Toast.makeText(MainActivity.this,"가입실패",Toast.LENGTH_SHORT).show();
}
}
});
break;
case R.id.btnlogin:
mAuth.signInWithEmailAndPassword(stremail, strpass)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(MainActivity.this,"로그인 성공",Toast.LENGTH_SHORT).show();
}else{
AlertDialog.Builder box=new AlertDialog.Builder(MainActivity.this);
box.setMessage("아이디/비밀번호가 틀립니다.");
box.setNegativeButton("확인",null);
box.show();
}
}
});
break;
case R.id.btncancel:
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:padding="30sp">
<EditText
android:id="@+id/edtemail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="text"/>
<EditText
android:id="@+id/edtpass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>
<Button
android:background="#ffffff"
android:onClick="mClick"
android:id="@+id/btnregister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"/>
<Button
android:background="#ffffff"
android:onClick="mClick"
android:id="@+id/btnlogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"/>
<Button
android:background="#ffffff"
android:onClick="mClick"
android:id="@+id/btncancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel"/>
</LinearLayout>
'Android' 카테고리의 다른 글
RecyclerView를 이용한 상품목록만들기 (0) | 2019.11.13 |
---|---|
recyclerview 사용하기 (0) | 2019.11.13 |
검색할 수 있는 메모장 만들기 (1) | 2019.11.12 |
캘린더를 활용한 다이어리 앱 만들기 (0) | 2019.11.11 |
주소록만들기(버튼,옵션메뉴) (0) | 2019.11.07 |