mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 20:14:54 +00:00
authentication module done
This commit is contained in:
28
libs/common/src/auth/auth.module.ts
Normal file
28
libs/common/src/auth/auth.module.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { PassportModule } from '@nestjs/passport';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { HelperModule } from '../helper/helper.module';
|
||||
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';
|
||||
|
||||
@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') },
|
||||
}),
|
||||
}),
|
||||
HelperModule,
|
||||
],
|
||||
providers: [JwtStrategy, UserSessionRepository,AuthService,UserRepository],
|
||||
exports: [AuthService],
|
||||
})
|
||||
export class AuthModule {}
|
||||
7
libs/common/src/auth/interfaces/auth.interface.ts
Normal file
7
libs/common/src/auth/interfaces/auth.interface.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export class AuthInterface {
|
||||
email: string;
|
||||
userId: number;
|
||||
uuid: string;
|
||||
sessionId: string;
|
||||
id: number;
|
||||
}
|
||||
55
libs/common/src/auth/services/auth.service.ts
Normal file
55
libs/common/src/auth/services/auth.service.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { HelperHashService } from '../../helper/services';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import { UserSessionRepository } from '@app/common/modules/session/repositories/session.repository';
|
||||
import { UserSessionEntity } from '@app/common/modules/session/entities';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(
|
||||
private jwtService: JwtService,
|
||||
private readonly userRepository: UserRepository,
|
||||
private readonly sessionRepository: UserSessionRepository,
|
||||
private readonly helperHashService: HelperHashService,
|
||||
) {}
|
||||
|
||||
async validateUser(email: string, pass: string): Promise<any> {
|
||||
const user = await this.userRepository.findOne({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
});
|
||||
if (!user.isUserVerified) {
|
||||
throw new BadRequestException('User is not verified');
|
||||
}
|
||||
if (user) {
|
||||
const passwordMatch = this.helperHashService.bcryptCompare(
|
||||
pass,
|
||||
user.password,
|
||||
);
|
||||
if (passwordMatch) {
|
||||
const { ...result } = user;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async createSession(data): Promise<UserSessionEntity> {
|
||||
return await this.sessionRepository.save(data);
|
||||
}
|
||||
|
||||
async login(user: any) {
|
||||
const payload = {
|
||||
email: user.email,
|
||||
userId: user.userId,
|
||||
uuid: user.uuid,
|
||||
type: user.type,
|
||||
sessionId: user.sessionId,
|
||||
};
|
||||
return {
|
||||
access_token: this.jwtService.sign(payload),
|
||||
};
|
||||
}
|
||||
}
|
||||
39
libs/common/src/auth/strategies/jwt.strategy.ts
Normal file
39
libs/common/src/auth/strategies/jwt.strategy.ts
Normal file
@ -0,0 +1,39 @@
|
||||
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 '@app/common/modules/session/repositories/session.repository';
|
||||
import { AuthInterface } from '../interfaces/auth.interface';
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
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