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