Skip to content

Commit

Permalink
fix(csrf-with-session): fix order for csrfMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
sixcolors committed May 28, 2024
1 parent 0048a38 commit c715d75
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ go.work

# Ignore main binaries
**/main
csrf-with-session/__debug_bin1210231247

# Ignore certificates in csrf-with-session example
csrf-with-session/cert.pem
csrf-with-session/key.pem
16 changes: 8 additions & 8 deletions csrf-with-session/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,17 @@ func main() {
})

// Route for the login page
app.Get("/login", csrfMiddleware, func(c fiber.Ctx) error {
app.Get("/login", func(c fiber.Ctx) error {
csrfToken := csrf.TokenFromContext(c)

return c.Render("login", fiber.Map{
"Title": "Login",
"csrf": csrfToken,
})
})
}, csrfMiddleware)

// Route for processing the login
app.Post("/login", csrfMiddleware, func(c fiber.Ctx) error {
app.Post("/login", func(c fiber.Ctx) error {
// Retrieve the submitted form data
username := c.FormValue("username")
password := c.FormValue("password")
Expand Down Expand Up @@ -192,7 +192,7 @@ func main() {

// Redirect to the protected route
return c.Redirect().To("/protected")
})
}, csrfMiddleware)

// Route for logging out
app.Get("/logout", func(c fiber.Ctx) error {
Expand All @@ -212,7 +212,7 @@ func main() {
})

// Route for the protected content
app.Get("/protected", csrfMiddleware, func(c fiber.Ctx) error {
app.Get("/protected", func(c fiber.Ctx) error {
// Check if the user is logged in
session, err := store.Get(c)
if err != nil {
Expand All @@ -231,10 +231,10 @@ func main() {
"Title": "Protected",
"csrf": csrfToken,
})
})
}, csrfMiddleware)

// Route for processing the protected form
app.Post("/protected", csrfMiddleware, func(c fiber.Ctx) error {
app.Post("/protected", func(c fiber.Ctx) error {
// Check if the user is logged in
session, err := store.Get(c)
if err != nil {
Expand All @@ -257,7 +257,7 @@ func main() {
"csrf": csrfToken,
"message": message,
})
})
}, csrfMiddleware)

certFile := "cert.pem"
keyFile := "key.pem"
Expand Down

0 comments on commit c715d75

Please sign in to comment.