rentease/internal/server/handle_auth.go
Ruidy 973a15c55b
feat(deps): migrate from Echo to Chi, update Stripe/Sentry
Switch web framework from Echo to Chi, removing Echo-related
dependencies
and adding chi and cors. Update Stripe to v83.1.0 and Sentry to v0.36.2.
Remove unused and indirect dependencies for a cleaner go.mod/go.sum.
2025-11-02 16:17:18 +01: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, 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, 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)
}
}
}