Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- JDBC
- http
- AOP
- Android
- jpa
- Greedy
- 백준
- Thymeleaf
- SpringBoot
- springdatajpa
- 인프런
- 그리디
- kotlin
- java
- 김영한
- QueryDSL
- 알고리즘
- 스프링 핵심 기능
- Proxy
- Servlet
- Spring Boot
- transaction
- 자바
- pointcut
- 스프링
- db
- spring
- JPQL
- Exception
- 스프링 핵심 원리
Archives
- Today
- Total
개발자되기 프로젝트
[Firebase] 이메일/비밀번호 로그인 본문
1. 예제 코드
package com.example.fb_email_pw_auth
import android.content.ContentValues.TAG
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
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() {
private lateinit var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// ...
// Initialize Firebase Auth
auth = Firebase.auth
//회원가입
val joinBtn = findViewById<Button>(R.id.joinBtn)
joinBtn.setOnClickListener {
val email = findViewById<EditText>(R.id.emailArea)
val password = findViewById<EditText>(R.id.passwordArea)
Log.d("MAIN", email.text.toString())
Log.d("MAIN", password.text.toString())
//이메일,비번 로그인
auth.createUserWithEmailAndPassword(email.text.toString(), password.text.toString())
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Toast.makeText(this, "회원가입/로그인 성공", Toast.LENGTH_LONG).show()
} else {
// If sign in fails, display a message to the user.
Toast.makeText(this, "회원가입 실패", Toast.LENGTH_LONG).show()
}
}
}
//로그인
val loginBtn = findViewById<Button>(R.id.loginBtn)
loginBtn.setOnClickListener {
val email = findViewById<EditText>(R.id.emailArea)
val password = findViewById<EditText>(R.id.passwordArea)
auth.signInWithEmailAndPassword(email.text.toString(), password.text.toString())
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Toast.makeText(this, "로그인 성공", Toast.LENGTH_LONG).show()
} else {
// If sign in fails, display a message to the user.
Toast.makeText(this, "로그인인 실패", Toast.LENGTH_LONG).show()
}
}
}
//로그아웃
val logoutBtn = findViewById<Button>(R.id.logoutBtn)
logoutBtn.setOnClickListener {
Firebase.auth.signOut()
Toast.makeText(this, "로그아웃 완료", Toast.LENGTH_LONG).show()
}
}
}
2. layout
<?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/passwordArea"
android:hint="password"
android:inputType="textPassword"
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"/>
<Button
android:id="@+id/loginBtn"
android:text="로그인"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/logoutBtn"
android:text="로그아웃"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
'인프런 > [인프런] 앱 8개를 만들면서 배우는 안드로이드 코틀린' 카테고리의 다른 글
[Firebase] RealTime Database (0) | 2022.03.22 |
---|---|
[Android] Dialog띄우기 (0) | 2022.03.22 |
[Firebae] Firebase 사용하기, 익명 로그인 (0) | 2022.03.22 |
[Android] Fragment, Navigation (0) | 2022.03.20 |
[Android] RecyclerView (0) | 2022.03.20 |
Comments