Skip to content

Commit

Permalink
Merge pull request #116 from kleydon/dev-1
Browse files Browse the repository at this point in the history
fix: dont get expired session
  • Loading branch information
kleydon committed Jun 28, 2023
2 parents 2409fff + 15e8381 commit 1651938
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -75,7 +75,6 @@
"supertest": "^6.1.6",
"ts-jest": "^27.0.7",
"tslint": "^6.1.3",
"tslint-config-suiyobi": "^0.4.8",
"typescript": "^4.4.4"
},
"config": {
Expand Down
19 changes: 18 additions & 1 deletion src/lib/prisma-session-store.spec.ts
Expand Up @@ -121,6 +121,23 @@ describe('PrismaSessionStore', () => {
expect(callback).toHaveBeenCalledWith();
});

it('should not get items for an expired session', async () => {
const [store, { findUniqueMock, deleteMock }] = freshStore();
const callback = jest.fn();
const s1 = {
sid: 'sid-0',
expiresAt: createExpiration(-1, { rounding: 100 }),
};
findUniqueMock.mockResolvedValue(s1);
deleteMock.mockResolvedValue(undefined);

await expect(store.get('sid-0', callback)).resolves.toStrictEqual(
undefined
);
expect(deleteMock).toHaveBeenCalledWith({ where: { sid: s1.sid } });
expect(callback).toHaveBeenCalledWith();
});

it('should resolve to a promise', async () => {
const [store, { findUniqueMock }] = freshStore();

Expand All @@ -139,7 +156,7 @@ describe('PrismaSessionStore', () => {
await store.get('sid-0', callback);

expect(callback).toHaveBeenCalledWith(
new SyntaxError('Unexpected token i in JSON at position 2')
expect.objectContaining({ name: 'SyntaxError' })
);
});

Expand Down
15 changes: 14 additions & 1 deletion src/lib/prisma-session-store.ts
Expand Up @@ -280,7 +280,9 @@ export class PrismaSessionStore<M extends string = 'session'> extends Store {
callback?: (err?: unknown, val?: SessionData) => void
) => {
if (!(await this.validateConnection())) return callback?.();
const session = await this.prisma[this.sessionModelName]
const p = this.prisma[this.sessionModelName];

const session = await p
.findUnique({
where: { sid },
})
Expand All @@ -289,6 +291,17 @@ export class PrismaSessionStore<M extends string = 'session'> extends Store {
if (session === null) return callback?.();

try {
// If session has has expired (allowing for missing 'expiresAt' and 'sid' fields)
if (
session.sid &&
session.expiresAt &&
new Date().valueOf() >= session.expiresAt.valueOf()
) {
this.logger.log(`Session with sid: ${sid} expired; deleting.`);
await p.delete({ where: { sid } });
return callback?.();
}

const result = this.serializer.parse(session.data ?? '{}') as SessionData;
if (callback) defer(callback, undefined, result);

Expand Down
6 changes: 4 additions & 2 deletions tslint.json
@@ -1,7 +1,9 @@
{
"extends": "tslint-config-suiyobi",
"extends": "tslint:recommended",
"rules": {
"no-big-function": false,
"max-file-line-count": false
"max-file-line-count": false,
"ban-types": false,
"variable-name": false
}
}
17 changes: 0 additions & 17 deletions yarn.lock
Expand Up @@ -6573,23 +6573,6 @@ tslint-clean-code@latest:
memoize-decorator "^1.0.2"
tsutils "2.7.1"

tslint-config-suiyobi@^0.4.8:
version "0.4.8"
resolved "https://registry.yarnpkg.com/tslint-config-suiyobi/-/tslint-config-suiyobi-0.4.8.tgz#7c0cddc1658b329bc694d729538ec422ac405679"
integrity sha512-Jfr0K6vuIJBvk3wWwc9MT6OaQT+zgZ580SWwQ/4qK6Y8aC/x09B6SE4ECxLc+fL8UhXwBP4HLbou9SVz56IB+A==
dependencies:
"@angular-devkit/schematics" "^10.1.0"
doctrine "^3.0.0"
immutable "^4.0.0-rc.12"
rxjs-tslint-rules latest
tslint-clean-code latest
tslint-etc latest
tslint-plugin-decorator-member-ordering "^0.0.1"
tslint-plugin-prettier latest
tsutils "^3.17.1"
tsutils-etc "^1.3.1"
typescript-tslint-plugin latest

tslint-etc@latest:
version "1.13.10"
resolved "https://registry.yarnpkg.com/tslint-etc/-/tslint-etc-1.13.10.tgz#5700b0d83f0a243cb7d32b0b7ec2662cb5998bce"
Expand Down

0 comments on commit 1651938

Please sign in to comment.