Skip to content

Commit

Permalink
[Feat] Session store
Browse files Browse the repository at this point in the history
Session information is now kept in the database, rather than in memory.
Resolves #17.
  • Loading branch information
angel-penchev committed Oct 17, 2021
1 parent 2a97a44 commit 7be2b7c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 27 deletions.
86 changes: 60 additions & 26 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@nestjs/typeorm": "^8.0.2",
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"connect-typeorm": "^1.1.4",
"dotenv": "^10.0.0",
"express-session": "^1.17.2",
"passport-discord": "^0.1.4",
Expand Down
16 changes: 16 additions & 0 deletions server/src/auth/models/session.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Column, Entity, Index, PrimaryColumn } from 'typeorm';

import { ISession } from 'connect-typeorm';

@Entity({ name: 'sessions ' })
export class Session implements ISession {
@Index()
@Column('bigint')
public expiredAt = Date.now();

@PrimaryColumn('varchar', { length: 255 })
public id = '';

@Column('text')
public json = '';
}
14 changes: 13 additions & 1 deletion server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,28 @@ import * as session from 'express-session';

import { AppModule } from './app.module';
import { NestFactory } from '@nestjs/core';
import { Session } from './auth/models/session.model';
import { TypeormStore } from 'connect-typeorm/out';
import { ValidationPipe } from '@nestjs/common';
import { configObject } from './configuration';
import { getRepository } from 'typeorm';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(session(configObject.session));
const sessionRepository = getRepository(Session);

app.use(
session({
...configObject.session,
store: new TypeormStore().connect(sessionRepository),
}),
);
app.use(passport.initialize());
app.use(passport.session());
app.setGlobalPrefix('api');
app.useGlobalPipes(new ValidationPipe());

await app.listen(3000);
}

bootstrap();

0 comments on commit 7be2b7c

Please sign in to comment.