go-cook-portal/handlers/auth.go
2024-04-14 19:38:45 -07:00

58 lines
1.2 KiB
Go

package handlers
import (
"log"
"net/http"
"templ-test/domain"
"templ-test/views/auth"
"templ-test/views/home"
"github.com/labstack/echo/v4"
)
func (h *Handlers) AuthLogin(c echo.Context) error {
return Render(c, http.StatusOK, auth.AuthLogin())
}
func (h *Handlers) AuthLoginPost(c echo.Context) error {
// check the form data
user := c.FormValue("username")
password := c.FormValue("password")
// send request to the API
resp, err := h.api.Auth.Login(user, password)
if err != nil {
return err
}
cookie := new(http.Cookie)
cookie.Name = domain.CookieToken
cookie.Value = resp.Token
c.SetCookie(cookie)
cookie = new(http.Cookie)
cookie.Name = domain.CookieRefreshToken
cookie.Value = resp.RefreshToken
c.SetCookie(cookie)
cookie = new(http.Cookie)
cookie.Name = domain.CookieUser
cookie.Value = user
c.SetCookie(cookie)
// render
return Render(c, http.StatusOK, auth.LoginPost())
}
func (h *Handlers) AuthShowCookies(c echo.Context) error {
claims, err := ValidateJwt(c, h.cfg.SharedApiSecret, h.cfg.ApiServerUri)
if err != nil {
return Render(c, http.StatusInternalServerError, home.Error(err))
}
log.Println(claims)
cookies := GetCookieValues(c)
return Render(c, http.StatusOK, auth.ShowCookie(cookies))
}