Skip to content

Instantly share code, notes, and snippets.

@vorobeij
Created December 19, 2018 09:25
Show Gist options
  • Save vorobeij/8c384d492e1d756dde2816fb7b4171c7 to your computer and use it in GitHub Desktop.
Save vorobeij/8c384d492e1d756dde2816fb7b4171c7 to your computer and use it in GitHub Desktop.
Google Firebase Auth
package XXX
import android.content.Intent
import android.os.Bundle
import au.sjowl.base.basex.BaseActivity
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import io.michaelrocks.lightsaber.getInstance
import kotlinx.android.synthetic.main.activity_auth.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
open class AuthActivity :
BaseActivity<AuthPresenter, AuthView>(),
AuthView {
override val layoutId: Int get() = R.layout.activity_auth
override fun providePresenter(): AuthPresenter = appInjector.getInstance()
/* ******************** google sign in *********************/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
signInButton.setOnClickListener { presenter.onGoogleSignIn() }
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
RC_SIGN_IN -> {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
presenter.onSignedIn(task)
}
}
}
/* ******************** View *********************/
override suspend fun closeView() = GlobalScope.launch(uiDispatcher) {
finish()
}
override suspend fun googleSignIn(googleSignInClient: GoogleSignInClient) = GlobalScope.launch(uiDispatcher) {
val signInIntent = googleSignInClient.signInIntent
startActivityForResult(signInIntent, RC_SIGN_IN)
}
companion object {
private const val RC_SIGN_IN = 123
}
}
package xxx
import android.content.Context
import au.sjowl.base.utils.log
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.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.GoogleAuthProvider
import javax.inject.Inject
class AuthInteractor @Inject constructor(
private val context: Context
) {
private val googleSignInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(BuildConfig.WEB_CLIENT_ID)
.requestEmail()
.build()
val googleSignInClient: GoogleSignInClient = GoogleSignIn.getClient(context, googleSignInOptions)
private val firebaseAuth = FirebaseAuth.getInstance()
private var googleAccount: GoogleSignInAccount? = null
fun isLogin() = FirebaseAuth.getInstance().currentUser != null
val account get() = GoogleSignIn.getLastSignedInAccount(context)
fun signInWithGoogle(task: Task<GoogleSignInAccount>) {
task.getResult(ApiException::class.java)?.let { account ->
googleAccount = account
firebaseAuthWithGoogle(account)
}
}
private fun firebaseAuthWithGoogle(account: GoogleSignInAccount) {
log("Google token:" + account.idToken)
val credential = GoogleAuthProvider.getCredential(account.idToken, null)
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val user = firebaseAuth.currentUser as FirebaseUser
}
}
}
fun signOut() {
FirebaseAuth.getInstance().signOut()
}
}
package XXX
import android.content.Context
import au.sjowl.base.basex.BasePresenter
import au.sjowl.base.basex.BaseTiView
import au.sjowl.base.utils.SLog
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.tasks.Task
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject
class AuthPresenter @Inject constructor(
val authInteractor: AuthInteractor,
val context: Context
) : BasePresenter<AuthView>() {
fun onGoogleSignIn() {
GlobalScope.launch(bgDispatcher + exceptionHandler) {
view?.googleSignIn(authInteractor.googleSignInClient)
}
}
fun onSignedIn(task: Task<GoogleSignInAccount>) {
GlobalScope.launch(bgDispatcher + exceptionHandler) {
authInteractor.signInWithGoogle(task)
view?.closeView()
}
}
override val exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable ->
SLog.e(throwable.message.orEmpty())
throwable.printStackTrace()
}
}
interface AuthView : BaseTiView {
suspend fun googleSignIn(googleSignInClient: GoogleSignInClient): Job
suspend fun closeView(): Job
}
// ...
android {
defaultConfig {
// ...
buildConfigField "String", "WEB_CLIENT_ID", '"Place your project web-client id"'
}
// ...
}
// launch activity
class HomePresenter @Inject constructor(
private val authInteractor: AuthInteractor
) : BasePresenter<HomeView>() {
override fun onCreate() {
super.onCreate()
if (authInteractor.isLogin()) {
} else {
sendToView { view?.showLogin() }
}
}
}
interface HomeView : BaseTiView {
fun showLogin()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment