t2/routes/auth.go

53 lines
1.2 KiB
Go

package routes
import (
"net/http"
"time"
"gitea.henriburau.de/haw-lan/cod4watcher/services"
"gitea.henriburau.de/haw-lan/cod4watcher/views/auth"
)
func (s *Server) HandleLogin(w http.ResponseWriter, r *http.Request) error {
return Render(w, r, auth.LoginForm(auth.LoginFormValues{}, map[string]string{}))
}
func (s *Server) HandleSignin(w http.ResponseWriter, r *http.Request) error {
r.ParseForm()
creds := services.Credentials{
Username: r.FormValue("username"),
Password: r.FormValue("password"),
}
token, expiresIn, err := services.Signin(creds)
if err != nil {
errors := map[string]string{
"username": "username or password incorrect" + err.Error(),
"password": "username or password incorrect" + err.Error(),
}
return Render(w, r, auth.LoginForm(auth.LoginFormValues{
Username: creds.Username,
Password: creds.Password,
}, errors))
}
http.SetCookie(w, &http.Cookie{
Name: "session_token",
Value: token,
Expires: expiresIn,
})
return hxRedirect(w, r, "/")
}
func (s *Server) HandleLogout(w http.ResponseWriter, r *http.Request) error {
http.SetCookie(w, &http.Cookie{
Name: "session_token",
Value: "",
Expires: time.Time{},
})
return hxRedirect(w, r, "/")
}