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.
145 lines
4.4 KiB
145 lines
4.4 KiB
package wow.doge.chatto.controller
|
|
|
|
import com.typesafe.scalalogging.LazyLogging
|
|
import com.sfxcode.sapphire.core.controller.ViewController
|
|
import javafx.fxml.FXML
|
|
import com.jfoenix.controls.JFXButton
|
|
import com.jfoenix.controls.JFXTextField
|
|
import com.jfoenix.controls.JFXPasswordField
|
|
import scalafx.Includes._
|
|
// import scalafx.application.Platform
|
|
import scalafx.event.ActionEvent
|
|
import com.sfxcode.sapphire.core.value.KeyBindings
|
|
import scalafx.scene.layout.VBox
|
|
import com.sfxcode.sapphire.core.value.FXBean
|
|
import javax.inject.Inject
|
|
import com.sfxcode.sapphire.core.value.FXBeanAdapter
|
|
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.AppData
|
|
import wow.doge.chatto.UserCredentials
|
|
import sttp.client._
|
|
import scala.concurrent.Future
|
|
import sttp.client.asynchttpclient.WebSocketHandler
|
|
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
|
|
// usernameTextField.requestFocus()
|
|
|
|
// submitButton.setOnAction(actionLogin)
|
|
|
|
val bindings = KeyBindings("username", "password")
|
|
// Expression Binding Example
|
|
// bindings.add(
|
|
// "usernameTextField",
|
|
// "${sf:i18n('personText', _self.usernameTextField(), _self.passwordTextField())})"
|
|
// )
|
|
|
|
val box = new VBox()
|
|
val adapter = FXBeanAdapter[Person](this)
|
|
val bean = FXBean[Person](Person("0", "username", "password"))
|
|
adapter.addBindings(bindings)
|
|
adapter.set(bean)
|
|
// adapter.addIntConverter("age")
|
|
// adapter.hasBeanProperty
|
|
// adapter.revert()
|
|
|
|
Array(usernameTextField, passwordTextField, submitButton)
|
|
.foreach(_.onKeyPressed = (keyEvent) => {
|
|
if (keyEvent.getCode() == KeyCode.ENTER) submitButton.fire()
|
|
})
|
|
|
|
}
|
|
|
|
override def didGainVisibility(): Unit = {
|
|
usernameTextField.requestFocus()
|
|
}
|
|
|
|
def actionLogin() = {
|
|
val inputUserName = usernameTextField.text()
|
|
val inputPassword = passwordTextField.text()
|
|
|
|
login(inputUserName, inputPassword) onComplete {
|
|
case Success(maybeToken) => {
|
|
maybeToken.foreach(println)
|
|
maybeToken match {
|
|
case Some(token) =>
|
|
async {
|
|
val credentials =
|
|
UserCredentials(inputUserName, inputPassword, token)
|
|
appDataHandler.updateCredentials(credentials)
|
|
// await {
|
|
// userService.getUsers(credentials).map(_.body.foreach(println))
|
|
// }
|
|
applicationController.showChatPane()
|
|
}
|
|
case None => {
|
|
updateErrorLabel("Error logging in - please check your password")
|
|
logger.warn("Login unsuccessful wrong password")
|
|
}
|
|
}
|
|
// applicationController.showChatPane()
|
|
|
|
}
|
|
case Failure(exception) => {
|
|
logger.error(s"${exception.getMessage()}")
|
|
logger.warn("Login unsuccessful network problem")
|
|
updateErrorLabel("Error logging in - Please check your network")
|
|
// applicationController.showChatPane()
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
)
|
|
|
|
}
|