Full Stack 교육 회고록

미니 어플리케이션 만들기(2)- 로그인, 파이어베이스, firebase 연결

순두부 호랑이 2022. 12. 11. 00:55
728x90
SMALL

LoginActivity

EditText Custom,Button 넣어주기 (Button 누르면 이동)

 

JoinActivity
비밀번호, 비밀번호 재입력, 이메일 EditText 구현 및 Button 만들기

 

alt+ Enter로 import 해주기

 

[textview 가운데 정렬은 gravity => center]

Hint :  입력 창 글씨 삽입과 없어지기 기능

[plain text아래 밑줄 사라지게 만드는것]

blackground => @android:color/transparent

[비밀번호 inputType은 textPassword로 변경]

[firebase연결]

프로젝트 추가 선택  -> 3 단계 순서대로 진행하기

- 내가 개발하는 것 선택 저는 안드로이드 개발을 하기 때문에 안드로이드를 선택하겠습니다.

패키지 이름 맞춰주기

다운로드후 진행해준다

app에 다운받은 json 파일을 넣어준다

Gradle Scripts>bulid.grade(project: MibiApplication) 에 

//firebase
id 'com.google.gms.google-services' version '4.3.13' apply false

작성후  우측 상단 sysn Now 버튼 클릭

build.gradle(module:MiniApplication.app)에

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    //firebase
    id 'com.google.gms.google-services'
}

dependencies {
    //firebase
    implementation platform('com.google.firebase:firebase-bom:30.3.1')
    implementation 'com.google.firebase:firebase-database:20.0.5'
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-crashlytics-buildtools:2.9.2'
    // firebase auth
    implementation 'com.google.firebase:firebase-auth-ktx:21.0.8'

    // firebase의 database에 접근하려면
    implementation 'com.google.firebase:firebase-database-ktx:20.0.6'

    // firebase의 storage사용
    implementation 'com.google.firebase:firebase-storage-ktx:20.0.2'

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}

firebase 추가후 Sysn Now 클릭

Authentication 시작하기 선택

 

[이메일 인증하기]

이메일/비밀번호 만 사용 설정 해준다

 

이렇게 추가가 된다

 

https://firebase.google.com/docs/auth/android/start#kotlin+ktx_1 

 

Android에서 Firebase 인증 시작하기

Firebase Summit에서 발표된 모든 내용을 살펴보고 Firebase로 앱을 빠르게 개발하고 안심하고 앱을 실행하는 방법을 알아보세요. 자세히 알아보기 이 페이지는 Cloud Translation API를 통해 번역되었습니

firebase.google.com

문서>Android>시작하기

회원가입이 오류나서 고쳐야 합니다....

회원가입 실패는 토스트로 뜨는데 성공은 안뜬다... 

<activity_login>

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".auth.LoginActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:background="@color/mainColor"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="로그인"
            android:textColor="#000000"
            android:textSize="24sp"
            android:textStyle="bold" />
    </LinearLayout>

    <EditText
        android:id="@+id/etLoginEmail"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="16dp"
        android:background="@android:color/transparent"
        android:ems="10"
        android:hint="이메일"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

    <EditText
        android:id="@+id/etLoginPw"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginTop="16dp"
        android:background="@android:color/transparent"
        android:ems="10"
        android:hint="비밀번호"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="@+id/etLoginEmail"
        app:layout_constraintStart_toStartOf="@+id/etLoginEmail"
        app:layout_constraintTop_toBottomOf="@+id/etLoginEmail" />

    <Button
        android:id="@+id/btnLoginLogin"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginTop="16dp"
        android:background="@drawable/button_radius_yellow"
        android:text="로그인하기"
        android:textSize="16sp"
        app:layout_constraintEnd_toEndOf="@+id/etLoginPw"
        app:layout_constraintStart_toStartOf="@+id/etLoginPw"
        app:layout_constraintTop_toBottomOf="@+id/etLoginPw" />
</androidx.constraintlayout.widget.ConstraintLayout>

