Skip to content
This repository was archived by the owner on Sep 22, 2024. It is now read-only.
Draft
Show file tree
Hide file tree
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
32 changes: 23 additions & 9 deletions src/main/xerus/monstercat/api/APIConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import java.io.IOException
import java.io.InputStream
import java.lang.ref.WeakReference
import java.net.URI
import java.net.UnknownHostException
import kotlin.math.min
import kotlin.reflect.KClass

Expand Down Expand Up @@ -226,20 +227,33 @@ class APIConnection(vararg path: String): HTTPQuery<APIConnection>() {
return ConnectResult(connectsid, validity, session)
}

fun login(username: String, password: String): Boolean {
enum class LoginStatus{
CONNECTED,
INVALID,
NOINTERNET,
UNKNOWNERROR
}

fun login(username: String, password: String): LoginStatus {
val connection = APIConnection("v2", "signin")
val context = HttpClientContext()
connection.execute(HttpPost(connection.uri).apply {
setHeader("Accept", "application/json")
setHeader("Content-type", "application/json")
entity = StringEntity("""{"email":"$username","password":"$password"}""")
}, context)
try {
connection.execute(HttpPost(connection.uri).apply {
setHeader("Accept", "application/json")
setHeader("Content-type", "application/json")
entity = StringEntity("""{"email":"$username","password":"$password"}""")
}, context)
} catch(e: Exception) {
logger.warn("Error when trying to login through API", e)
return if(e is UnknownHostException) LoginStatus.NOINTERNET
else LoginStatus.UNKNOWNERROR
}

val code = connection.response?.statusLine?.statusCode
logger.trace("Login POST returned response code $code")
if (code !in 200..206) return false
CONNECTSID.value = (context.cookieStore.cookies.find { it.name == "connect.sid" }?.value ?: return false)
return true
if (code !in 200..206) return LoginStatus.INVALID
CONNECTSID.value = (context.cookieStore.cookies.find { it.name == "connect.sid" }?.value ?: return LoginStatus.INVALID)
return LoginStatus.CONNECTED
}

fun logout() {
Expand Down
16 changes: 11 additions & 5 deletions src/main/xerus/monstercat/downloader/TabDownloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import xerus.ktutil.javafx.ui.controls.alwaysTruePredicate
import xerus.ktutil.javafx.ui.initWindowOwner
import xerus.ktutil.preferences.PropertySetting
import xerus.monstercat.api.APIConnection
import xerus.monstercat.api.APIConnection.Companion.LoginStatus.*
import xerus.monstercat.api.ConnectValidity
import xerus.monstercat.api.Covers
import xerus.monstercat.api.response.ArtistRel
Expand Down Expand Up @@ -419,7 +420,7 @@ class TabDownloader: VTab() {
val emailField = TextField("").apply { promptText = "Email address" }
val passwordField = PasswordField().apply { promptText = "Password" }

val errorMessage = Label("Wrong username / password").apply { isVisible = false; managedProperty().bind(visibleProperty()) }
val errorMessage = Label("An error has been encountered").apply { isVisible = false; managedProperty().bind(visibleProperty()) }
val loadingGif = ImageView(Image("img/loading-16.gif")).apply { isVisible = false }

val buttonHBox = HBox()
Expand All @@ -437,15 +438,20 @@ class TabDownloader: VTab() {
val login = APIConnection.login(emailField.text, passwordField.text)
logger.debug("Login as ${emailField.text} returned $login")
onFx {
if(login) {
stage.close()
} else {
fun showErrorText(message: String) {
errorMessage.text = message
errorMessage.isVisible = true
loadingGif.isVisible = false
buttonHBox.isDisable = false

stage.sizeToScene()
}

when(login) {
CONNECTED -> stage.close()
INVALID -> showErrorText("Wrong username / password")
NOINTERNET -> showErrorText("You are not connected to the Internet")
UNKNOWNERROR -> showErrorText("An error has occured.")
}
}
}
}
Expand Down