85 lines
3.2 KiB
Kotlin
85 lines
3.2 KiB
Kotlin
package be.simplenotes.app.views
|
|
|
|
import be.simplenotes.app.utils.StaticFileResolver
|
|
import be.simplenotes.app.views.components.Alert
|
|
import be.simplenotes.app.views.components.alert
|
|
import be.simplenotes.app.views.components.input
|
|
import be.simplenotes.app.views.components.submitButton
|
|
import be.simplenotes.domain.security.JwtPayload
|
|
import io.konform.validation.ValidationError
|
|
import kotlinx.html.*
|
|
|
|
class UserView(staticFileResolver: StaticFileResolver) : View(staticFileResolver) {
|
|
fun register(
|
|
jwtPayload: JwtPayload?,
|
|
error: String? = null,
|
|
validationErrors: List<ValidationError> = emptyList(),
|
|
) = accountForm(
|
|
"Register",
|
|
"Registration page",
|
|
jwtPayload,
|
|
error,
|
|
validationErrors,
|
|
"Create an account",
|
|
"Register"
|
|
) {
|
|
+"Already have an account? "
|
|
a(href = "/login", classes = "no-underline text-blue-500 hover:text-blue-400 font-bold") { +"Sign In" }
|
|
}
|
|
|
|
fun login(
|
|
jwtPayload: JwtPayload?,
|
|
error: String? = null,
|
|
validationErrors: List<ValidationError> = emptyList(),
|
|
new: Boolean = false,
|
|
) = accountForm("Login", "Login page", jwtPayload, error, validationErrors, "Sign In", "Sign In", new) {
|
|
+"Don't have an account yet? "
|
|
a(href = "/register", classes = "no-underline text-blue-500 hover:text-blue-400 font-bold") {
|
|
+"Create an account"
|
|
}
|
|
}
|
|
|
|
private fun accountForm(
|
|
title: String,
|
|
description: String,
|
|
jwtPayload: JwtPayload?,
|
|
error: String? = null,
|
|
validationErrors: List<ValidationError> = emptyList(),
|
|
h1: String,
|
|
submit: String,
|
|
new: Boolean = false,
|
|
footer: FlowContent.() -> Unit,
|
|
) = renderPage(title = title, description, jwtPayload = jwtPayload) {
|
|
div("centered container mx-auto flex justify-center items-center") {
|
|
div("w-full md:w-1/2 lg:w-1/3 m-4") {
|
|
div("p-8 mb-6") {
|
|
h1("font-semibold text-lg mb-6 text-center") { +h1 }
|
|
if (new) alert(Alert.Success, "Your account has been created")
|
|
error?.let { alert(Alert.Warning, error) }
|
|
form(method = FormMethod.post) {
|
|
input(
|
|
id = "username",
|
|
placeholder = "Username",
|
|
autoComplete = "username",
|
|
error = validationErrors.find { it.dataPath == ".username" }?.message
|
|
)
|
|
input(
|
|
id = "password",
|
|
placeholder = "Password",
|
|
autoComplete = "new-password",
|
|
type = InputType.password,
|
|
error = validationErrors.find { it.dataPath == ".password" }?.message
|
|
)
|
|
submitButton(submit)
|
|
}
|
|
}
|
|
div("text-center") {
|
|
p("text-gray-200 text-sm") {
|
|
footer()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|