daily pastebin goal
67%
SHARE
TWEET

Untitled

a guest Jul 11th, 2018 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package me.levansmith.dockit.util.manager
  2.  
  3.  
  4. import android.app.Activity
  5. import android.support.v4.app.FragmentActivity
  6. import com.google.android.gms.auth.api.Auth
  7. import com.google.android.gms.auth.api.signin.GoogleSignInAccount
  8. import com.google.android.gms.auth.api.signin.GoogleSignInOptions
  9. import com.google.android.gms.common.ConnectionResult
  10. import com.google.android.gms.common.api.GoogleApiClient
  11. import com.google.android.gms.tasks.OnCompleteListener
  12. import com.google.android.gms.tasks.Task
  13. import com.google.firebase.auth.*
  14. import kotlinx.coroutines.experimental.launch
  15. import me.levansmith.dockit.R
  16. import me.levansmith.dockit.util.Constants
  17.  
  18. object AuthManager : OnCompleteListener<AuthResult>, GoogleApiClient.OnConnectionFailedListener {
  19.  
  20.     private val mAuth: FirebaseAuth = FirebaseAuth.getInstance()
  21.     var user: FirebaseUser? = mAuth.currentUser
  22.     private var listener: AuthListener? = null
  23.     private var mGoogleApiClient: GoogleApiClient? = null
  24.  
  25.     val uid: String?
  26.         get() = if (user != null) {
  27.             user!!.uid
  28.         } else null
  29.  
  30.     val isLoggedIn: Boolean
  31.         get() = user != null
  32.  
  33.     fun subscribe(authListener: AuthListener?): AuthManager {
  34.         listener = authListener
  35.         return this
  36.     }
  37.  
  38.     fun signout() {
  39.         mAuth.signOut()
  40.     }
  41.  
  42.     fun createEmailUser(activity: Activity, email: String, password: String) {
  43.         launch {
  44.             if (user == null) {
  45.                 mAuth.createUserWithEmailAndPassword(email, password)
  46.                         .addOnCompleteListener(activity, this@AuthManager)
  47.             }
  48.         }
  49.     }
  50.  
  51.     fun signInEmailUser(activity: Activity, email: String, password: String) {
  52.         launch {
  53.             if (user == null) {
  54.                 mAuth.signInWithEmailAndPassword(email, password)
  55.                         .addOnCompleteListener(activity, this@AuthManager)
  56.                         .addOnFailureListener {
  57.                             when(it) {
  58.                                 is FirebaseAuthInvalidUserException -> {
  59.                                     createEmailUser(activity, email, password)
  60.                                 }
  61.                             }
  62.                         }
  63.             }
  64.         }
  65.     }
  66.  
  67.     fun signInGoogleUser(activity: FragmentActivity) {
  68.         if (mGoogleApiClient == null) {
  69.             configureGoogleApiClient(activity)
  70.         }
  71.         val intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient)
  72.         activity.startActivityForResult(intent, Constants.RequestCodes.SIGN_IN_GOOGLE)
  73.     }
  74.  
  75.     fun authenticateGoogleUser(activity: Activity, account: GoogleSignInAccount?) {
  76.         if (account != null) {
  77.             val credential = GoogleAuthProvider.getCredential(account.idToken, null)
  78.             mAuth.signInWithCredential(credential)
  79.                     .addOnCompleteListener(activity, this)
  80.         } else {
  81.             listener!!.onError(Exception("Error: Google account passed in is null"))
  82.         }
  83.     }
  84.  
  85.     private fun configureGoogleApiClient(activity: FragmentActivity) {
  86.         val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  87.                 .requestIdToken(activity.getString(R.string.default_web_client_id))
  88.                 .requestEmail()
  89.                 .build()
  90.  
  91.         mGoogleApiClient = GoogleApiClient.Builder(activity)
  92.                 .enableAutoManage(activity, this)
  93.                 .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
  94.                 .build()
  95.     }
  96.  
  97.     override fun onComplete(task: Task<AuthResult>) {
  98.         if (task.isSuccessful) {
  99.             user = mAuth.currentUser
  100.             if (listener != null) {
  101.                 listener!!.onComplete()
  102.                 listener = null
  103.             }
  104.         } else {
  105.             user = null
  106.             if (listener != null) {
  107.                 listener!!.onError(Exception(task.exception))
  108.             }
  109.         }
  110.     }
  111.  
  112.     override fun onConnectionFailed(connectionResult: ConnectionResult) {
  113.         if (listener != null) {
  114.             listener!!.onError(Exception(String.format("Error %d: " + connectionResult.errorMessage!!, connectionResult.errorCode)))
  115.         }
  116.     }
  117.  
  118.     interface AuthListener {
  119.  
  120.         fun onComplete()
  121.  
  122.         fun onError(e: Exception)
  123.     }
  124. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top