go-cook-portal/handlers/auth.go

49 lines
1016 B
Go
Raw Normal View History

2024-04-09 07:29:45 -07:00
package handlers
import (
"net/http"
"templ-test/views"
"templ-test/views/auth"
2024-04-09 07:29:45 -07:00
"github.com/labstack/echo/v4"
)
func (h *Handlers) AuthLogin(c echo.Context) error {
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
user := c.FormValue("username")
password := c.FormValue("password")
2024-04-09 07:29:45 -07:00
// send request to the API
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
return Render(c, http.StatusOK, views.Home())
}
func (h *Handlers) AuthShowCookies(c echo.Context) error {
cookies := GetCookieValues(c)
return Render(c, http.StatusOK, auth.ShowCookie(cookies))
2024-04-09 07:29:45 -07:00
}