Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BullModule } from '@nestjs/bullmq';
import { QUEUE_NAME } from './constants';
import { ENVIRONMENT, QUEUE_NAME } from './constants';
import { RedisModule } from './redis/redis.module';
import { APP_GUARD } from '@nestjs/core';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';

@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true, envFilePath: ['.env.local', '.env.prod'] }),
ConfigModule.forRoot({
isGlobal: true,
envFilePath: ['.env.local', '.env.prod'],
}),
TypeOrmModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService<IENV, true>) => ({
Expand All @@ -29,7 +32,10 @@ import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
database: config.get('POSTGRES_DB'),
autoLoadEntities: true,
synchronize: true,
ssl: true
ssl:
config.get('NODE_ENVIRONMENT') == ENVIRONMENT.PRODUCTION
? true
: false,
}),
}),
BullModule.forRootAsync({
Expand Down
10 changes: 7 additions & 3 deletions src/auth/auth-session.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, UnprocessableEntityException } from '@nestjs/common';
import { IAuthSession } from './interfaces/auth-session.interface';
import { RedisService } from 'src/redis/redis.service';
import {
Expand All @@ -21,17 +21,21 @@ export class AuthSessionService {
const userSessionKey = `${USER_SESSIONS_PREFIX}${data.userId}`;
const userSessionsValue = sessionId;

await this.redisService.set({
const authSession = await this.redisService.set({
key: authSessionKey,
value: authSessionValue,
ttl: AUTH_SESSION_TTL,
});

await this.redisService.addToSet({
if (!authSession) throw new UnprocessableEntityException();

const userSession = await this.redisService.addToSet({
key: userSessionKey,
value: userSessionsValue,
});

if (!userSession) throw new UnprocessableEntityException();

return sessionId;
} catch (error) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/commons/interfaces/env.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
interface IENV {
// App
NODE_ENVIRONMENT: 'local' | 'docker' | 'dev' | 'staging' | 'prod';
NODE_ENVIRONMENT: 'local' | 'docker' | 'development' | 'staging' | 'production';
APP_PORT: number;
APP_KEY: string;
ENABLE_RATE_LIMITING: string;
Expand Down
14 changes: 11 additions & 3 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
export const AUTH_SESSION_PREFIX = 'auth:session:'
export const USER_SESSIONS_PREFIX = 'user:sessions:'
export const MESSAGE_FROM_PAST_DEFAULT_EMAIL_SUBJECT = 'Message from your past'
export const AUTH_SESSION_PREFIX = 'auth:session:';
export const USER_SESSIONS_PREFIX = 'user:sessions:';
export const MESSAGE_FROM_PAST_DEFAULT_EMAIL_SUBJECT = 'Message from your past';

export const AUTH_SESSION_TTL = 60 * 60 * 24; // 1 day

export enum ENVIRONMENT {
LOCAL = 'local',
DOCKER = 'docker',
DEVELOPMENT = 'development',
STAGING = 'staging',
PRODUCTION = 'production',
}

export enum EMAIL_TEMPLATES {
SIGNIN_MAGIC_LINK = 'signin_magic_link',
MESSAGE_TO_FUTURE = 'message_to_future',
Expand Down
2 changes: 1 addition & 1 deletion src/redis/redis.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class RedisService {
const redisPassword = this.configService.get('REDIS_PASSWORD');
const redisHost = this.configService.get('REDIS_HOST');
const redisPort = this.configService.get('REDIS_PORT');

if (redisUser && redisPassword) {
this.redisUrl = `redis://${redisUser}:${redisPassword}@${redisHost}:${redisPort}`;
} else {
Expand Down
Loading