WIP desktop client for Chatto reimplemented in ScalaFX and Sapphire Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

115 lines
3.3 KiB

package wow.doge.chatto.controller
import com.typesafe.scalalogging.LazyLogging
import javafx.fxml.FXML
import com.jfoenix.controls.JFXButton
import com.jfoenix.controls.JFXTextField
import com.jfoenix.controls.JFXPasswordField
import scalafx.Includes._
import javax.inject.Inject
import wow.doge.chatto.service.UserService
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Success
import scala.util.Failure
import javafx.scene.control.Label
import javafx.scene.input.KeyCode
import scala.async.Async.{async, await}
import wow.doge.chatto.UserCredentials
import sttp.client._
import wow.doge.chatto.types.AppTypes.HttpBackend
import wow.doge.chatto.types.AppTypes
import org.scalafx.extras._
import wow.doge.chatto.AppDataHandler
import com.sfxcode.sapphire.core.value.BeanConversions
class LoginController @Inject() (
userService: UserService,
appDataHandler: AppDataHandler
)(
implicit backend: HttpBackend
) extends AbstractViewController
with LazyLogging
with AppTypes
with BeanConversions {
@FXML var submitButton: JFXButton = _
@FXML private var usernameTextField: JFXTextField = _
@FXML private var passwordTextField: JFXPasswordField = _
@FXML private var errorLabel: Label = _
override def didGainVisibilityFirstTime(): Unit = {
super.didGainVisibilityFirstTime()
this.stage.resizable = false
Array(usernameTextField, passwordTextField, submitButton)
.foreach(_.onKeyPressed = (keyEvent) => {
if (keyEvent.getCode() == KeyCode.ENTER) submitButton.fire()
})
}
override def didGainVisibility(): Unit = {
usernameTextField.requestFocus()
usernameTextField.text() = "username"
passwordTextField.text() = "password"
}
def actionLogin() = {
val inputUserName = usernameTextField.text()
val inputPassword = passwordTextField.text()
submitButton.disable = true
login(inputUserName, inputPassword) andThen {
case Success(maybeToken) => {
maybeToken match {
case Some(token) =>
async {
val credentials =
UserCredentials(inputUserName, token)
appDataHandler.updateCredentials(credentials)
updateErrorLabel("")
applicationController.showChatPane()
}
case None => {
updateErrorLabel("Error logging in - please check your password")
logger.warn("Login unsuccessful wrong password")
}
}
}
case Failure(exception) => {
logger.error(s"${exception.getMessage()}")
logger.warn("Login unsuccessful network problem")
updateErrorLabel("Error logging in - Please check your network")
}
} andThen {
case _ => { onFX { submitButton.disable = false } }
}
}
def updateErrorLabel(message: String) = onFX {
errorLabel.text = message
}
def login(username: String, password: String) = async {
val resp = await(initLogin(username, password))
resp.header("X-AUTH-TOKEN")
}
def initLogin(username: String, password: String) = {
basicRequest.auth
.basic(username, password)
.get(uri"http://localhost:8080/api/chat/get/token")
.send()
}
final case class Result(username: String, password: String)
final case class Person(
id: String,
username: String,
password: String
)
}