mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-12 13:46:51 +00:00
read session to login
This commit is contained in:
parent
e1766812c4
commit
cc621582f9
5 changed files with 9 additions and 10 deletions
|
|
@ -6,6 +6,7 @@ import (
|
||||||
|
|
||||||
"github.com/labstack/echo-contrib/session"
|
"github.com/labstack/echo-contrib/session"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/labstack/gommon/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -13,7 +14,7 @@ const (
|
||||||
routeLogin = "/"
|
routeLogin = "/"
|
||||||
)
|
)
|
||||||
|
|
||||||
func MakeAuthMiddleware(secretKey string) echo.MiddlewareFunc {
|
func MakeAuthMiddleware() echo.MiddlewareFunc {
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
if c.Request().RequestURI == routeLogin {
|
if c.Request().RequestURI == routeLogin {
|
||||||
|
|
@ -21,8 +22,9 @@ func MakeAuthMiddleware(secretKey string) echo.MiddlewareFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
s, err := readSession(c)
|
s, err := readSession(c)
|
||||||
if s != "foo" || err != nil {
|
log.Warnf("%=v", s)
|
||||||
return c.Redirect(http.StatusUnauthorized, routeLogin)
|
if s != "bar" || err != nil {
|
||||||
|
return c.Redirect(http.StatusSeeOther, routeLogin)
|
||||||
}
|
}
|
||||||
return next(c)
|
return next(c)
|
||||||
}
|
}
|
||||||
|
|
@ -34,5 +36,5 @@ func readSession(c echo.Context) (string, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("foo=%v\n", sess.Values["foo"]), nil
|
return fmt.Sprintf("%s", sess.Values["foo"]), nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ 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/labstack/gommon/log"
|
|
||||||
"github.com/rjNemo/rentease/internal/auth"
|
"github.com/rjNemo/rentease/internal/auth"
|
||||||
"github.com/rjNemo/rentease/internal/view"
|
"github.com/rjNemo/rentease/internal/view"
|
||||||
)
|
)
|
||||||
|
|
@ -26,7 +25,6 @@ func handleLogin(as *auth.Service) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
sess, err := session.Get(sessionName, c)
|
sess, err := session.Get(sessionName, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn(err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
sess.Options = &sessions.Options{
|
sess.Options = &sessions.Options{
|
||||||
|
|
@ -41,7 +39,6 @@ func handleLogin(as *auth.Service) echo.HandlerFunc {
|
||||||
|
|
||||||
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 {
|
||||||
log.Warn(err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return c.Redirect(http.StatusSeeOther, "/bookings")
|
return c.Redirect(http.StatusSeeOther, "/bookings")
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ func (s Server) MountHandlers() {
|
||||||
s.Router.POST("/", handleLogin(s.as))
|
s.Router.POST("/", handleLogin(s.as))
|
||||||
// admin
|
// admin
|
||||||
g := s.Router.Group("")
|
g := s.Router.Group("")
|
||||||
g.Use(MakeAuthMiddleware(s.secretKey))
|
g.Use(MakeAuthMiddleware())
|
||||||
g.GET("/bookings", handleListBookingPage(s.bs, s.hc))
|
g.GET("/bookings", handleListBookingPage(s.bs, s.hc))
|
||||||
g.GET("/bookings/new", handleNewBookingPage(s.hc))
|
g.GET("/bookings/new", handleNewBookingPage(s.hc))
|
||||||
g.POST("/bookings/new", handleCreateBooking(s.bs))
|
g.POST("/bookings/new", handleCreateBooking(s.bs))
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ templ Login() {
|
||||||
<main class="container">
|
<main class="container">
|
||||||
<section>
|
<section>
|
||||||
<h1>Welcome</h1>
|
<h1>Welcome</h1>
|
||||||
<form hx-post="/">
|
<form method="POST">
|
||||||
<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>
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ func Login() templ.Component {
|
||||||
templ_7745c5c3_Buffer = templ.GetBuffer()
|
templ_7745c5c3_Buffer = templ.GetBuffer()
|
||||||
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
|
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||||
}
|
}
|
||||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<main class=\"container\"><section><h1>Welcome</h1><form hx-post=\"/\"><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></form></section></main>")
|
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<main class=\"container\"><section><h1>Welcome</h1><form method=\"POST\"><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></form></section></main>")
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue