mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
add helper file
This commit is contained in:
parent
eda38b52a0
commit
eef6c85fe3
4 changed files with 30 additions and 17 deletions
|
|
@ -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
26
internal/server/helper.go
Normal 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)
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Reference in a new issue