Skip to content

Conversation

@BarryThePenguin
Copy link
Contributor

@BarryThePenguin BarryThePenguin commented Jun 2, 2025

I've been replacing an authentication library in a Next.js application, and thought it would be a good contribution to make. This PR is only focusing on the session middleware.

It started out as a library to bridge the gap between the previous authentication library and a new library. It runs on Cloudflare Workers, so the library gradually evolved into a standalone worker. This latest iteration is now middleware for a Hono app. It's been in progress for a while now.

I've done a lot of reading and research along the way. Some key resources that I've used include:

I also looked at using other similar libraries and frameworks including..

..all of which do a lot more than I need, like account and database management, or don't expose the functionality I'm after. Specifically, an OIDC-client service to manage sessions and access/id/refresh tokens, while acting as an auth middleware proxying requests to upstream services.

What I'm essentially after is OAuth2 Proxy that I can run in Cloudflare Workers 😅

There are a few other's out there with similar needs, so I thought I'd share the end result.

@changeset-bot
Copy link

changeset-bot bot commented Jun 2, 2025

🦋 Changeset detected

Latest commit: 9281235

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hono/session Minor

Not sure what this means? Click here to learn what changesets are.

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

@codecov-commenter
Copy link

codecov-commenter commented Jun 2, 2025

Codecov Report

Attention: Patch coverage is 99.25373% with 2 lines in your changes missing coverage. Please review.

Project coverage is 63.45%. Comparing base (86a4ca4) to head (9281235).
Report is 43 commits behind head on main.

Files with missing lines Patch % Lines
packages/session/src/helper/testing/index.ts 95.34% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1188       +/-   ##
===========================================
- Coverage   79.32%   63.45%   -15.88%     
===========================================
  Files          81      100       +19     
  Lines        2443     3467     +1024     
  Branches      633      911      +278     
===========================================
+ Hits         1938     2200      +262     
- Misses        419     1029      +610     
- Partials       86      238      +152     
Flag Coverage Δ
session 8.35% <99.25%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@BarryThePenguin BarryThePenguin force-pushed the feature/session branch 6 times, most recently from ad26303 to 1a85b15 Compare June 11, 2025 10:34
@BarryThePenguin BarryThePenguin force-pushed the feature/session branch 4 times, most recently from 42dbe54 to 4bea706 Compare June 19, 2025 23:40
useSession({ secret })
)

app.get('/session', async (c) => {
Copy link
Member

Choose a reason for hiding this comment

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

I updated the examples to the "not chained" style. It's common for users. If you pass SessionEnv to Hono as a generic, you can write it like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, thank you!

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

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

LGTM!

@yusukebe
Copy link
Member

Hi @BarryThePenguin

Looks good to me! I like this middleware. I'll create some examples using it later. Thank you!

@yusukebe yusukebe merged commit 18b1ebc into honojs:main Jun 26, 2025
6 of 7 checks passed
@github-actions github-actions bot mentioned this pull request Jun 26, 2025
@BarryThePenguin BarryThePenguin deleted the feature/session branch June 26, 2025 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants