Firebase是一个移动和Web应用程序开发平台。它提供Web应用程序或移动应用程序可能需要的服务。 Firebase提供了电子邮件和密码身份验证,而没有构建用于用户身份验证的后端的任何开销。 Google登录是一种安全的方法,可以对您的应用程序中的用户进行身份验证。它减少了用户处理和处理这些额外密码以进行应用身份验证的麻烦。 Firebase提供了许多选项来在您的应用程序中实现登录,例如电子邮件,电话号码,Google,Facebook等。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
第2步:将您的应用连接到Firebase
在Android Studio中创建新项目后。将您的应用程序连接到Firebase。用于将您的应用程序连接到Firebase。导航到顶部栏上的“工具”。之后,单击Firebase。右侧将打开一个新窗口。在该窗口内,单击身份验证,然后单击电子邮件和密码身份验证。
单击电子邮件和密码身份验证后,您将看到以下屏幕。在此屏幕内,单击第一个选项以连接到Firebase,然后单击第二个选项以将Firebase身份验证添加到您的应用程序。
如果尚不存在以下依赖项,请添加。
implementation ‘com.google.firebase:firebase-auth:16.0.5’
implementation ‘com.google.android.gms:play-services-auth:18.1.0’
第3步:启用Google登录
步骤4:使用activity_main.xml文件
导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。
XML
Kotlin
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.tasks.Task
import com.google.firebase.FirebaseApp
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.GoogleAuthProvider
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
lateinit var mGoogleSignInClient: GoogleSignInClient
val Req_Code: Int = 123
private lateinit var firebaseAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
FirebaseApp.initializeApp(this)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
firebaseAuth = FirebaseAuth.getInstance()
Signin.setOnClickListener { view: View? ->
Toast.makeText(this, "Logging In", Toast.LENGTH_SHORT).show()
signInGoogle()
}
}
private fun signInGoogle() {
val signInIntent: Intent = mGoogleSignInClient.signInIntent
startActivityForResult(signInIntent, Req_Code)
}
// onActivityResult() function : this is where
// we provide the task and data for the Google Account
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == Req_Code) {
val task: Task = GoogleSignIn.getSignedInAccountFromIntent(data)
handleResult(task)
}
}
private fun handleResult(completedTask: Task) {
try {
val account: GoogleSignInAccount? = completedTask.getResult(ApiException::class.java)
if (account != null) {
UpdateUI(account)
}
} catch (e: ApiException) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show()
}
}
// this is where we update the UI after Google signin takes place
private fun UpdateUI(account: GoogleSignInAccount) {
val credential = GoogleAuthProvider.getCredential(account.idToken, null)
firebaseAuth.signInWithCredential(credential).addOnCompleteListener { task ->
if (task.isSuccessful) {
SavedPreference.setEmail(this, account.email.toString())
SavedPreference.setUsername(this, account.displayName.toString())
val intent = Intent(this, DashboardActivity::class.java)
startActivity(intent)
finish()
}
}
}
override fun onStart() {
super.onStart()
if (GoogleSignIn.getLastSignedInAccount(this) != null) {
startActivity(
Intent(
this, DashboardActivity
::class.java
)
)
finish()
}
}
}
XML
Kotlin
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_dashboard.*
class DashboardActivity : AppCompatActivity() {
// declare the GoogleSignInClient
lateinit var mGoogleSignInClient: GoogleSignInClient
private val auth by lazy {
FirebaseAuth.getInstance()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)
// call requestIdToken as follows
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
logout.setOnClickListener {
mGoogleSignInClient.signOut().addOnCompleteListener {
val intent = Intent(this, MainActivity::class.java)
Toast.makeText(this, "Logging Out", Toast.LENGTH_SHORT).show()
startActivity(intent)
finish()
}
}
}
}
步骤5:使用MainActivity.kt文件
转到MainActivity.kt文件,并参考以下代码。下面是MainActivity.kt文件的代码。在代码内部添加了注释,以更详细地了解代码。
科特林
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.tasks.Task
import com.google.firebase.FirebaseApp
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.GoogleAuthProvider
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
lateinit var mGoogleSignInClient: GoogleSignInClient
val Req_Code: Int = 123
private lateinit var firebaseAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
FirebaseApp.initializeApp(this)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
firebaseAuth = FirebaseAuth.getInstance()
Signin.setOnClickListener { view: View? ->
Toast.makeText(this, "Logging In", Toast.LENGTH_SHORT).show()
signInGoogle()
}
}
private fun signInGoogle() {
val signInIntent: Intent = mGoogleSignInClient.signInIntent
startActivityForResult(signInIntent, Req_Code)
}
// onActivityResult() function : this is where
// we provide the task and data for the Google Account
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == Req_Code) {
val task: Task = GoogleSignIn.getSignedInAccountFromIntent(data)
handleResult(task)
}
}
private fun handleResult(completedTask: Task) {
try {
val account: GoogleSignInAccount? = completedTask.getResult(ApiException::class.java)
if (account != null) {
UpdateUI(account)
}
} catch (e: ApiException) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show()
}
}
// this is where we update the UI after Google signin takes place
private fun UpdateUI(account: GoogleSignInAccount) {
val credential = GoogleAuthProvider.getCredential(account.idToken, null)
firebaseAuth.signInWithCredential(credential).addOnCompleteListener { task ->
if (task.isSuccessful) {
SavedPreference.setEmail(this, account.email.toString())
SavedPreference.setUsername(this, account.displayName.toString())
val intent = Intent(this, DashboardActivity::class.java)
startActivity(intent)
finish()
}
}
}
override fun onStart() {
super.onStart()
if (GoogleSignIn.getLastSignedInAccount(this) != null) {
startActivity(
Intent(
this, DashboardActivity
::class.java
)
)
finish()
}
}
}
步骤6:建立新的空白活动
请参考在Android Studio中创建新活动并将该活动命名为DashboardActivity。
步骤7:使用activity_dashboard.xml文件
转到activity_dashboard.xml文件,并参考以下代码。以下是activity_dashboard.xml文件的代码。创建一个按钮,单击该按钮可将用户从应用程序中注销。
XML格式
步骤8:使用DashboardActivity.kt文件
转到DashboardActivity.kt文件并参考以下代码。下面是DashboardActivity.kt文件的代码。在代码内部添加了注释,以更详细地了解代码。
科特林
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_dashboard.*
class DashboardActivity : AppCompatActivity() {
// declare the GoogleSignInClient
lateinit var mGoogleSignInClient: GoogleSignInClient
private val auth by lazy {
FirebaseAuth.getInstance()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)
// call requestIdToken as follows
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
logout.setOnClickListener {
mGoogleSignInClient.signOut().addOnCompleteListener {
val intent = Intent(this, MainActivity::class.java)
Toast.makeText(this, "Logging Out", Toast.LENGTH_SHORT).show()
startActivity(intent)
finish()
}
}
}
}
输出: