mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 13:54:53 +00:00
Merge pull request #88 from SyncrowIOT/feature/google-login
added google login
This commit is contained in:
@ -19,6 +19,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 {
|
||||
@ -89,13 +90,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(
|
||||
@ -110,7 +136,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,
|
||||
@ -119,6 +145,7 @@ export class UserAuthService {
|
||||
}),
|
||||
sessionId: session[1].uuid,
|
||||
});
|
||||
return res;
|
||||
} catch (error) {
|
||||
throw new BadRequestException('Invalid credentials');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user