google login done

This commit is contained in:
unknown
2024-10-01 16:52:42 +03:00
parent 20156568e2
commit c0c41cf91d
6 changed files with 227 additions and 212 deletions

View File

@ -41,5 +41,5 @@ export class UserSignUpDto {
@IsString()
@IsOptional()
public regionUuid: string;
public regionUuid?: string;
}

View File

@ -1,16 +1,16 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { IsEmail, IsOptional, IsString } from 'class-validator';
export class UserLoginDto {
@ApiProperty()
@IsEmail()
@IsNotEmpty()
email: string;
@IsOptional()
email?: string;
@ApiProperty()
@IsString()
@IsOptional()
password: string;
password?: string;
@ApiProperty()
@IsString()

View File

@ -21,6 +21,7 @@ import * as argon2 from 'argon2';
import { differenceInSeconds } from '@app/common/helper/differenceInSeconds';
import { LessThan, MoreThan } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { UUID } from 'typeorm/driver/mongodb/bson.typings';
@Injectable()
export class UserAuthService {
@ -93,13 +94,38 @@ export class UserAuthService {
async userLogin(data: UserLoginDto) {
try {
const user = await this.authService.validateUser(
data.email,
data.password,
data.regionUuid,
);
if (!user) {
throw new UnauthorizedException('Invalid login credentials.');
let user;
if (data.googleCode) {
const googleUserData = await this.authService.login({
googleCode: data.googleCode,
});
const userExists = await this.userRepository.exists({
where: {
email: googleUserData['email'],
},
});
user = await this.userRepository.findOne({
where: {
email: googleUserData['email'],
},
});
if (!userExists) {
await this.signUp({
email: googleUserData['email'],
firstName: googleUserData['given_name'],
lastName: googleUserData['family_name'],
password: googleUserData['email'],
});
}
data.email = googleUserData['email'];
data.password = googleUserData['password'];
}
if (!data.googleCode) {
user = await this.authService.validateUser(
data.email,
data.password,
data.regionUuid,
);
}
const session = await Promise.all([
await this.sessionRepository.update(
@ -114,7 +140,7 @@ export class UserAuthService {
isLoggedOut: false,
}),
]);
return await this.authService.login({
const res = await this.authService.login({
email: user.email,
userId: user.uuid,
uuid: user.uuid,
@ -122,8 +148,8 @@ export class UserAuthService {
return { uuid: role.uuid, type: role.roleType.type };
}),
sessionId: session[1].uuid,
googleCode: data.googleCode,
});
return res;
} catch (error) {
throw new BadRequestException('Invalid credentials');
}