mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-12 13:46:51 +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/gorilla/sessions"
|
||||||
"github.com/labstack/echo-contrib/session"
|
"github.com/labstack/echo-contrib/session"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
|
||||||
"github.com/rjNemo/rentease/internal/auth"
|
"github.com/rjNemo/rentease/internal/auth"
|
||||||
"github.com/rjNemo/rentease/internal/view"
|
"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")) {
|
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"
|
sess.Values["foo"] = "bar"
|
||||||
if err := sess.Save(c.Request(), c.Response()); err != nil {
|
if err := sess.Save(c.Request(), c.Response()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c.Response().Header().Add("HX-Redirect", "/bookings")
|
return hxRedirect(c, http.StatusOK, "/bookings")
|
||||||
return c.NoContent(200)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
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"
|
"embed"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/a-h/templ"
|
|
||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
sentryecho "github.com/getsentry/sentry-go/echo"
|
sentryecho "github.com/getsentry/sentry-go/echo"
|
||||||
"github.com/gorilla/sessions"
|
"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 {
|
func NewRouter(fs embed.FS, debug bool, secret string, origins []string) *echo.Echo {
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
// config
|
// 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="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=""/>
|
<input type="password" name="password" placeholder="p4Ssw0rD" aria-label="password" autocomplete="password" required=""/>
|
||||||
<button type="submit">Log in</button>
|
<button type="submit">Log in</button>
|
||||||
|
<small> Registration is disabled. Please ask an admin to activate it. </small>
|
||||||
</form>
|
</form>
|
||||||
</article>
|
</article>
|
||||||
</section>
|
</section>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue