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