Skip to content

Commit

Permalink
Prevent duplicate accounts from logging in (#1082)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Phoenix <[email protected]>
  • Loading branch information
MV-GH and twizmwazin authored Jul 21, 2023
1 parent 61e87a2 commit a272431
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
21 changes: 19 additions & 2 deletions app/src/main/java/com/jerboa/model/LoginViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.jerboa.getHostFromInstanceString
import com.jerboa.serializeToMap
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import java.lang.RuntimeException

class LoginViewModel : ViewModel() {

Expand Down Expand Up @@ -52,7 +53,7 @@ class LoginViewModel : ViewModel() {
R.string.login_view_model_is_not_a_lemmy_instance,
instance,
)
Log.e("login", e.toString())
Log.e("login", msg, e)
Toast.makeText(
ctx,
msg,
Expand Down Expand Up @@ -101,6 +102,17 @@ class LoginViewModel : ViewModel() {

try {
val luv = siteRes.data.my_user!!.local_user_view

if (accountViewModel.allAccounts.value?.any {
it.name.equals(
luv.person.name,
true,
) && it.instance.equals(instance, true)
} == true
) {
throw RuntimeException(ctx.getString(R.string.login_already_logged_in))
}

val account = Account(
id = luv.person.id,
name = luv.person.name,
Expand All @@ -118,7 +130,12 @@ class LoginViewModel : ViewModel() {
accountViewModel.insert(account)
} catch (e: Exception) {
loading = false
Log.e("login", e.toString())
Log.e("login", "failed", e)
Toast.makeText(
ctx,
e.message,
Toast.LENGTH_SHORT,
).show()
API.changeLemmyInstance(originalInstance)
this.cancel()
return@launch
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/jerboa/ui/components/login/Login.kt
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ fun LoginForm(
)
Button(
enabled = isValid && !loading,
onClick = { onClickLogin(form, instance) },
onClick = { onClickLogin(form, instance.lowercase()) },
modifier = Modifier.padding(top = 10.dp),
) {
if (loading) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,5 @@
<string name="exit">Exit</string>
<string name="back_dialog_title">Are you sure you want to exit?</string>
<string name="failed_saving_image">Failed to save image!</string>
<string name="login_already_logged_in">You are already logged in with this account</string>
</resources>

0 comments on commit a272431

Please sign in to comment.