mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
feat: username recovery on login post
This commit is contained in:
+25
-19
@@ -10,9 +10,28 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var contentTypes = map[string]string{
|
||||
".css": "text/css",
|
||||
".js": "application/javascript",
|
||||
".png": "image/png",
|
||||
".jpg": "image/jpeg",
|
||||
".webp": "image/webp",
|
||||
".svg": "image/svg+xml",
|
||||
".ico": "image/x-icon",
|
||||
}
|
||||
|
||||
type LoginPage struct {
|
||||
IsError bool `json:"isError"`
|
||||
Error string `json:"error"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
func NewLoginPage(error string, username string) *LoginPage {
|
||||
return &LoginPage{
|
||||
IsError: strings.Compare(error, "") != 0,
|
||||
Error: error,
|
||||
Username: username,
|
||||
}
|
||||
}
|
||||
|
||||
func getPage(page string, data interface{}) ([]byte, error) {
|
||||
@@ -28,7 +47,7 @@ func getPage(page string, data interface{}) ([]byte, error) {
|
||||
return processedHTML.Bytes(), nil
|
||||
}
|
||||
|
||||
func getLoginPageHandler(errMsg string) http.HandlerFunc {
|
||||
func getLoginPageHandler(loginPage *LoginPage) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if IsLoggedIn(r) {
|
||||
http.Redirect(w, r, "/home", http.StatusSeeOther)
|
||||
@@ -38,10 +57,7 @@ func getLoginPageHandler(errMsg string) http.HandlerFunc {
|
||||
postLoginPage(w, r)
|
||||
return
|
||||
}
|
||||
bs, err := getPage("login", &LoginPage{
|
||||
IsError: strings.Compare(errMsg, "") != 0,
|
||||
Error: errMsg,
|
||||
})
|
||||
bs, err := getPage("login", &loginPage)
|
||||
if err != nil {
|
||||
_, _ = w.Write([]byte(err.Error()))
|
||||
}
|
||||
@@ -58,7 +74,7 @@ func postLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
if err != nil {
|
||||
r.Method = http.MethodGet
|
||||
getLoginPageHandler("Missing username or password.")(w, r)
|
||||
getLoginPageHandler(NewLoginPage("Invalid username or password.", r.FormValue("username")))(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -67,7 +83,7 @@ func postLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO handle possible errors in separate file (api package)
|
||||
if err != nil || response.StatusCode != http.StatusOK {
|
||||
r.Method = http.MethodGet
|
||||
getLoginPageHandler("Invalid username or password.")(w, r)
|
||||
getLoginPageHandler(NewLoginPage("Invalid username or password.", r.FormValue("username")))(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -104,16 +120,6 @@ func IsLoggedInMiddleware(next http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
func staticFileServerWithContentType(dir http.Dir) http.Handler {
|
||||
contentTypes := map[string]string{
|
||||
".css": "text/css",
|
||||
".js": "application/javascript",
|
||||
".png": "image/png",
|
||||
".jpg": "image/jpeg",
|
||||
".webp": "image/webp",
|
||||
".svg": "image/svg+xml",
|
||||
".ico": "image/x-icon",
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requestedFilePath := r.URL.Path
|
||||
fileExtension := filepath.Ext(requestedFilePath)
|
||||
@@ -135,8 +141,8 @@ func NewPageRouter() http.Handler {
|
||||
})
|
||||
|
||||
r.Get("/", getLogin)
|
||||
r.Get("/login", getLoginPageHandler(""))
|
||||
r.Post("/login", getLoginPageHandler(""))
|
||||
r.Get("/login", getLoginPageHandler(NewLoginPage("", "")))
|
||||
r.Post("/login", getLoginPageHandler(NewLoginPage("", "")))
|
||||
r.Get("/logout", getLogout)
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
|
||||
+32
-16
@@ -3,7 +3,7 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||||
<title>GohCMS | Login</title>
|
||||
<title>GohCMS | Connexion</title>
|
||||
|
||||
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
|
||||
<script src="/static/bootstrap.min.js"
|
||||
@@ -16,8 +16,7 @@
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body class="d-flex min-vh-100 vw-100 justify-content-center align-items-center">
|
||||
|
||||
<body class="d-flex min-vh-100 vw-100 justify-content-center align-items-center text-dark">
|
||||
<div class="container">
|
||||
<div class="d-flex flex-column gap-5 m-auto form-container">
|
||||
<h1>GohCMS</h1>
|
||||
@@ -27,31 +26,34 @@
|
||||
<input class="form-control {{ if .IsError }} is-invalid {{ end }}"
|
||||
id="username"
|
||||
name="username"
|
||||
placeholder="Votre nom d'utilisateur"
|
||||
placeholder="Nom d'utilisateur"
|
||||
required
|
||||
type="text">
|
||||
<label for="username">Username</label>
|
||||
type="text"
|
||||
value="{{.Username}}">
|
||||
<label for="username">Nom d'utilisateur</label>
|
||||
</div>
|
||||
<div class="form-floating">
|
||||
<input class="form-control {{ if .IsError }} is-invalid {{ end }}"
|
||||
id="password"
|
||||
name="password"
|
||||
placeholder="Votre mot de passe"
|
||||
placeholder="Mot de passe"
|
||||
required
|
||||
type="password">
|
||||
<label for="password">Password</label>
|
||||
<div class="invalid-feedback">
|
||||
{{.Error}}
|
||||
<label for="password">Mot de passe</label>
|
||||
<div class="invalid-feedback">{{.Error}}</div>
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn btn-primary" disabled type="submit">Log in</button>
|
||||
<button id="loginFormButton" class="btn btn-primary" disabled type="submit">
|
||||
<span class="loginFormButtonLoading visually-hidden spinner-border spinner-border-sm"
|
||||
role="status"></span>
|
||||
<span class="loginFormButtonDefault">Se connecter</span>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const button = document.querySelector("button[type=submit]")
|
||||
const button = document.querySelector("#loginFormButton")
|
||||
const inputs = document.querySelectorAll('input')
|
||||
|
||||
function formFieldsEmpty() {
|
||||
@@ -62,10 +64,24 @@
|
||||
button.disabled = formFieldsEmpty() ? "disabled" : ""
|
||||
}
|
||||
|
||||
window.addEventListener('input', (event) => {
|
||||
if (event.target.tagName === "INPUT") setButtonDisabled()
|
||||
})
|
||||
function setButtonLoading() {
|
||||
button.classList.add("disabled")
|
||||
button.querySelector(".loginFormButtonDefault").classList.add("visually-hidden")
|
||||
button.querySelector(".loginFormButtonLoading").classList.remove("visually-hidden")
|
||||
}
|
||||
|
||||
function onLoginFormSubmit(event) {
|
||||
event.preventDefault()
|
||||
setButtonLoading()
|
||||
event.target.submit()
|
||||
}
|
||||
|
||||
function onInput(event) {
|
||||
if (event.target.tagName === "INPUT") setButtonDisabled()
|
||||
}
|
||||
|
||||
window.addEventListener('submit', onLoginFormSubmit)
|
||||
window.addEventListener('input', onInput)
|
||||
setButtonDisabled()
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user