add helper file

This commit is contained in:
Ruidy 2024-06-08 12:27:05 +02:00
parent eda38b52a0
commit eef6c85fe3
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
4 changed files with 30 additions and 17 deletions

View file

@ -6,6 +6,7 @@ import (
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"github.com/rjNemo/rentease/internal/auth"
"github.com/rjNemo/rentease/internal/view"
)
@ -34,14 +35,13 @@ func handleLogin(as *auth.Service) echo.HandlerFunc {
}
if !as.Authenticate(c.FormValue("email"), c.FormValue("password")) {
return c.Redirect(http.StatusTemporaryRedirect, routeLogin)
return hxRedirect(c, http.StatusTemporaryRedirect, routeLogin)
}
sess.Values["foo"] = "bar"
if err := sess.Save(c.Request(), c.Response()); err != nil {
return err
}
c.Response().Header().Add("HX-Redirect", "/bookings")
return c.NoContent(200)
return hxRedirect(c, http.StatusOK, "/bookings")
}
}

26
internal/server/helper.go Normal file
View file

@ -0,0 +1,26 @@
package server
import (
"context"
"log"
"github.com/a-h/templ"
"github.com/labstack/echo/v4"
)
func renderTempl(c echo.Context, status int, t templ.Component) error {
c.Response().Writer.WriteHeader(status)
err := t.Render(context.Background(), c.Response().Writer)
if err != nil {
log.Printf("failed to render response template %s", err)
return err
}
return nil
}
func hxRedirect(c echo.Context, statusCode int, url string) error {
c.Response().Header().Add("HX-Redirect", url)
return c.NoContent(statusCode)
}

View file

@ -5,13 +5,11 @@ import (
"embed"
"errors"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/a-h/templ"
"github.com/getsentry/sentry-go"
sentryecho "github.com/getsentry/sentry-go/echo"
"github.com/gorilla/sessions"
@ -123,18 +121,6 @@ func (s Server) Start() {
}
}
func renderTempl(c echo.Context, status int, t templ.Component) error {
c.Response().Writer.WriteHeader(status)
err := t.Render(context.Background(), c.Response().Writer)
if err != nil {
log.Printf("failed to render response template %s", err)
return err
}
return nil
}
func NewRouter(fs embed.FS, debug bool, secret string, origins []string) *echo.Echo {
e := echo.New()
// config

View file

@ -16,6 +16,7 @@ templ Login() {
<input type="email" name="email" placeholder="john@email.com" aria-label="email" autocomplete="email" autofocus required=""/>
<input type="password" name="password" placeholder="p4Ssw0rD" aria-label="password" autocomplete="password" required=""/>
<button type="submit">Log in</button>
<small> Registration is disabled. Please ask an admin to activate it. </small>
</form>
</article>
</section>