feat: handle google login

This commit is contained in:
Abdalhamid Alhamad
2025-01-09 15:14:09 +03:00
parent db02a28b4d
commit 756e947c8a
10 changed files with 210 additions and 22 deletions
+8 -2
View File
@@ -22,10 +22,10 @@ export class User extends BaseEntity {
@Column('varchar', { length: 255, nullable: true, name: 'email' })
email!: string;
@Column('varchar', { length: 255, name: 'phone_number' })
@Column('varchar', { length: 255, name: 'phone_number', nullable: true })
phoneNumber!: string;
@Column('varchar', { length: 10, name: 'country_code' })
@Column('varchar', { length: 10, name: 'country_code', nullable: true })
countryCode!: string;
@Column('varchar', { length: 255, name: 'password', nullable: true })
@@ -40,6 +40,12 @@ export class User extends BaseEntity {
@Column('varchar', { length: 255, nullable: true, name: 'apple_id' })
appleId!: string;
@Column('boolean', { default: false, name: 'is_phone_verified' })
isPhoneVerified!: boolean;
@Column('boolean', { default: false, name: 'is_email_verified' })
isEmailVerified!: boolean;
@Column('boolean', { default: false, name: 'is_profile_completed' })
isProfileCompleted!: boolean;
+29
View File
@@ -1,5 +1,6 @@
import { BadRequestException, forwardRef, Inject, Injectable, Logger } from '@nestjs/common';
import { FindOptionsWhere } from 'typeorm';
import { Transactional } from 'typeorm-transactional';
import { CustomerNotificationSettings } from '~/customer/entities/customer-notification-settings.entity';
import { CustomerService } from '~/customer/services';
import { Guardian } from '~/guardian/entities/guradian.entity';
@@ -75,8 +76,36 @@ export class UserService {
return this.userRepository.update(userId, { password: passcode, salt, isProfileCompleted: true });
}
setPhoneNumber(userId: string, phoneNumber: string, countryCode: string) {
this.logger.log(`Setting phone number ${phoneNumber} for user ${userId}`);
return this.userRepository.update(userId, { phoneNumber, countryCode });
}
verifyPhoneNumber(userId: string) {
this.logger.log(`Verifying phone number for user ${userId}`);
return this.userRepository.update(userId, { isPhoneVerified: true });
}
@Transactional()
async createGoogleUser(googleId: string, email: string) {
this.logger.log(`Creating google user with googleId ${googleId} and email ${email}`);
const user = await this.userRepository.createUser({ googleId, email, roles: [Roles.GUARDIAN] });
await this.customerService.createCustomer(
{
guardian: Guardian.create({ id: user.id }),
notificationSettings: new CustomerNotificationSettings(),
},
user,
);
return this.findUserOrThrow({ id: user.id });
}
@Transactional()
async verifyUserAndCreateCustomer(user: User) {
this.logger.log(`Verifying user ${user.id} and creating customer`);
await this.userRepository.update(user.id, { isPhoneVerified: true });
await this.customerService.createCustomer(
{
guardian: Guardian.create({ id: user.id }),