Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: add Reset method to Session struct in session middleware #2654

Merged
merged 1 commit into from
Oct 2, 2023

Conversation

kaptinlin
Copy link
Contributor

@kaptinlin kaptinlin commented Sep 30, 2023

Description

This pull request introduces a new Reset method to the Session struct in the Fiber's session middleware. The Reset method is designed to generate a new session id, delete the old one from storage, and reset the associated data. This functionality extends the existing session management methods such as getting, setting, and deleting session data, as well as destroying the session.

The motivation behind this change is to provide more flexibility in session management, especially in scenarios like repeated user logins. With the Reset method, we can easily invalidate the current session data and simultaneously create a new session, without the need to explicitly destroy and recreate the session as before. This simplifies the process and makes the code cleaner and more efficient.

This pull request also includes relevant test code to ensure the correct functionality of the Reset method. Additionally, some existing test code has been optimized to the session middleware.

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Checklist:

  • For new functionalities I follow the inspiration of the express js framework and built them similar in usage
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation - /docs/ directory for https://docs.gofiber.io/
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • If new dependencies exist, I have checked that they are really necessary and agreed with the maintainers/community (we want to have as few dependencies as possible)
  • I tried to make my code as fast as possible with as few allocations as possible
  • For new code I have written benchmarks so that they can be analyzed and improved

Usage

// Initialize default config
// This stores all of your app's sessions
store := session.New()

app.Post("/login", func(c *fiber.Ctx) error {
    // Get session from storage
    sess, err := store.Get(c)
    if err != nil {
        panic(err)
    }

    // ... validate login ...

    // Check if the session is fresh
    if !sess.Fresh() {
        // If the session is not fresh, reset it
        if err := sess.Reset(); err != nil {
            panic(err)
        }
    }

    // Set new session data
    sess.Set("user_id", user.ID)

    // Save session
    if err := sess.Save(); err != nil {
        panic(err)
    }

    return c.SendString(fmt.Sprintf("Welcome %v", user.ID))
})

@welcome
Copy link

welcome bot commented Sep 30, 2023

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@kaptinlin kaptinlin changed the title 🎉 feat: Add Reset method to Session struct in session middleware ✨ feat: Add Reset method to Session struct in session middleware Sep 30, 2023
@kaptinlin kaptinlin changed the title ✨ feat: Add Reset method to Session struct in session middleware ✨ feat: add Reset method to Session struct in session middleware Sep 30, 2023
Copy link
Member

@gaby gaby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets wait for the other PR to be merged. Then this one can be rebased.

docs/api/middleware/session.md Outdated Show resolved Hide resolved
@gaby
Copy link
Member

gaby commented Oct 1, 2023

Pending #2655

@ReneWerner87 ReneWerner87 merged commit d00f0b8 into gofiber:master Oct 2, 2023
34 of 39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants