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('[Link]:firebase-bom:28.3.1')
implementation '[Link]:firebase-analytics-ktx'
implementation '[Link]:firebase-auth-ktx'
implementation '[Link]:play-services-auth:19.2.0'
implementation '[Link]: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 [Link] file
lateinit var mGoogleSignInClient: GoogleSignInClient
val Req_Code: Int = 123
private lateinit var firebaseAuth: FirebaseAuth
15) Now intialise app(inside onCreate method)
[Link](this)
16) Now create google sign in options for getting the email//It will show error for
default_web_client_id
val gso = [Link](GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString([Link].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 = [Link](this, gso)
firebaseAuth = [Link]()
18) now make the button for the login screen
We are directly getting without the val Signin = findViewById<Button>([Link])
By import [Link].activity_main.*
TO use above u have to declare plugin in build gradle (Module) in plugin section
id 'kotlin-android-extensions'
[Link] { view : View? ->
[Link](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 = [Link]
startActivityForResult(signInIntent, Req_Code)
}
20) After starting activity for result (see above) (intention is signin)
we will handle the onActivityResult with override method ([Link]
because it may have some defined class in other superClss
override fun onActivityResult(requestCode : Int, resultCode : Int, data : Intent?){
[Link](requestCode, resultCode, data)
if(requestCode == Req_Code){
val task : Task<GoogleSignInAccount> =
[Link](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? =
[Link](ApiException::[Link])
if(account != null){
UpdateUI(account)
}
} catch (e : ApiException){
[Link](this, [Link](), 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 = [Link]([Link], null)
[Link](credential).addOnCompleteListener{ task -
>
if([Link]){
[Link](this, [Link]())
[Link](this, [Link]())
val intent = Intent(this, HomeActivity::[Link])
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(){
[Link]()
if([Link](this) != null){
startActivity(Intent(this, HomeActivity::[Link]))
finish()
}
}
Logout portion
1) add at the top above the onCreate method
lateinit var mGoogleSignInClient: GoogleSignInClient
private val auth by lazy {
[Link]()
}
2) Now call the request id token for googlesignIn
and build all the properties
val gso = [Link](GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString([Link].default_web_client_id))
.requestEmail()
.build()
3) Now get the client
mGoogleSignInClient = [Link](this, gso)
4) Now build the listener
For any button
DO directly Logout_btn using
import [Link].activity_home_activity.*
and also declare the plugin in module [Link]
id 'kotlin-android-extensions'
[Link] {
[Link]().addOnCompleteListener {
val intent = Intent(this, MainActivity::[Link])
[Link](this, "Logging Out", Toast.LENGTH_SHORT).show()
startActivity(intent)
finish()
}
}