rentease/internal/server/handle_auth.go

43 lines
962 B
Go

package server
import (
"errors"
"fmt"
"net/http"
"os"
"github.com/labstack/echo/v4"
"github.com/rjNemo/rentease/internal/view"
)
func handleLoginPage() echo.HandlerFunc {
return func(c echo.Context) error {
return renderTempl(c, http.StatusOK, view.Login())
}
}
// TODO: move to auth service
func signIn(email, pwd string) error {
if email != os.Getenv("ADMIN") || pwd != os.Getenv("ADMIN_PASSWORD") {
return errors.New("unauthorized")
}
return nil
}
func handleLogin() echo.HandlerFunc {
return func(c echo.Context) error {
email := c.FormValue("email")
pwd := c.FormValue("password")
err := signIn(email, pwd)
if err != nil {
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("%s?err=invalid+credentials", "/login"))
}
if err = writeCookie(c, email); err != nil {
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("%s?err=invalid+credentials", "/login"))
}
return c.Redirect(http.StatusFound, "/bookings")
}
}