2024-04-09 07:29:45 -07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"templ-test/client"
|
2024-04-12 15:48:51 -07:00
|
|
|
"templ-test/models"
|
2024-04-09 07:29:45 -07:00
|
|
|
"templ-test/services"
|
|
|
|
|
|
|
|
"github.com/a-h/templ"
|
2024-04-12 15:48:51 -07:00
|
|
|
|
2024-04-09 07:29:45 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2024-04-12 15:48:51 -07:00
|
|
|
const (
|
|
|
|
CookieToken = "token"
|
|
|
|
CookieRefreshToken = "refresh"
|
|
|
|
CookieUser = "user"
|
|
|
|
)
|
|
|
|
|
2024-04-09 07:29:45 -07:00
|
|
|
type Handlers struct {
|
2024-04-12 15:48:51 -07:00
|
|
|
api client.ApiClient
|
|
|
|
cfg services.EnvConfig
|
2024-04-09 07:29:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandlerClient(api client.ApiClient, cfg services.EnvConfig) *Handlers {
|
|
|
|
h := Handlers{
|
2024-04-12 15:48:51 -07:00
|
|
|
api: api,
|
|
|
|
cfg: cfg,
|
2024-04-09 07:29:45 -07:00
|
|
|
}
|
|
|
|
|
2024-04-12 15:48:51 -07:00
|
|
|
return &h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) Register(group echo.Group) {
|
|
|
|
group.GET("/", h.HomeHandler)
|
|
|
|
group.GET("/list", h.ListHandler)
|
|
|
|
|
|
|
|
auth := group.Group("/auth")
|
2024-04-09 07:29:45 -07:00
|
|
|
auth.GET("/login", h.AuthLogin)
|
|
|
|
auth.POST("/login", h.AuthLoginPost)
|
2024-04-12 15:48:51 -07:00
|
|
|
auth.GET("/cookie", h.AuthShowCookies)
|
2024-04-09 07:29:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func Render(ctx echo.Context, statusCode int, t templ.Component) error {
|
|
|
|
ctx.Response().Writer.WriteHeader(statusCode)
|
|
|
|
ctx.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)
|
|
|
|
return t.Render(ctx.Request().Context(), ctx.Response().Writer)
|
|
|
|
}
|
2024-04-12 15:48:51 -07:00
|
|
|
|
|
|
|
func GetCookieValues(ctx echo.Context) models.AllCookies {
|
|
|
|
m := models.AllCookies{}
|
|
|
|
|
|
|
|
token, err := ctx.Cookie(CookieToken)
|
|
|
|
if err == nil {
|
|
|
|
m.Token = token.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := ctx.Cookie(CookieUser)
|
|
|
|
if err == nil {
|
|
|
|
m.Username = user.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh, err := ctx.Cookie(CookieRefreshToken)
|
|
|
|
if err == nil {
|
|
|
|
m.RefreshToken = refresh.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|