[LoginActivity]

package com.example.miniapplication.auth

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import com.example.miniapplication.R

class LoginActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        val btnLoginLogin = findViewById<Button>(R.id.btnLoginLogin)
        val etLoginEmail = findViewById<EditText>(R.id.etLoginEmail)
        val etLoginPw = findViewById<EditText>(R.id.etLoginPw)

        // Login 버튼을 눌렀을 때
        btnLoginLogin.setOnClickListener {
            val email = etLoginEmail.text.toString()
            val pw = etLoginPw.text.toString()

            Toast.makeText(this@LoginActivity, "$email, $pw ", Toast.LENGTH_SHORT).show()
        }
    }
}

[activity_join.xml]

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".auth.JoinActivity">

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:background="@color/mainColor"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="회원가입"
            android:textColor="#000000"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

    <EditText
        android:id="@+id/etJoinEmail"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="16dp"
        android:background="@android:color/transparent"
        android:ems="10"
        android:hint="이메일"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout2" />

    <EditText
        android:id="@+id/etJoinPw"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginTop="16dp"
        android:background="@android:color/transparent"
        android:ems="10"
        android:hint="비밀번호"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="@+id/etJoinEmail"
        app:layout_constraintStart_toStartOf="@+id/etJoinEmail"
        app:layout_constraintTop_toBottomOf="@+id/etJoinEmail" />

    <EditText
        android:id="@+id/etJoinCheck"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginTop="16dp"
        android:background="@android:color/transparent"
        android:ems="10"
        android:hint="비밀번호 재입력"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="@+id/etJoinPw"
        app:layout_constraintStart_toStartOf="@+id/etJoinPw"
        app:layout_constraintTop_toBottomOf="@+id/etJoinPw" />

    <Button
        android:id="@+id/btnJoinJoin"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginTop="16dp"
        android:background="@drawable/button_radius_yellow"
        android:text="회원가입하기"
        android:textSize="16sp"
        app:layout_constraintEnd_toEndOf="@+id/etJoinCheck"
        app:layout_constraintHorizontal_bias="0.491"
        app:layout_constraintStart_toStartOf="@+id/etJoinCheck"
        app:layout_constraintTop_toBottomOf="@+id/etJoinCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>

[joinActivity]

package com.example.miniapplication.auth

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import com.example.miniapplication.R
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase


class JoinActivity : AppCompatActivity() {

    //FirebaseAuth 선언
    lateinit var auth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_join)

        val btnJoinJoin = findViewById<Button>(R.id.btnJoinJoin)
        val etJoinEmail = findViewById<EditText>(R.id.etJoinEmail)
        val etJoinPw = findViewById<EditText>(R.id.etJoinPw)
        val etJoinCheck = findViewById<EditText>(R.id.etJoinCheck)

        // auth를 초기화화
        auth= Firebase.auth
        // Firebase.auth : 로그인, 회원가입, 인증 시스템에 대한 모든 기능이 담겨 있다!

       //btnJoinJoin을 눌렀을 때
        btnJoinJoin.setOnClickListener {
            val email = etJoinEmail.text.toString()
            val pw = etJoinPw.text.toString()

//            Toast.makeText(this@JoinActivity, "$email, $pw",
//            Toast.LENGTH_SHORT).show()

            //create가 보내고 있는 전달인자 2개(email, pw)는 실제로 회원가입 정보 전달(firebase로 전달)
            auth.createUserWithEmailAndPassword("asdf@naver.com","12345678")
                .addOnCompleteListener(this) { task ->
                    //task --> 보낸 후 결과(성공했는지 실패했는지
                    if (task.isSuccessful) {
                        //성공했을 때 실행시킬 코드
                        Toast.makeText(this, "회원가입 성공", Toast.LENGTH_SHORT).show()
                    } else {
                        //실패했을 때 실행시킬 코드
                        Toast.makeText(this,"회원가입 실패", Toast.LENGTH_SHORT).show()
                    }
                }
        }
    }
}
728x90
LIST