mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 02:24:54 +00:00
SPRINT-1 related tasks done
This commit is contained in:
@ -7,22 +7,22 @@ import { JwtStrategy } from './strategies/jwt.strategy';
|
||||
import { UserSessionRepository } from '../modules/session/repositories/session.repository';
|
||||
import { AuthService } from './services/auth.service';
|
||||
import { UserRepository } from '../modules/user/repositories';
|
||||
import { RefreshTokenStrategy } from './strategies/refresh-token.strategy';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot(),
|
||||
PassportModule,
|
||||
JwtModule.registerAsync({
|
||||
imports: [ConfigModule],
|
||||
inject: [ConfigService],
|
||||
useFactory: async (configService: ConfigService) => ({
|
||||
secret: configService.get('JWT_SECRET'),
|
||||
signOptions: { expiresIn: configService.get('JWT_EXPIRE_TIME') },
|
||||
}),
|
||||
}),
|
||||
JwtModule.register({}),
|
||||
HelperModule,
|
||||
],
|
||||
providers: [JwtStrategy, UserSessionRepository, AuthService, UserRepository],
|
||||
providers: [
|
||||
JwtStrategy,
|
||||
RefreshTokenStrategy,
|
||||
UserSessionRepository,
|
||||
AuthService,
|
||||
UserRepository,
|
||||
],
|
||||
exports: [AuthService],
|
||||
})
|
||||
export class AuthModule {}
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import * as argon2 from 'argon2';
|
||||
import { HelperHashService } from '../../helper/services';
|
||||
import { UserRepository } from '../../../../common/src/modules/user/repositories';
|
||||
import { UserSessionRepository } from '../../../../common/src/modules/session/repositories/session.repository';
|
||||
import { UserSessionEntity } from '../../../../common/src/modules/session/entities';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
@ -12,6 +14,7 @@ export class AuthService {
|
||||
private readonly userRepository: UserRepository,
|
||||
private readonly sessionRepository: UserSessionRepository,
|
||||
private readonly helperHashService: HelperHashService,
|
||||
private readonly configService: ConfigService,
|
||||
) {}
|
||||
|
||||
async validateUser(email: string, pass: string): Promise<any> {
|
||||
@ -40,6 +43,24 @@ export class AuthService {
|
||||
return await this.sessionRepository.save(data);
|
||||
}
|
||||
|
||||
async getTokens(payload) {
|
||||
const [accessToken, refreshToken] = await Promise.all([
|
||||
this.jwtService.signAsync(payload, {
|
||||
secret: this.configService.get<string>('JWT_SECRET'),
|
||||
expiresIn: '24h',
|
||||
}),
|
||||
this.jwtService.signAsync(payload, {
|
||||
secret: this.configService.get<string>('JWT_SECRET'),
|
||||
expiresIn: '7d',
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
accessToken,
|
||||
refreshToken,
|
||||
};
|
||||
}
|
||||
|
||||
async login(user: any) {
|
||||
const payload = {
|
||||
email: user.email,
|
||||
@ -48,8 +69,22 @@ export class AuthService {
|
||||
type: user.type,
|
||||
sessionId: user.sessionId,
|
||||
};
|
||||
return {
|
||||
access_token: this.jwtService.sign(payload),
|
||||
};
|
||||
const tokens = await this.getTokens(payload);
|
||||
await this.updateRefreshToken(user.uuid, tokens.refreshToken);
|
||||
return tokens;
|
||||
}
|
||||
|
||||
async updateRefreshToken(userId: string, refreshToken: string) {
|
||||
const hashedRefreshToken = await this.hashData(refreshToken);
|
||||
await this.userRepository.update(
|
||||
{ uuid: userId },
|
||||
{
|
||||
refreshToken: hashedRefreshToken,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
hashData(data: string) {
|
||||
return argon2.hash(data);
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@ import { UserSessionRepository } from '../../../src/modules/session/repositories
|
||||
import { AuthInterface } from '../interfaces/auth.interface';
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
||||
constructor(
|
||||
private readonly sessionRepository: UserSessionRepository,
|
||||
private readonly configService: ConfigService,
|
||||
|
||||
42
libs/common/src/auth/strategies/refresh-token.strategy.ts
Normal file
42
libs/common/src/auth/strategies/refresh-token.strategy.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { UserSessionRepository } from '../../../src/modules/session/repositories/session.repository';
|
||||
import { AuthInterface } from '../interfaces/auth.interface';
|
||||
|
||||
@Injectable()
|
||||
export class RefreshTokenStrategy extends PassportStrategy(
|
||||
Strategy,
|
||||
'jwt-refresh',
|
||||
) {
|
||||
constructor(
|
||||
private readonly sessionRepository: UserSessionRepository,
|
||||
private readonly configService: ConfigService,
|
||||
) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
ignoreExpiration: false,
|
||||
secretOrKey: configService.get('JWT_SECRET'),
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: AuthInterface) {
|
||||
const validateUser = await this.sessionRepository.findOne({
|
||||
where: {
|
||||
uuid: payload.sessionId,
|
||||
isLoggedOut: false,
|
||||
},
|
||||
});
|
||||
if (validateUser) {
|
||||
return {
|
||||
email: payload.email,
|
||||
userId: payload.id,
|
||||
uuid: payload.uuid,
|
||||
sessionId: payload.sessionId,
|
||||
};
|
||||
} else {
|
||||
throw new BadRequestException('Unauthorized');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user