Compare commits
No commits in common. "4f8d54ebd61cd1528f9bec89053a24f52c98de45" and "a341196f37d49048f6a897ec8a1bf47e41512c04" have entirely different histories.
4f8d54ebd6
...
a341196f37
|
@ -41,9 +41,9 @@ export class AuthController {
|
||||||
description: 'User signin successfully',
|
description: 'User signin successfully',
|
||||||
type: LoginResponseDto,
|
type: LoginResponseDto,
|
||||||
})
|
})
|
||||||
@HttpCode(HttpStatus.OK)
|
|
||||||
@Public()
|
@Public()
|
||||||
@Post('signin')
|
@Post('signin')
|
||||||
|
@HttpCode(HttpStatus.OK)
|
||||||
public async signin(
|
public async signin(
|
||||||
@Res({ passthrough: true }) response: Response,
|
@Res({ passthrough: true }) response: Response,
|
||||||
@Req() request: Request,
|
@Req() request: Request,
|
||||||
|
@ -52,11 +52,6 @@ export class AuthController {
|
||||||
return await this.authService.signin(userCredentials, response, request);
|
return await this.authService.signin(userCredentials, response, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiCreatedResponse({
|
|
||||||
description: 'User tokens refreshed successfully',
|
|
||||||
type: AccessTokenDto,
|
|
||||||
})
|
|
||||||
@HttpCode(HttpStatus.OK)
|
|
||||||
@Public()
|
@Public()
|
||||||
@Post('refresh')
|
@Post('refresh')
|
||||||
public async refreshToken(@Req() request: Request): Promise<AccessTokenDto> {
|
public async refreshToken(@Req() request: Request): Promise<AccessTokenDto> {
|
||||||
|
@ -67,9 +62,48 @@ export class AuthController {
|
||||||
description: 'User signed out successfully',
|
description: 'User signed out successfully',
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
})
|
})
|
||||||
@HttpCode(HttpStatus.OK)
|
|
||||||
@Post('logout')
|
@Post('logout')
|
||||||
|
@HttpCode(HttpStatus.OK)
|
||||||
public async logout(@GetCurrentUserId() userId: string): Promise<boolean> {
|
public async logout(@GetCurrentUserId() userId: string): Promise<boolean> {
|
||||||
return this.authService.logout(userId);
|
return this.authService.logout(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @ApiHeader({
|
||||||
|
// name: 'Authorization',
|
||||||
|
// required: true,
|
||||||
|
// schema: {
|
||||||
|
// example: 'Bearer <refresh_token>',
|
||||||
|
// },
|
||||||
|
// })
|
||||||
|
// @ApiCreatedResponse({
|
||||||
|
// description: 'User tokens refreshed successfully',
|
||||||
|
// type: TokensDto,
|
||||||
|
// })
|
||||||
|
// @Public()
|
||||||
|
// @UseGuards(RefreshTokenGuard)
|
||||||
|
// @Post('refresh')
|
||||||
|
// @HttpCode(HttpStatus.OK)
|
||||||
|
// public async refresh(
|
||||||
|
// @GetCurrentUserId() userId: string,
|
||||||
|
// @GetCurrentUser('refresh_token') refresh_token: string
|
||||||
|
// ): Promise<TokensDto> {
|
||||||
|
// return this.authService.refresh(userId, refresh_token);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// @ApiHeader({
|
||||||
|
// name: 'Authorization',
|
||||||
|
// required: true,
|
||||||
|
// schema: {
|
||||||
|
// example: 'Bearer <access_token>',
|
||||||
|
// },
|
||||||
|
// })
|
||||||
|
// @ApiCreatedResponse({
|
||||||
|
// description: 'Token validity checked successfully',
|
||||||
|
// type: Boolean,
|
||||||
|
// })
|
||||||
|
// @Post('check-token')
|
||||||
|
// @HttpCode(HttpStatus.OK)
|
||||||
|
// public checkTokenValidity(): Promise<boolean> {
|
||||||
|
// return this.authService.checkTokenValidity();
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Response } from 'express';
|
|
||||||
import { Session } from 'src/entities';
|
import { Session } from 'src/entities';
|
||||||
import { LessThan, Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
|
@ -31,21 +30,6 @@ export class SessionRepository {
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async findSessionBySessionId(sessionId: string): Promise<Session> {
|
|
||||||
return await this.sessionRepository.findOne({
|
|
||||||
where: { sessionId: sessionId },
|
|
||||||
relations: ['userCredentials'],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public attachSessionToResponse(response: Response, sessionId: string): void {
|
|
||||||
response.cookie('session_id', sessionId, {
|
|
||||||
httpOnly: true,
|
|
||||||
secure: true,
|
|
||||||
sameSite: 'strict',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public async validateSessionUserAgent(
|
public async validateSessionUserAgent(
|
||||||
sessionId: string,
|
sessionId: string,
|
||||||
currentUserAgent: string
|
currentUserAgent: string
|
||||||
|
@ -61,41 +45,4 @@ export class SessionRepository {
|
||||||
|
|
||||||
return session.userAgent === currentUserAgent;
|
return session.userAgent === currentUserAgent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async checkSessionLimit(userId: string): Promise<void> {
|
|
||||||
const userSessions = await this.sessionRepository
|
|
||||||
.createQueryBuilder('session')
|
|
||||||
.leftJoinAndSelect('session.userCredentials', 'userCredentials')
|
|
||||||
.where('userCredentials.id = :userId', { userId })
|
|
||||||
.orderBy('session.expiresAt', 'ASC')
|
|
||||||
.getMany();
|
|
||||||
|
|
||||||
if (userSessions.length >= 5) {
|
|
||||||
await this.sessionRepository.delete(userSessions[0].id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async invalidateAllSessionsForUser(userId: string): Promise<void> {
|
|
||||||
await this.sessionRepository.delete({ userCredentials: userId });
|
|
||||||
}
|
|
||||||
|
|
||||||
public async extendSessionExpiration(sessionId: string): Promise<void> {
|
|
||||||
const session = await this.sessionRepository.findOne({
|
|
||||||
where: { sessionId },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (session) {
|
|
||||||
session.expiresAt = new Date(
|
|
||||||
session.expiresAt.setMinutes(session.expiresAt.getMinutes() + 30)
|
|
||||||
);
|
|
||||||
await this.sessionRepository.save(session);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO Add cron job to clear expired sessions
|
|
||||||
public async clearExpiredSessions(): Promise<void> {
|
|
||||||
const now = new Date();
|
|
||||||
|
|
||||||
await this.sessionRepository.delete({ expiresAt: LessThan(now) });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,7 +88,7 @@ export class AuthService {
|
||||||
|
|
||||||
this.sessionService.attachSessionToResponse(response, sesseionId.sessionId);
|
this.sessionService.attachSessionToResponse(response, sesseionId.sessionId);
|
||||||
|
|
||||||
return this.generateAndPersistTokens(user.id, user.email, true);
|
return this.generateAndPersistTokens(user.id, user.email);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async logout(userId: string): Promise<boolean> {
|
public async logout(userId: string): Promise<boolean> {
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Response } from 'express';
|
import { Response } from 'express';
|
||||||
import { Session } from 'src/entities';
|
import { Session } from 'src/entities';
|
||||||
|
import { LessThan, Repository } from 'typeorm';
|
||||||
|
|
||||||
import { SessionRepository } from '../repositories/session.repository';
|
import { SessionRepository } from '../repositories/session.repository';
|
||||||
|
|
||||||
|
@ -9,47 +10,76 @@ import { SessionRepository } from '../repositories/session.repository';
|
||||||
export class SessionService {
|
export class SessionService {
|
||||||
public constructor(
|
public constructor(
|
||||||
@InjectRepository(Session)
|
@InjectRepository(Session)
|
||||||
private readonly sessionRepository: SessionRepository
|
private sessionRepository: Repository<Session>,
|
||||||
|
private readonly sessionRepository2: SessionRepository
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public async createSession(
|
public async createSession(
|
||||||
userId: string,
|
userId: string,
|
||||||
userAgent: string
|
userAgent: string
|
||||||
): Promise<Session> {
|
): Promise<Session> {
|
||||||
return await this.sessionRepository.createSession(userId, userAgent);
|
return this.sessionRepository2.createSession(userId, userAgent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async validateSessionUserAgent(
|
public async validateSessionUserAgent(
|
||||||
sessionId: string,
|
sessionId: string,
|
||||||
currentUserAgent: string
|
currentUserAgent: string
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
return await this.sessionRepository.validateSessionUserAgent(
|
return this.sessionRepository2.validateSessionUserAgent(
|
||||||
sessionId,
|
sessionId,
|
||||||
currentUserAgent
|
currentUserAgent
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async checkSessionLimit(userId: string): Promise<void> {
|
public async checkSessionLimit(userId: string): Promise<void> {
|
||||||
await this.sessionRepository.checkSessionLimit(userId);
|
const userSessions = await this.sessionRepository
|
||||||
|
.createQueryBuilder('session')
|
||||||
|
.leftJoinAndSelect('session.userCredentials', 'userCredentials')
|
||||||
|
.where('userCredentials.id = :userId', { userId })
|
||||||
|
.orderBy('session.expiresAt', 'ASC')
|
||||||
|
.getMany();
|
||||||
|
|
||||||
|
if (userSessions.length >= 5) {
|
||||||
|
await this.sessionRepository.delete(userSessions[0].id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async invalidateAllSessionsForUser(userId: string): Promise<void> {
|
public async invalidateAllSessionsForUser(userId: string): Promise<void> {
|
||||||
await this.sessionRepository.invalidateAllSessionsForUser(userId);
|
await this.sessionRepository.delete({ userCredentials: userId });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO Add cron job to clear expired sessions
|
||||||
public async clearExpiredSessions(): Promise<void> {
|
public async clearExpiredSessions(): Promise<void> {
|
||||||
await this.sessionRepository.clearExpiredSessions();
|
const now = new Date();
|
||||||
|
|
||||||
|
await this.sessionRepository.delete({ expiresAt: LessThan(now) });
|
||||||
}
|
}
|
||||||
|
|
||||||
public async extendSessionExpiration(sessionId: string): Promise<void> {
|
public async extendSessionExpiration(sessionId: string): Promise<void> {
|
||||||
await this.sessionRepository.extendSessionExpiration(sessionId);
|
const session = await this.sessionRepository.findOne({
|
||||||
|
where: { sessionId },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (session) {
|
||||||
|
session.expiresAt = new Date(
|
||||||
|
session.expiresAt.setMinutes(session.expiresAt.getMinutes() + 30)
|
||||||
|
);
|
||||||
|
await this.sessionRepository.save(session);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async findSessionBySessionId(sessionId: string): Promise<Session> {
|
public async findSessionBySessionId(sessionId: string): Promise<Session> {
|
||||||
return await this.sessionRepository.findSessionBySessionId(sessionId);
|
return this.sessionRepository.findOne({
|
||||||
|
where: { sessionId: sessionId },
|
||||||
|
relations: ['userCredentials'],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public attachSessionToResponse(response: Response, sessionId: string): void {
|
public attachSessionToResponse(response: Response, sessionId: string): void {
|
||||||
this.sessionRepository.attachSessionToResponse(response, sessionId);
|
response.cookie('session_id', sessionId, {
|
||||||
|
httpOnly: true,
|
||||||
|
secure: true,
|
||||||
|
sameSite: 'strict',
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue