make browser login work again (#4704)

Yes, saving the data in the savedInstanceState is nicer... but
SharedPreferences works better so lets revert this code

closes #4702 
closes #4592
This commit is contained in:
Konrad Pozniak 2024-10-09 16:19:11 +02:00 committed by GitHub
parent 754ef647c5
commit 2a4e785bd7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 36 additions and 52 deletions

View File

@ -64,18 +64,12 @@ class LoginActivity : BaseActivity() {
private val doWebViewAuth = registerForActivityResult(OauthLogin()) { result -> private val doWebViewAuth = registerForActivityResult(OauthLogin()) { result ->
when (result) { when (result) {
is LoginResult.Ok -> lifecycleScope.launch { is LoginResult.Ok -> fetchOauthToken(result.code)
fetchOauthToken(result.code)
}
is LoginResult.Err -> displayError(result.errorMessage) is LoginResult.Err -> displayError(result.errorMessage)
is LoginResult.Cancel -> setLoading(false) is LoginResult.Cancel -> setLoading(false)
} }
} }
private var domain: String = ""
private var clientId: String = ""
private var clientSecret: String = ""
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -89,12 +83,6 @@ class LoginActivity : BaseActivity() {
binding.domainEditText.setSelection(BuildConfig.CUSTOM_INSTANCE.length) binding.domainEditText.setSelection(BuildConfig.CUSTOM_INSTANCE.length)
} }
if (savedInstanceState != null) {
domain = savedInstanceState.getString(DOMAIN, "")
clientId = savedInstanceState.getString(CLIENT_ID, "")
clientSecret = savedInstanceState.getString(CLIENT_SECRET, "")
}
if (isAccountMigration()) { if (isAccountMigration()) {
binding.domainEditText.setText(accountManager.activeAccount!!.domain) binding.domainEditText.setText(accountManager.activeAccount!!.domain)
binding.domainEditText.isEnabled = false binding.domainEditText.isEnabled = false
@ -138,18 +126,11 @@ class LoginActivity : BaseActivity() {
return super.onCreateOptionsMenu(menu) return super.onCreateOptionsMenu(menu)
} }
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString(DOMAIN, domain)
outState.putString(CLIENT_ID, clientId)
outState.putString(CLIENT_SECRET, clientSecret)
}
private fun onLoginClick(openInWebView: Boolean) { private fun onLoginClick(openInWebView: Boolean) {
binding.loginButton.isEnabled = false binding.loginButton.isEnabled = false
binding.domainTextInputLayout.error = null binding.domainTextInputLayout.error = null
domain = canonicalizeDomain(binding.domainEditText.text.toString()) val domain = canonicalizeDomain(binding.domainEditText.text.toString())
try { try {
HttpUrl.Builder().host(domain).scheme("https").build() HttpUrl.Builder().host(domain).scheme("https").build()
@ -175,9 +156,12 @@ class LoginActivity : BaseActivity() {
getString(R.string.tusky_website) getString(R.string.tusky_website)
).fold( ).fold(
{ credentials -> { credentials ->
// Save credentials. These will be put into the savedInstanceState so they get restored after activity recreation. // Save credentials so we can access them after we opened another activity for auth.
clientId = credentials.clientId preferences.edit()
clientSecret = credentials.clientSecret .putString(DOMAIN, domain)
.putString(CLIENT_ID, credentials.clientId)
.putString(CLIENT_SECRET, credentials.clientSecret)
.apply()
redirectUserToAuthorizeAndLogin(domain, credentials.clientId, openInWebView) redirectUserToAuthorizeAndLogin(domain, credentials.clientId, openInWebView)
}, },
@ -229,15 +213,8 @@ class LoginActivity : BaseActivity() {
val code = uri.getQueryParameter("code") val code = uri.getQueryParameter("code")
val error = uri.getQueryParameter("error") val error = uri.getQueryParameter("error")
/* restore variables from SharedPreferences */ if (code != null) {
val domain = preferences.getNonNullString(DOMAIN, "") fetchOauthToken(code)
val clientId = preferences.getNonNullString(CLIENT_ID, "")
val clientSecret = preferences.getNonNullString(CLIENT_SECRET, "")
if (code != null && domain.isNotEmpty() && clientId.isNotEmpty() && clientSecret.isNotEmpty()) {
lifecycleScope.launch {
fetchOauthToken(code)
}
} else { } else {
displayError(error) displayError(error)
} }
@ -262,27 +239,34 @@ class LoginActivity : BaseActivity() {
} }
} }
private suspend fun fetchOauthToken(code: String) { private fun fetchOauthToken(code: String) {
setLoading(true) setLoading(true)
mastodonApi.fetchOAuthToken( /* restore variables from SharedPreferences */
domain, val domain = preferences.getNonNullString(DOMAIN, "")
clientId, val clientId = preferences.getNonNullString(CLIENT_ID, "")
clientSecret, val clientSecret = preferences.getNonNullString(CLIENT_SECRET, "")
oauthRedirectUri,
code, lifecycleScope.launch {
"authorization_code" mastodonApi.fetchOAuthToken(
).fold( domain,
{ accessToken -> clientId,
fetchAccountDetails(accessToken, domain, clientId, clientSecret) clientSecret,
}, oauthRedirectUri,
{ e -> code,
setLoading(false) "authorization_code"
binding.domainTextInputLayout.error = ).fold(
getString(R.string.error_retrieving_oauth_token) { accessToken ->
Log.e(TAG, getString(R.string.error_retrieving_oauth_token), e) fetchAccountDetails(accessToken, domain, clientId, clientSecret)
} },
) { e ->
setLoading(false)
binding.domainTextInputLayout.error =
getString(R.string.error_retrieving_oauth_token)
Log.e(TAG, getString(R.string.error_retrieving_oauth_token), e)
}
)
}
} }
private suspend fun fetchAccountDetails( private suspend fun fetchAccountDetails(