0% found this document useful (0 votes)
31 views

UserAuthentication On Firebase With Android

The document provides steps to integrate Google Sign-in authentication with a Firebase project in an Android application. It includes instructions to initialize Firebase and Google Sign-in, handle sign-in and sign-out workflows, and save user credentials to shared preferences for future use. Key steps are to create Google sign-in options, get the GoogleSignInClient, handle sign-in results, update the UI on successful sign-in, and provide a logout button that signs out and redirects the user back to the login screen.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

UserAuthentication On Firebase With Android

The document provides steps to integrate Google Sign-in authentication with a Firebase project in an Android application. It includes instructions to initialize Firebase and Google Sign-in, handle sign-in and sign-out workflows, and save user credentials to shared preferences for future use. Key steps are to create Google sign-in options, get the GoogleSignInClient, handle sign-in results, update the UI on successful sign-in, and provide a logout button that signs out and redirects the user back to the login screen.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1) go to tools in the upper bar of android studio

2) GO to authentication
3) connect to firebase
4) accept on firebase
...if not then go to authentication section of your project
5) dependencies will be added.
6) syncing
7) go to project settings in firebase and go to sdk and do as directed.
8) do above to add the json file (it will be added automatically in the 5 th step)

this in build gradle module

implementation platform('com.google.firebase:firebase-bom:28.3.1')
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.google.firebase:firebase-auth-ktx'
implementation 'com.google.android.gms:play-services-auth:19.2.0'
implementation 'com.google.firebase:firebase-auth:19.2.0'

9) get the sha-1 fingerprint from android studio (secure hash algorithm) 1995
10) message digest MD-5 introduced in 1992

11) write gradle signingReport in terminal and hit ctrl + enter

12) add sha to firebase


13) make activity file for the user sign in and corresponding xml file
14) Add below code above onCreate method in mainActivity.kt file

lateinit var mGoogleSignInClient: GoogleSignInClient


val Req_Code: Int = 123
private lateinit var firebaseAuth: FirebaseAuth

15) Now intialise app(inside onCreate method)


FirebaseApp.initializeApp(this)

16) Now create google sign in options for getting the email//It will show error for
default_web_client_id
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()

17) Now get instance of firebase and at the same time get the client through
GoogleSignInClient(variable is mGoogleSignInClient)

mGoogleSignInClient = GoogleSignIn.getClient(this, gso)


firebaseAuth = FirebaseAuth.getInstance()

18) now make the button for the login screen

We are directly getting without the val Signin = findViewById<Button>(R.id.signin)


By import kotlinx.android.synthetic.main.activity_main.*

TO use above u have to declare plugin in build gradle (Module) in plugin section
id 'kotlin-android-extensions'

Signin.setOnClickListener { view : View? ->


Toast.makeText(this, "Logging In", Toast.LENGTH_SHORT).show()
signInGoogle()
}

19) Now we will create intent for signIN here actual signin will take place(see
above also)
private fun signInGoogle() {
val signInIntent : Intent = mGoogleSignInClient.signInIntent
startActivityForResult(signInIntent, Req_Code)
}

20) After starting activity for result (see above) (intention is signin)
we will handle the onActivityResult with override method (super.onActivityResult
because it may have some defined class in other superClss

override fun onActivityResult(requestCode : Int, resultCode : Int, data : Intent?){


super.onActivityResult(requestCode, resultCode, data)

if(requestCode == Req_Code){
val task : Task<GoogleSignInAccount> =
GoogleSignIn.getSignedInAccountFromIntent(data)
handleResult(task)
}
}

In above we are getting the signedIn account from data

21) Now we will handle the result of the Task<GoogleSignInAccount>

private fun handleResult(completedTask: Task<GoogleSignInAccount>) {


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()
}
}

22) Now we will update the UI after signIn has taken place

Through the account signedIn and also start the HomeActivity

Also build the SavePreference object in order to userCredentials anywhere inside


the app

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, HomeActivity::class.java)
startActivity(intent)
finish()
}
}
}

23) Now if the user has already signedIn we will use the OnStart method to directly
send them to homePage

override fun onStart(){


super.onStart()
if(GoogleSignIn.getLastSignedInAccount(this) != null){
startActivity(Intent(this, HomeActivity::class.java))
finish()
}
}

Logout portion

1) add at the top above the onCreate method


lateinit var mGoogleSignInClient: GoogleSignInClient
private val auth by lazy {
FirebaseAuth.getInstance()
}

2) Now call the request id token for googlesignIn


and build all the properties

val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)


.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
3) Now get the client
mGoogleSignInClient = GoogleSignIn.getClient(this, gso)

4) Now build the listener


For any button

DO directly Logout_btn using


import kotlinx.android.synthetic.main.activity_home_activity.*
and also declare the plugin in module build.gradle
id 'kotlin-android-extensions'

logout.setOnClickListener {
mGoogleSignInClient.signOut().addOnCompleteListener {
val intent = Intent(this, MainActivity::class.java)
Toast.makeText(this, "Logging Out", Toast.LENGTH_SHORT).show()
startActivity(intent)
finish()
}
}

You might also like