Accounts can now be deleted
This commit is contained in:
@@ -6,10 +6,12 @@ import be.simplenotes.app.extensions.redirect
|
||||
import be.simplenotes.app.views.UserView
|
||||
import be.simplenotes.domain.security.JwtPayload
|
||||
import be.simplenotes.domain.usecases.UserService
|
||||
import be.simplenotes.domain.usecases.login.*
|
||||
import be.simplenotes.domain.usecases.register.InvalidRegisterForm
|
||||
import be.simplenotes.domain.usecases.register.RegisterForm
|
||||
import be.simplenotes.domain.usecases.register.UserExists
|
||||
import be.simplenotes.domain.usecases.users.delete.DeleteError
|
||||
import be.simplenotes.domain.usecases.users.delete.DeleteForm
|
||||
import be.simplenotes.domain.usecases.users.login.*
|
||||
import be.simplenotes.domain.usecases.users.register.InvalidRegisterForm
|
||||
import be.simplenotes.domain.usecases.users.register.RegisterForm
|
||||
import be.simplenotes.domain.usecases.users.register.UserExists
|
||||
import be.simplenotes.shared.config.JwtConfig
|
||||
import org.http4k.core.Method.GET
|
||||
import org.http4k.core.Request
|
||||
@@ -110,4 +112,38 @@ class UserController(
|
||||
|
||||
fun logout(@Suppress("UNUSED_PARAMETER") request: Request) = Response.redirect("/")
|
||||
.invalidateCookie("Authorization")
|
||||
|
||||
private fun Request.deleteForm(jwtPayload: JwtPayload) =
|
||||
DeleteForm(jwtPayload.username, form("password"), form("checked") != null)
|
||||
|
||||
fun settings(request: Request, jwtPayload: JwtPayload): Response {
|
||||
if (request.method == GET)
|
||||
return Response(OK).html(userView.settings(jwtPayload))
|
||||
|
||||
val deleteForm = request.deleteForm(jwtPayload)
|
||||
val result = userService.delete(deleteForm)
|
||||
|
||||
return result.fold(
|
||||
{
|
||||
when (it) {
|
||||
DeleteError.Unregistered -> Response.redirect("/").invalidateCookie("Authorization")
|
||||
DeleteError.WrongPassword -> Response(OK).html(
|
||||
userView.settings(
|
||||
jwtPayload,
|
||||
error = "Wrong password"
|
||||
)
|
||||
)
|
||||
is DeleteError.InvalidForm -> Response(OK).html(
|
||||
userView.settings(
|
||||
jwtPayload,
|
||||
validationErrors = it.validationErrors
|
||||
)
|
||||
)
|
||||
}
|
||||
},
|
||||
{
|
||||
Response.redirect("/").invalidateCookie("Authorization")
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user