-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Support for login and registration via a browser custom tab #371
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package org.openedx.app | |
import android.content.Intent | ||
import android.content.res.Configuration | ||
import android.graphics.Color | ||
import android.net.Uri | ||
import android.os.Bundle | ||
import android.view.View | ||
import android.view.WindowManager | ||
|
@@ -64,6 +65,14 @@ class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder { | |
private var _insetCutout = 0 | ||
|
||
private var _windowSize = WindowSize(WindowType.Compact, WindowType.Compact) | ||
private val authCode: String? | ||
get() { | ||
val data = intent?.data | ||
if (data is Uri && data.scheme == BuildConfig.APPLICATION_ID && data.host == "oauth2Callback") { | ||
return data.getQueryParameter("code") | ||
} | ||
return null | ||
} | ||
|
||
private val branchCallback = | ||
BranchUniversalReferralInitListener { branchUniversalObject, _, error -> | ||
|
@@ -141,10 +150,15 @@ class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder { | |
if (savedInstanceState == null) { | ||
when { | ||
corePreferencesManager.user == null -> { | ||
if (viewModel.isLogistrationEnabled) { | ||
val authCode = authCode; | ||
if (viewModel.isLogistrationEnabled && authCode == null) { | ||
addFragment(LogistrationFragment()) | ||
} else { | ||
addFragment(SignInFragment()) | ||
val bundle = Bundle() | ||
bundle.putString("auth_code", authCode) | ||
val fragment = SignInFragment() | ||
fragment.arguments = bundle | ||
addFragment(fragment) | ||
Comment on lines
+157
to
+161
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,15 @@ class AuthRepository( | |
.processAuthResponse() | ||
} | ||
|
||
suspend fun browserAuthCodeLogin(code: String) { | ||
api.getAccessTokenFromCode( | ||
grantType = ApiConstants.GRANT_TYPE_CODE, | ||
clientId = config.getOAuthClientId(), | ||
code = code, | ||
redirectUri = "${config.getApplicationID()}://oauth2Callback" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
).mapToDomain().processAuthResponse() | ||
} | ||
|
||
suspend fun getRegistrationFields(): List<RegistrationField> { | ||
return api.getRegistrationFields().fields?.map { it.mapToDomain() } ?: emptyList() | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ import org.openedx.core.ui.theme.OpenEdXTheme | |
import org.openedx.core.ui.theme.appColors | ||
import org.openedx.core.ui.theme.appTypography | ||
import org.openedx.core.ui.theme.compose.LogistrationLogoView | ||
import org.openedx.foundation.utils.UrlUtils | ||
|
||
class LogistrationFragment : Fragment() { | ||
|
||
|
@@ -67,10 +68,23 @@ class LogistrationFragment : Fragment() { | |
OpenEdXTheme { | ||
LogistrationScreen( | ||
onSignInClick = { | ||
viewModel.navigateToSignIn(parentFragmentManager) | ||
if(viewModel.isBrowserLoginEnabled) { | ||
viewModel.signInBrowser(requireActivity()) | ||
} else { | ||
viewModel.navigateToSignIn(parentFragmentManager) | ||
} | ||
|
||
}, | ||
onRegisterClick = { | ||
viewModel.navigateToSignUp(parentFragmentManager) | ||
if (viewModel.isBrowserRegistrationEnabled) { | ||
UrlUtils.openInBrowser( | ||
activity = context, | ||
apiHostUrl = viewModel.apiHostUrl, | ||
url = "/register", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move |
||
) | ||
} else { | ||
viewModel.navigateToSignUp(parentFragmentManager) | ||
} | ||
}, | ||
onSearchClick = { querySearch -> | ||
viewModel.navigateToDiscovery(parentFragmentManager, querySearch) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,11 @@ class SignInFragment : Fragment() { | |
val appUpgradeEvent by viewModel.appUpgradeEvent.observeAsState(null) | ||
|
||
if (appUpgradeEvent == null) { | ||
val authCode = arguments?.getString("auth_code") | ||
if (authCode is String && !state.loginFailure && !state.loginSuccess) { | ||
arguments?.remove("auth_code") | ||
Comment on lines
+46
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move arguments like
|
||
viewModel.signInAuthCode(authCode) | ||
} | ||
LoginScreen( | ||
windowSize = windowSize, | ||
state = state, | ||
|
@@ -59,6 +64,10 @@ class SignInFragment : Fragment() { | |
viewModel.navigateToForgotPassword(parentFragmentManager) | ||
} | ||
|
||
AuthEvent.SignInBrowser -> { | ||
viewModel.signInBrowser(requireActivity()) | ||
} | ||
|
||
AuthEvent.RegisterClick -> { | ||
viewModel.navigateToSignUp(parentFragmentManager) | ||
} | ||
|
@@ -107,6 +116,7 @@ internal sealed interface AuthEvent { | |
data class SignIn(val login: String, val password: String) : AuthEvent | ||
data class SocialSignIn(val authType: AuthType) : AuthEvent | ||
data class OpenLink(val links: Map<String, String>, val link: String) : AuthEvent | ||
object SignInBrowser : AuthEvent | ||
object RegisterClick : AuthEvent | ||
object ForgotPasswordClick : AuthEvent | ||
object BackClick : AuthEvent | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package org.openedx.auth.presentation.signin | ||
|
||
import android.app.Activity | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentManager | ||
import androidx.lifecycle.LiveData | ||
|
@@ -17,6 +18,7 @@ import org.openedx.auth.domain.interactor.AuthInteractor | |
import org.openedx.auth.domain.model.SocialAuthResponse | ||
import org.openedx.auth.presentation.AgreementProvider | ||
import org.openedx.auth.presentation.AuthAnalytics | ||
import org.openedx.auth.presentation.sso.BrowserAuthHelper | ||
import org.openedx.auth.presentation.AuthAnalyticsEvent | ||
import org.openedx.auth.presentation.AuthAnalyticsKey | ||
import org.openedx.auth.presentation.AuthRouter | ||
|
@@ -53,7 +55,8 @@ class SignInViewModel( | |
private val calendarPreferences: CalendarPreferences, | ||
private val calendarInteractor: CalendarInteractor, | ||
agreementProvider: AgreementProvider, | ||
config: Config, | ||
private val browserAuthHelper: BrowserAuthHelper, | ||
val config: Config, | ||
val courseId: String?, | ||
val infoType: String?, | ||
) : BaseViewModel() { | ||
|
@@ -65,6 +68,8 @@ class SignInViewModel( | |
isFacebookAuthEnabled = config.getFacebookConfig().isEnabled(), | ||
isGoogleAuthEnabled = config.getGoogleConfig().isEnabled(), | ||
isMicrosoftAuthEnabled = config.getMicrosoftConfig().isEnabled(), | ||
isBrowserLoginEnabled = config.isBrowserLoginEnabled(), | ||
isBrowserRegistrationEnabled = config.isBrowserRegistrationEnabled(), | ||
isSocialAuthEnabled = config.isSocialAuthEnabled(), | ||
isLogistrationEnabled = config.isPreLoginExperienceEnabled(), | ||
isRegistrationEnabled = config.isRegistrationEnabled(), | ||
|
@@ -158,11 +163,41 @@ class SignInViewModel( | |
} | ||
} | ||
|
||
fun signInBrowser(activityContext: Activity) { | ||
_uiState.update { it.copy(showProgress = true) } | ||
viewModelScope.launch { | ||
runCatching { | ||
browserAuthHelper.signIn(activityContext) | ||
}.onFailure { | ||
logger.e { "Browser auth error: $it" } | ||
} | ||
} | ||
} | ||
|
||
fun navigateToSignUp(parentFragmentManager: FragmentManager) { | ||
router.navigateToSignUp(parentFragmentManager, null, null) | ||
logEvent(AuthAnalyticsEvent.REGISTER_CLICKED) | ||
} | ||
|
||
fun signInAuthCode(authCode: String) { | ||
_uiState.update { it.copy(showProgress = true) } | ||
viewModelScope.launch { | ||
runCatching { | ||
interactor.loginAuthCode(authCode) | ||
} | ||
.onFailure { | ||
logger.e { "OAuth2 code error: $it" } | ||
onUnknownError() | ||
_uiState.update { it.copy(loginFailure = true) } | ||
}.onSuccess { | ||
logger.d { "Browser login success" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove the debug log from here |
||
_uiState.update { it.copy(loginSuccess = true) } | ||
setUserId() | ||
_uiState.update { it.copy(showProgress = false) } | ||
} | ||
} | ||
} | ||
|
||
fun navigateToForgotPassword(parentFragmentManager: FragmentManager) { | ||
router.navigateToRestorePassword(parentFragmentManager) | ||
logEvent(AuthAnalyticsEvent.FORGOT_PASSWORD_CLICKED) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move
"oauth2Callback"
and"code"
to constants.