Skip to content

Commit

Permalink
refactor: Enhance Social Authentication on Sign In and Sign Up Screens (
Browse files Browse the repository at this point in the history
#68)

- Update resource files
- Store last sign in type in preferences
- Fix Sign-In Success Analytics
- Improve the social auth view
- Fix Auth Test Cases
- Update doc string

Fixes: LEARNER-10256
  • Loading branch information
HamzaIsrar12 authored Nov 26, 2024
1 parent 8779f40 commit 41fd982
Show file tree
Hide file tree
Showing 19 changed files with 284 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.openedx.app.data.storage
import android.content.Context
import com.google.gson.Gson
import org.openedx.app.BuildConfig
import org.openedx.auth.data.model.AuthType
import org.openedx.core.data.model.User
import org.openedx.core.data.storage.CorePreferences
import org.openedx.core.data.storage.InAppReviewPreferences
Expand Down Expand Up @@ -167,6 +168,12 @@ class PreferencesManager(context: Context) : CorePreferences, ProfilePreferences
}
get() = getBoolean(RESET_APP_DIRECTORY, true)

override var lastSignInType: String
set(value) {
saveString(LAST_SIGN_IN_TYPE, AuthType.valueOf(value).name)
}
get() = getString(LAST_SIGN_IN_TYPE, AuthType.PASSWORD.name)

override fun setCalendarSyncEventsDialogShown(courseName: String) {
saveBoolean(courseName.replaceSpace("_"), true)
}
Expand All @@ -189,5 +196,6 @@ class PreferencesManager(context: Context) : CorePreferences, ProfilePreferences
private const val VIDEO_SETTINGS_DOWNLOAD_QUALITY = "video_settings_download_quality"
private const val APP_CONFIG = "app_config"
private const val RESET_APP_DIRECTORY = "reset_app_directory"
private const val LAST_SIGN_IN_TYPE = "last_sign_in_type"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openedx.auth.presentation.signin

import org.openedx.auth.data.model.AuthType
import org.openedx.core.domain.model.RegistrationField

