UserAuthentication On Firebase With Android
UserAuthentication On Firebase With Android
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)
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
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)
TO use above u have to declare plugin in build gradle (Module) in plugin section
id 'kotlin-android-extensions'
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
if(requestCode == Req_Code){
val task : Task<GoogleSignInAccount> =
GoogleSignIn.getSignedInAccountFromIntent(data)
handleResult(task)
}
}
22) Now we will update the UI after signIn has taken place
23) Now if the user has already signedIn we will use the OnStart method to directly
send them to homePage
Logout portion
logout.setOnClickListener {
mGoogleSignInClient.signOut().addOnCompleteListener {
val intent = Intent(this, MainActivity::class.java)
Toast.makeText(this, "Logging Out", Toast.LENGTH_SHORT).show()
startActivity(intent)
finish()
}
}