mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 16:14:55 +00:00
SPRINT-1 related tasks done
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import { UserRepository } from '../../../libs/common/src/modules/user/repositories';
|
||||
import {
|
||||
BadRequestException,
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
@ -14,7 +15,7 @@ import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
|
||||
import { EmailService } from '../../../libs/common/src/util/email.service';
|
||||
import { OtpType } from '../../../libs/common/src/constants/otp-type.enum';
|
||||
import { UserEntity } from '../../../libs/common/src/modules/user/entities/user.entity';
|
||||
import { ILoginResponse } from '../constants/login.response.constant';
|
||||
import * as argon2 from 'argon2';
|
||||
|
||||
@Injectable()
|
||||
export class UserAuthService {
|
||||
@ -64,7 +65,7 @@ export class UserAuthService {
|
||||
);
|
||||
}
|
||||
|
||||
async userLogin(data: UserLoginDto): Promise<ILoginResponse> {
|
||||
async userLogin(data: UserLoginDto) {
|
||||
const user = await this.authService.validateUser(data.email, data.password);
|
||||
if (!user) {
|
||||
throw new UnauthorizedException('Invalid login credentials.');
|
||||
@ -86,7 +87,7 @@ export class UserAuthService {
|
||||
|
||||
return await this.authService.login({
|
||||
email: user.email,
|
||||
userId: user.id,
|
||||
userId: user.uuid,
|
||||
uuid: user.uuid,
|
||||
sessionId: session[1].uuid,
|
||||
});
|
||||
@ -97,7 +98,7 @@ export class UserAuthService {
|
||||
if (!user) {
|
||||
throw new BadRequestException('User does not found');
|
||||
}
|
||||
return await this.userRepository.delete({ uuid });
|
||||
return await this.userRepository.update({ uuid }, { isActive: false });
|
||||
}
|
||||
|
||||
async findOneById(id: string): Promise<UserEntity> {
|
||||
@ -148,4 +149,41 @@ export class UserAuthService {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async userList(): Promise<UserEntity[]> {
|
||||
return await this.userRepository.find({
|
||||
where: { isActive: true },
|
||||
select: {
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
email: true,
|
||||
isActive: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async refreshToken(
|
||||
userId: string,
|
||||
refreshToken: string,
|
||||
type: string,
|
||||
sessionId: string,
|
||||
) {
|
||||
const user = await this.userRepository.findOne({ where: { uuid: userId } });
|
||||
if (!user || !user.refreshToken)
|
||||
throw new ForbiddenException('Access Denied');
|
||||
const refreshTokenMatches = await argon2.verify(
|
||||
user.refreshToken,
|
||||
refreshToken,
|
||||
);
|
||||
if (!refreshTokenMatches) throw new ForbiddenException('Access Denied');
|
||||
const tokens = await this.authService.getTokens({
|
||||
email: user.email,
|
||||
userId: user.uuid,
|
||||
uuid: user.uuid,
|
||||
type,
|
||||
sessionId,
|
||||
});
|
||||
await this.authService.updateRefreshToken(user.uuid, tokens.refreshToken);
|
||||
return tokens;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user