Skip to content
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

make browser login work again #4704

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
},
Expand Down Expand Up @@ -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)
}
Expand All @@ -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(
Expand Down
Loading