Authentication
Authentication is the heart of application session, and it one of Service module main feature. DevBase implement Authentication with multiple login Type to try. Initialize Auth
like this:
private val authHandler by lazy { MobileService.auth }
Tip
It's recommended to initialize AuthenticationHandler
using Dependency Injection with Singleton
Sign In with Google¶
Before Sign-In with Google, Activity needed to be registered with ActivityResultContract before it enter onStart
lifecycle. Do that like this:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
authHandler.registerContract(this)
}
signInWithGoogle
method like this: binding.btnGoogle.setOnClickListener {
authHandler.signInWithGoogle { success, message ->
if (success) {
// Action to do when sign-in successful
} else {
// Action to do when sign-in failed
}
}
}
Sign In with Other Providers¶
Other Providers supported are:
-
Twitter
-
Microsoft
-
Github
-
Yahoo
For other providers, call method signInWith
to start authentication:
authHandler.signInWith(TWITTER, activity) {success, message ->
if(success) {
// Action to do when sign-in successful
} else {
// Action to do when sign-in failed
}
}
Get Current user¶
Get current logged in user by calling this method:
// It returns MobileServiceUser when there's user logged in, and null there's not any
authHandler.getCurrentUser()
Auth Listener¶
App can listen to authentication changes by calling this method:
authHandler.setAuthListener { authenticated
if(authenticated) {
// There's user signed in
} else {
// There's no user signed in
}
}
Logout¶
When logging out, call this method:
authHandler.logout