mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gorilla/sessions"
|
|
"github.com/labstack/echo-contrib/session"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/gommon/log"
|
|
"github.com/rjNemo/rentease/internal/auth"
|
|
"github.com/rjNemo/rentease/internal/view"
|
|
)
|
|
|
|
const (
|
|
sessionName = "rentease"
|
|
sessionAge = 86400 * 7 // 7 days
|
|
)
|
|
|
|
func handleLoginPage() echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
return renderTempl(c, http.StatusOK, view.Login())
|
|
}
|
|
}
|
|
|
|
func handleLogin(as *auth.Service) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
sess, err := session.Get(sessionName, c)
|
|
if err != nil {
|
|
log.Warn(err)
|
|
return err
|
|
}
|
|
sess.Options = &sessions.Options{
|
|
Path: "/",
|
|
MaxAge: sessionAge,
|
|
HttpOnly: true,
|
|
}
|
|
|
|
if !as.Authenticate(c.FormValue("email"), c.FormValue("password")) {
|
|
return c.Redirect(http.StatusTemporaryRedirect, routeLogin)
|
|
}
|
|
|
|
sess.Values["foo"] = "bar"
|
|
if err := sess.Save(c.Request(), c.Response()); err != nil {
|
|
log.Warn(err)
|
|
return err
|
|
}
|
|
return c.Redirect(http.StatusSeeOther, "/bookings")
|
|
}
|
|
}
|