feat: login/registration messages
This commit is contained in:
parent
9bf854db1a
commit
4cfa05d9fd
|
|
@ -0,0 +1,28 @@
|
||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"lishwist/templates"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LoginGetProps struct {
|
||||||
|
SuccessfulRegistration bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (auth *AuthMiddleware) Login(w http.ResponseWriter, r *http.Request) {
|
||||||
|
session, _ := auth.Store.Get(r, "lishwist_user")
|
||||||
|
successfulReg, ok := session.Values["successful_registration"].(bool)
|
||||||
|
if ok {
|
||||||
|
delete(session.Values, "successful_registration")
|
||||||
|
if err := session.Save(r, w); err != nil {
|
||||||
|
log.Println("Couldn't save session:", err)
|
||||||
|
http.Error(w, "Something went wrong. Error code: Zuko", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
templates.Execute(w, "login.gotmpl", LoginGetProps{
|
||||||
|
SuccessfulRegistration: successfulReg,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"lishwist/db"
|
"lishwist/db"
|
||||||
|
|
@ -41,5 +42,13 @@ func (auth *AuthMiddleware) RegisterPost(w http.ResponseWriter, r *http.Request)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
session, _ := auth.Store.Get(r, "lishwist_user")
|
||||||
|
session.Values["successful_registration"] = true
|
||||||
|
if err := session.Save(r, w); err != nil {
|
||||||
|
log.Println("Couldn't save session:", err)
|
||||||
|
http.Error(w, "Something went wrong. Error code: Zuko", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
main.go
2
main.go
|
|
@ -32,7 +32,7 @@ func main() {
|
||||||
|
|
||||||
publicMux.HandleFunc("GET /register", templates.Register)
|
publicMux.HandleFunc("GET /register", templates.Register)
|
||||||
publicMux.HandleFunc("POST /register", authMiddleware.RegisterPost)
|
publicMux.HandleFunc("POST /register", authMiddleware.RegisterPost)
|
||||||
publicMux.HandleFunc("GET /", templates.Login)
|
publicMux.HandleFunc("GET /", authMiddleware.Login)
|
||||||
publicMux.HandleFunc("POST /", authMiddleware.LoginPost)
|
publicMux.HandleFunc("POST /", authMiddleware.LoginPost)
|
||||||
|
|
||||||
protectedMux.HandleFunc("GET /", ctx.Home)
|
protectedMux.HandleFunc("GET /", ctx.Home)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
package templates
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Login(w http.ResponseWriter, r *http.Request) {
|
|
||||||
Execute(w, "login.gotmpl", nil)
|
|
||||||
}
|
|
||||||
|
|
@ -2,7 +2,12 @@
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{define "body"}}
|
{{define "body"}}
|
||||||
<div class="flex-grow-1 d-flex justify-content-center align-items-center">
|
<div class="container d-flex flex-grow-1 justify-content-center align-items-center flex-column">
|
||||||
|
{{if .SuccessfulRegistration}}
|
||||||
|
<div class="alert alert-success" role="alert">
|
||||||
|
<p class="mb-0">Registration successful. Now you can login.</p>
|
||||||
|
</div>
|
||||||
|
{{end}}
|
||||||
<form method="post">
|
<form method="post">
|
||||||
<div class="d-flex flex-column gap-3">
|
<div class="d-flex flex-column gap-3">
|
||||||
<label>
|
<label>
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,11 @@
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{define "body"}}
|
{{define "body"}}
|
||||||
<div class="flex-grow-1 d-flex justify-content-center align-items-center">
|
<div class="container d-flex flex-grow-1 justify-content-center align-items-center flex-column">
|
||||||
|
<div class="alert alert-warning" role="alert">
|
||||||
|
<p>Your password will be stored in a safe, responsible manner; but don't trust my programming skills!</p>
|
||||||
|
<p class="mb-0">Maybe use a password here that you don't use for important things...</p>
|
||||||
|
</div>
|
||||||
<form method="post">
|
<form method="post">
|
||||||
<div class="d-flex flex-column gap-3">
|
<div class="d-flex flex-column gap-3">
|
||||||
<label>
|
<label>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue