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(sessions): implement ttl and flash #12693

Merged
merged 8 commits into from
Dec 10, 2024
Merged

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Dec 9, 2024

Changes

Adds support for session data expiry. This includes:

  • a new ttl config option
  • support for a ttl argument to set
  • support for session.flash(), which expires the data after the next request

Testing

Added unit tests

Docs

Copy link

changeset-bot bot commented Dec 9, 2024

⚠️ No Changeset found

Latest commit: a55fedb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) docs pr labels Dec 9, 2024
@ematipico
Copy link
Member

Can you update the RFC with the relative information, so we can review the PR?

@ascorbic
Copy link
Contributor Author

ascorbic commented Dec 9, 2024

@ematipico the RFC has been updated

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Code-wise, I would add more test cases, especially for middleware and rewrites. I also left some questions in the RFC around the .flash.

Comment on lines +2 to +3
Astro.session.flash('flash-value', `Flashed value at ${new Date().toISOString()}`);
return Astro.redirect('/');
Copy link
Member

Choose a reason for hiding this comment

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

Can we add more cases, such as:

  • usage with Astro.rewrite
  • usage with the next("/some-route") (the other form of rewrite): see if the value changes when the next middleware function read from .flash
  • usage with multiple middleware functions e.g. sequence(fn1, fn2)

@@ -138,10 +147,19 @@ export class AstroSession<TDriver extends SessionDriverName = any> {
this.#cookieSet = true;
}
this.#data ??= new Map();
this.#data.set(key, value);
const lifetime = ttl ?? this.#config.ttl;
const expires = typeof lifetime === 'number' ? Date.now() + lifetime * 1000 : lifetime;
Copy link
Member

Choose a reason for hiding this comment

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

Can we add a comment that explains the 1000?

@ascorbic ascorbic merged commit f45962d into astro-dot-session Dec 10, 2024
4 checks passed
@ascorbic ascorbic deleted the session-ttl branch December 10, 2024 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs pr pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants