rentease/internal/server/handle_auth.go
Ruidy 584d81f7bd
Some checks failed
CI / checks (push) Has been cancelled
feat(i18n): add language toggle and localize views
2026-01-09 16:09:20 -04:00

51 lines
1.4 KiB
Go

package server
import (
"net/http"
"github.com/rjNemo/rentease/internal/constant"
"github.com/rjNemo/rentease/internal/service/auth"
"github.com/rjNemo/rentease/internal/view"
)
func handleLoginPage() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := renderTempl(w, r, http.StatusOK, view.Login(view.LoginFormViewModel{})); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func handleLogin(as *auth.Service) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
email := r.FormValue("email")
password := r.FormValue("password")
if !as.ValidCredentials(email, password) {
lfvm := view.LoginFormViewModel{
Email: email,
Password: password,
Errors: make(map[string]string),
}
lfvm.Errors["credentials"] = "invalid credentials"
if err := renderTempl(w, r, http.StatusUnauthorized, view.LoginForm(lfvm)); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
if err := as.Authenticate(w, r, "foo"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := hxRedirect(w, http.StatusOK, constant.RouteBooking); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}