-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
774ac5e
commit df3d697
Showing
4 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
...muelokello/kazihub/presentation/shared/authentication/SignIn/Google/GoogleAuthUiClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.samuelokello.kazihub.presentation.shared.authentication.SignIn.Google | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentSender | ||
import com.google.android.gms.auth.api.identity.BeginSignInRequest | ||
import com.google.android.gms.auth.api.identity.BeginSignInRequest.GoogleIdTokenRequestOptions | ||
import com.google.android.gms.auth.api.identity.SignInClient | ||
import com.google.firebase.auth.GoogleAuthProvider | ||
import com.google.firebase.auth.ktx.auth | ||
import com.google.firebase.ktx.Firebase | ||
import com.samuelokello.kazihub.R | ||
import com.samuelokello.kazihub.presentation.shared.authentication.SignIn.SignInResult | ||
import com.samuelokello.kazihub.presentation.shared.authentication.SignIn.UserData | ||
import kotlinx.coroutines.CancellationException | ||
import kotlinx.coroutines.tasks.await | ||
|
||
class GoogleAuthUiClient( | ||
private val context: Context, | ||
private val oneTapClient: SignInClient | ||
) { | ||
private val auth = Firebase.auth | ||
|
||
suspend fun signIn(): IntentSender? { | ||
val result = try { | ||
oneTapClient.beginSignIn( | ||
buildSignInRequest() | ||
).await() | ||
} catch(e: Exception) { | ||
e.printStackTrace() | ||
if(e is CancellationException) throw e | ||
null | ||
} | ||
return result?.pendingIntent?.intentSender | ||
} | ||
|
||
suspend fun signInWithIntent(intent: Intent): SignInResult { | ||
val credential = oneTapClient.getSignInCredentialFromIntent(intent) | ||
val googleIdToken = credential.googleIdToken | ||
val googleCredentials = GoogleAuthProvider.getCredential(googleIdToken, null) | ||
return try { | ||
val user = auth.signInWithCredential(googleCredentials).await().user | ||
SignInResult( | ||
data = user?.run { | ||
UserData( | ||
userId = uid, | ||
username = displayName, | ||
profilePictureUrl = photoUrl?.toString() | ||
) | ||
}, | ||
errorMessage = null | ||
) | ||
} catch(e: Exception) { | ||
e.printStackTrace() | ||
if(e is CancellationException) throw e | ||
SignInResult( | ||
data = null, | ||
errorMessage = e.message | ||
) | ||
} | ||
} | ||
|
||
suspend fun signOut() { | ||
try { | ||
oneTapClient.signOut().await() | ||
auth.signOut() | ||
} catch(e: Exception) { | ||
e.printStackTrace() | ||
if(e is CancellationException) throw e | ||
} | ||
} | ||
|
||
fun getSignedInUser(): UserData? = auth.currentUser?.run { | ||
UserData( | ||
userId = uid, | ||
username = displayName, | ||
profilePictureUrl = photoUrl?.toString() | ||
) | ||
} | ||
|
||
private fun buildSignInRequest(): BeginSignInRequest { | ||
return BeginSignInRequest.Builder() | ||
.setGoogleIdTokenRequestOptions( | ||
GoogleIdTokenRequestOptions.builder() | ||
.setSupported(true) | ||
.setFilterByAuthorizedAccounts(false) | ||
.setServerClientId(context.getString(R.string.web_client_id)) | ||
.build() | ||
) | ||
.setAutoSelectEnabled(true) | ||
.build() | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...in/java/com/samuelokello/kazihub/presentation/shared/authentication/SignIn/SignInEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.samuelokello.kazihub.presentation.shared.authentication.SignIn | ||
|
||
sealed interface SignInEvent { | ||
data class onEmailChanged(val email: String): SignInEvent | ||
data class onPasswordChanged(val password: String): SignInEvent | ||
} |
12 changes: 12 additions & 0 deletions
12
...n/java/com/samuelokello/kazihub/presentation/shared/authentication/SignIn/SignInResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.samuelokello.kazihub.presentation.shared.authentication.SignIn | ||
|
||
data class SignInResult( | ||
val data: UserData?, | ||
val errorMessage: String? | ||
) | ||
|
||
data class UserData( | ||
val userId: String, | ||
val username: String?, | ||
val profilePictureUrl: String? | ||
) |
45 changes: 45 additions & 0 deletions
45
...ava/com/samuelokello/kazihub/presentation/shared/authentication/SignIn/SignInViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.samuelokello.kazihub.presentation.shared.authentication.SignIn | ||
|
||
import androidx.lifecycle.ViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
|
||
class SignInViewModel : ViewModel() { | ||
|
||
private val _state = MutableStateFlow(SignInState()) | ||
val state = _state.asStateFlow() | ||
|
||
fun onSignInResult(result: SignInResult) { | ||
_state.update { | ||
it.copy( | ||
isSignInSuccessful = result.data != null, | ||
signInError = result.errorMessage | ||
) | ||
} | ||
} | ||
|
||
fun resetState() { | ||
_state.update { SignInState() } | ||
} | ||
|
||
fun onSignInClicked() { | ||
|
||
} | ||
|
||
fun onEvent(event: SignInEvent) { | ||
when (event) { | ||
is SignInEvent.onEmailChanged -> { | ||
_state.update { | ||
it.copy(email = event.email) | ||
} | ||
} | ||
|
||
is SignInEvent.onPasswordChanged -> { | ||
_state.update { | ||
it.copy(password = event.password) | ||
} | ||
} | ||
} | ||
} | ||
} |