gaurishhs / elysia-session

Sessions plugin for Elysia
MIT License
20 stars 2 forks source link

Am I dumb or it doesn't save state? #6

Closed ultimateshadsform closed 1 month ago

ultimateshadsform commented 1 month ago

Here is example:

import Elysia, { type Context, type ICtx } from 'elysia';
import { sessionPlugin } from 'elysia-session';
import { MongooseStore } from './db/mongostore';
import { conn, connectDB } from './db/db';
import pc from 'picocolors';

connectDB();

const app = new Elysia();

app.use(
  sessionPlugin({
    cookieName: 'session', // Optional, defaults to "session"
    store: new MongooseStore(conn, 'sessions'),
    cookieOptions: {
      httpOnly: true,
      sameSite: 'lax',
      secure: true,
    },
    expireAfter: 15 * 60, // 15 minutes
  })
);

app.get('/count', (ctx: ICtx) => {
  let count = (ctx.session.get('count') as number) || 0;

  if (!count) {
    ctx.session.set('count', 1);
  }

  ctx.session.set('count', count + 1);

  ctx.set.headers['Content-Type'] = 'text/html';

  return `
  <html>
    <head>
      <title>Session Count</title>
    </head>
    <body>
      <h1>Session Count</h1>
      <p>Count: ${count}</p>
    </body>
  </html>
  `;
});

app.listen(3000, () => {
  console.log(pc.green('Server running on port 3000'));
});

It creates a new session id on every page visit. It would be nice with some usage examples.

gaurishhs commented 1 month ago

should be fixed in the latest release.

ultimateshadsform commented 1 month ago

should be fixed in the latest release.

Did you update the npm package on npm?

gaurishhs commented 1 month ago

Yes. There is a github action for it and it seems to have passed.

On Wed, 21 Aug, 2024, 20:15 Alexander, @.***> wrote:

should be fixed in the latest release.

Did you update the npm package on npm?

— Reply to this email directly, view it on GitHub https://github.com/gaurishhs/elysia-session/issues/6#issuecomment-2302228795, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASZRZB4NHRRVLTIJBHUQUTLZSSRXRAVCNFSM6AAAAABM2PLRD2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMBSGIZDQNZZGU . You are receiving this because you commented.Message ID: @.***>