auth.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package web
  2. import (
  3. "net/http"
  4. "github.com/pocketbase/pocketbase/core"
  5. "code.osinet.fr/fgm/jamtrack/views"
  6. )
  7. const cookieName = "pb_auth"
  8. // LoginGet renders the login form.
  9. func LoginGet(e *core.RequestEvent) error {
  10. return views.Login("").Render(e.Request.Context(), e.Response)
  11. }
  12. // LoginPost handles username/password authentication and sets the session cookie.
  13. func LoginPost(e *core.RequestEvent) error {
  14. username := e.Request.FormValue("username")
  15. password := e.Request.FormValue("password")
  16. record, err := e.App.FindFirstRecordByData("users", "username", username)
  17. if err != nil || !record.ValidatePassword(password) {
  18. return views.Login("Invalid username or password.").Render(e.Request.Context(), e.Response)
  19. }
  20. token, err := record.NewAuthToken()
  21. if err != nil {
  22. return views.Login("Authentication error. Please try again.").Render(e.Request.Context(), e.Response)
  23. }
  24. e.SetCookie(&http.Cookie{
  25. Name: cookieName,
  26. Value: token,
  27. Path: "/",
  28. HttpOnly: true,
  29. SameSite: http.SameSiteLaxMode,
  30. })
  31. return e.Redirect(http.StatusFound, "/")
  32. }
  33. // Logout clears the session cookie and redirects to the login page.
  34. func Logout(e *core.RequestEvent) error {
  35. e.SetCookie(&http.Cookie{
  36. Name: cookieName,
  37. Value: "",
  38. Path: "/",
  39. MaxAge: -1,
  40. HttpOnly: true,
  41. SameSite: http.SameSiteLaxMode,
  42. })
  43. return e.Redirect(http.StatusFound, "/login")
  44. }
  45. // RequireAuth is a middleware that validates the session cookie and populates
  46. // e.Auth. Unauthenticated requests are redirected to /login.
  47. func RequireAuth(e *core.RequestEvent) error {
  48. cookie, err := e.Request.Cookie(cookieName)
  49. if err != nil || cookie.Value == "" {
  50. return e.Redirect(http.StatusFound, "/login")
  51. }
  52. record, err := e.App.FindAuthRecordByToken(cookie.Value, core.TokenTypeAuth)
  53. if err != nil {
  54. return e.Redirect(http.StatusFound, "/login")
  55. }
  56. e.Auth = record
  57. return e.Next()
  58. }
PANIC: session(release): write data/sessions/1/8/18e667181114b949: no space left on device

PANIC

session(release): write data/sessions/1/8/18e667181114b949: no space left on device
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/session@v1.0.3/session.go:204 (0xb13e07)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/context.go:80 (0x967b75)
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/inject@v0.0.0-20200308113650-138e5925c53b/inject.go:157 (0x9512ee)
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/inject@v0.0.0-20200308113650-138e5925c53b/inject.go:135 (0x951205)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/context.go:124 (0x967cc4)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/context.go:114 (0x967bf6)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/recovery.go:161 (0x15baec4)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/logger.go:40 (0x96b257)
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/inject@v0.0.0-20200308113650-138e5925c53b/inject.go:157 (0x9512ee)
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/inject@v0.0.0-20200308113650-138e5925c53b/inject.go:135 (0x951205)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/context.go:124 (0x967cc4)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/router.go:187 (0x972959)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/router.go:304 (0x973a01)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/macaron.go:218 (0x96c572)
/my/cache/.heroku/go/go1.26.3/go/src/net/http/server.go:3311 (0x85a5cd)
/my/cache/.heroku/go/go1.26.3/go/src/net/http/server.go:2073 (0x837f6f)
/my/cache/.heroku/go/go1.26.3/go/src/runtime/asm_amd64.s:1771 (0x493380)