아주간단한 회원가입,로그인하는 예제를 만들어볼것이다
먼저 안드로이드 스튜디오에서 newSamPleApp이라는 새로운 프로젝트를 생성해준다
파이어베이스에도 mySampleProject라는 프로젝트를 생성해준뒤 Tools-> Firebase -> Analytics -> Connect your app to Firebase에 있는 첫번째 버튼을 눌러 안드로이드 스튜디오와 연결해준다
그런다음 Firebase Authentication -> Sign in method로 들어가서 이메일/비밀번호 , 익명 상태를 활성화시켜준다
익명을 활성화시켜준 이유는 비회원일때도 앱 사용을 가능하게하기 위해서이다
다시 안드로이드 스튜디오로 돌아온다음 Firebase공식문서(https://firebase.google.com/docs/auth/android/start?hl=ko)를 참고해서 build.gradle(app)에 아래 firebase에 해당하는 코드를 입력해준 뒤 sync now를 눌러 동기화시켜준다
dependencies {
...
implementation("com.google.firebase:firebase-analytics-ktx:21.5.0")
implementation(platform("com.google.firebase:firebase-bom:32.3.1"))
implementation("com.google.firebase:firebase-auth-ktx")
}
build.gradle(app)
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="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/emailArea"
android:hint="email"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/pwdArea"
android:hint="password"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/joinBtn"
android:text="회원가입"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
activity_main.xml
MainActivity로 돌아가서 Firebase공식문서에 있는대로 FirebaseAuth의 인스턴스를 선언하고, onCreate() 메서드에서 FirebaseAuth 인스턴스를 초기화한다.
package com.example.mysampleapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
class MainActivity : AppCompatActivity() {
//FirebaseAuth의 인스턴스를 선언
private lateinit var auth : FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
// FirebaseAuth 인스턴스를 초기화
auth = Firebase.auth
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 회원가입하기 버튼 눌렀을때
val joinBtnClicked = findViewById<Button>(R.id.joinBtn)
joinBtnClicked.setOnClickListener {
auth.createUserWithEmailAndPassword("abc@naver.com", "12341234")
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "no", Toast.LENGTH_SHORT).show()
}
}
}
}
}
MainActivity.kt
이렇게해서 앱을 실행시켜서 회원가입을 눌러서 Firebase를 확인해보면 abc@naver.com으로 회원가입이 된것을 확인할수있다
'Android Project' 카테고리의 다른 글
예제 앱 만들기(3) - 게시글 등록 (0) | 2024.01.30 |
---|---|
예제 앱 만들기(2) - 데이터바인딩, 로그인, 로그아웃 (0) | 2024.01.30 |
[Android/Kotlin] 커뮤니티앱(12) - 내가 쓴글만 수정,삭제 가능하도록 (0) | 2024.01.28 |
[Android/Kotlin] 커뮤니티앱(11) - 로그아웃 (0) | 2024.01.28 |
[Android/Kotlin] 커뮤니티앱(10) - 게시글 삭제, 수정 (0) | 2024.01.28 |