49 lines
1016 B
Go
49 lines
1016 B
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"templ-test/views"
|
|
"templ-test/views/auth"
|
|
|
|
"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 = 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)
|
|
|
|
// 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))
|
|
}
|