/**
Expand All @@ -8,16 +9,20 @@ import org.openedx.core.domain.model.RegistrationField
* @param isFacebookAuthEnabled is Facebook auth enabled
* @param isGoogleAuthEnabled is Google auth enabled
* @param isMicrosoftAuthEnabled is Microsoft auth enabled
* @param isSocialAuthEnabled is OAuth buttons visible
* @param isSocialAuthEnabled are OAuth buttons visible
* @param isLogistrationEnabled indicates if the pre-login experience is available
* @param lastSignIn the last authentication type used
* @param showProgress is progress visible
* @param loginSuccess is login succeed
* @param loginSuccess indicates if the login was successful
* @param agreement contains the honor code with multiple hyperlinks to agreement URLs
*/
internal data class SignInUIState(
val isFacebookAuthEnabled: Boolean = false,
val isGoogleAuthEnabled: Boolean = false,
val isMicrosoftAuthEnabled: Boolean = false,
val isSocialAuthEnabled: Boolean = false,
val isLogistrationEnabled: Boolean = false,
val lastSignIn: AuthType = AuthType.PASSWORD,
val showProgress: Boolean = false,
val loginSuccess: Boolean = false,
val agreement: RegistrationField? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class SignInViewModel(
isMicrosoftAuthEnabled = config.getMicrosoftConfig().isEnabled(),
isSocialAuthEnabled = config.isSocialAuthEnabled(),
isLogistrationEnabled = config.isPreLoginExperienceEnabled(),
lastSignIn = AuthType.valueOf(preferencesManager.lastSignInType),
agreement = agreementProvider.getAgreement(isSignIn = true)?.createHonorCodeField(),
)
)
Expand Down Expand Up @@ -99,16 +100,8 @@ class SignInViewModel(
try {
interactor.login(username, password)
_uiState.update { it.copy(loginSuccess = true) }
setUserId()
logEvent(
AuthAnalyticsEvent.SIGN_IN_SUCCESS,
buildMap {
put(
AuthAnalyticsKey.METHOD.key,
AuthType.PASSWORD.methodName.lowercase()
)
}
)
setMetadata(AuthType.PASSWORD)
logSignInSuccessEvent(AuthType.PASSWORD)
appNotifier.send(SignInEvent())
} catch (e: Exception) {
if (e is EdxError.InvalidGrantException) {
Expand Down Expand Up @@ -173,8 +166,9 @@ class SignInViewModel(
}.onSuccess {
logger.d { "Social login (${authType.methodName}) success" }
_uiState.update { it.copy(loginSuccess = true) }
setUserId()
setMetadata(authType)
_uiState.update { it.copy(showProgress = false) }
logSignInSuccessEvent(authType)
appNotifier.send(SignInEvent())
}
}
Expand All @@ -189,10 +183,11 @@ class SignInViewModel(
_uiState.update { it.copy(showProgress = false) }
}

private fun setUserId() {
private fun setMetadata(authType: AuthType) {
preferencesManager.user?.let {
analytics.setUserIdForSession(it.id)
}
preferencesManager.lastSignInType = authType.name
}

private suspend fun SocialAuthResponse?.checkToken() {
Expand Down Expand Up @@ -247,6 +242,17 @@ class SignInViewModel(
)
}

private fun logSignInSuccessEvent(authType: AuthType) {
val event = AuthAnalyticsEvent.SIGN_IN_SUCCESS
analytics.logEvent(
event = event.eventName,
params = buildMap {
put(AuthAnalyticsKey.NAME.key, event.biValue)
put(AuthAnalyticsKey.METHOD.key, authType.methodName.lowercase())
}
)
}

private fun logSignInScreenEvent() {
val event = AuthAnalyticsEvent.SIGN_IN
analytics.logScreenEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,30 @@ private fun AuthForm(
var isPasswordError by rememberSaveable { mutableStateOf(false) }

Column(horizontalAlignment = Alignment.CenterHorizontally) {
if (state.isSocialAuthEnabled) {
SocialAuthView(
modifier = buttonWidth.fillMaxWidth(),
isGoogleAuthEnabled = state.isGoogleAuthEnabled,
isFacebookAuthEnabled = state.isFacebookAuthEnabled,
isMicrosoftAuthEnabled = state.isMicrosoftAuthEnabled,
lastSignIn = state.lastSignIn,
isSignIn = true,
) {
keyboardController?.hide()
onEvent(AuthEvent.SocialSignIn(it))
}
Text(
modifier = Modifier
.testTag("txt_sign_in_with_email")
.fillMaxWidth()
.padding(vertical = 16.dp),
text = stringResource(
id = R.string.auth_sign_in_with_email
),
color = MaterialTheme.appColors.textPrimary,
style = MaterialTheme.appTypography.titleSmall
)
}
LoginTextField(
modifier = Modifier
.fillMaxWidth(),
Expand Down Expand Up @@ -306,18 +330,6 @@ private fun AuthForm(
}
)
}
if (state.isSocialAuthEnabled) {
SocialAuthView(
modifier = buttonWidth,
isGoogleAuthEnabled = state.isGoogleAuthEnabled,
isFacebookAuthEnabled = state.isFacebookAuthEnabled,
isMicrosoftAuthEnabled = state.isMicrosoftAuthEnabled,
isSignIn = true,
) {
keyboardController?.hide()
onEvent(AuthEvent.SocialSignIn(it))
}
}
}
}

Expand Down Expand Up @@ -409,7 +421,12 @@ private fun SignInScreenPreview() {
OpenEdXTheme {
LoginScreen(
windowSize = WindowSize(WindowType.Compact, WindowType.Compact),
state = SignInUIState(),
state = SignInUIState().copy(
isSocialAuthEnabled = true,
isFacebookAuthEnabled = true,
isGoogleAuthEnabled = true,
isMicrosoftAuthEnabled = true,
),
uiMessage = null,
onEvent = {},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
Expand Down Expand Up @@ -346,6 +347,31 @@ internal fun SignUpView(
color = MaterialTheme.appColors.textPrimary,
style = MaterialTheme.appTypography.titleSmall
)
if (uiState.isSocialAuthEnabled) {
Spacer(modifier = Modifier.height(24.dp))
SocialAuthView(
modifier = buttonWidth,
isGoogleAuthEnabled = uiState.isGoogleAuthEnabled,
isFacebookAuthEnabled = uiState.isFacebookAuthEnabled,
isMicrosoftAuthEnabled = uiState.isMicrosoftAuthEnabled,
isSignIn = false,
) {
keyboardController?.hide()
onRegisterClick(it)
}
Text(
modifier = Modifier
.testTag("txt_register_below")
.padding(top = 8.dp)
.offset(y = 8.dp)
.fillMaxWidth(),
text = stringResource(
id = R.string.auth_register_below
),
color = MaterialTheme.appColors.textPrimary,
style = MaterialTheme.appTypography.titleSmall
)
}
}
}
RequiredFields(
Expand Down Expand Up @@ -446,18 +472,6 @@ internal fun SignUpView(
}
)
}
if (uiState.isSocialAuthEnabled && uiState.socialAuth == null) {
SocialAuthView(
modifier = buttonWidth,
isGoogleAuthEnabled = uiState.isGoogleAuthEnabled,
isFacebookAuthEnabled = uiState.isFacebookAuthEnabled,
isMicrosoftAuthEnabled = uiState.isMicrosoftAuthEnabled,
isSignIn = false,
) {
keyboardController?.hide()
onRegisterClick(it)
}
}
Spacer(Modifier.height(70.dp))
}
}
Expand Down
Loading

0 comments on commit 41fd982

Please sign in to comment.