mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 03:05:13 +00:00
authentication module done
This commit is contained in:
13
libs/common/src/modules/abstract/dtos/abstract.dto.ts
Normal file
13
libs/common/src/modules/abstract/dtos/abstract.dto.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { AbstractEntity } from '../entities/abstract.entity';
|
||||
|
||||
export class AbstractDto {
|
||||
@ApiProperty({ format: 'uuid' })
|
||||
readonly uuid: string;
|
||||
|
||||
constructor(abstract: AbstractEntity, options?: { excludeFields?: boolean }) {
|
||||
if (!options?.excludeFields) {
|
||||
this.uuid = abstract.uuid;
|
||||
}
|
||||
}
|
||||
}
|
1
libs/common/src/modules/abstract/dtos/index.ts
Normal file
1
libs/common/src/modules/abstract/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './abstract.dto'
|
47
libs/common/src/modules/abstract/entities/abstract.entity.ts
Normal file
47
libs/common/src/modules/abstract/entities/abstract.entity.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { Exclude } from 'class-transformer';
|
||||
import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Generated,
|
||||
PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
|
||||
import { AbstractDto } from '../dtos';
|
||||
import { Constructor } from '@app/common/util/types';
|
||||
|
||||
export abstract class AbstractEntity<
|
||||
T extends AbstractDto = AbstractDto,
|
||||
O = never
|
||||
> {
|
||||
@PrimaryGeneratedColumn('increment')
|
||||
@Exclude()
|
||||
public id: number;
|
||||
|
||||
@Column()
|
||||
@Generated('uuid')
|
||||
public uuid: string;
|
||||
|
||||
@CreateDateColumn({ type: 'timestamp' })
|
||||
@Exclude()
|
||||
public createdAt: Date;
|
||||
|
||||
@UpdateDateColumn({ type: 'timestamp' })
|
||||
@Exclude()
|
||||
public updatedAt: Date;
|
||||
|
||||
private dtoClass: Constructor<T, [AbstractEntity, O?]>;
|
||||
|
||||
toDto(options?: O): T {
|
||||
const dtoClass = this.dtoClass;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
if (!dtoClass) {
|
||||
throw new Error(
|
||||
`You need to use @UseDto on class (${this.constructor.name}) be able to call toDto function`
|
||||
);
|
||||
}
|
||||
|
||||
return new this.dtoClass(this, options);
|
||||
}
|
||||
}
|
26
libs/common/src/modules/session/dtos/session.dto.ts
Normal file
26
libs/common/src/modules/session/dtos/session.dto.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import {
|
||||
IsBoolean,
|
||||
IsDate,
|
||||
IsNotEmpty,
|
||||
IsNumber,
|
||||
IsString,
|
||||
} from 'class-validator';
|
||||
|
||||
export class SessionDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsNumber()
|
||||
@IsNotEmpty()
|
||||
userId: number;
|
||||
|
||||
@IsDate()
|
||||
@IsNotEmpty()
|
||||
public loginTime: Date;
|
||||
|
||||
@IsBoolean()
|
||||
@IsNotEmpty()
|
||||
public isLoggedOut: boolean;
|
||||
|
||||
}
|
1
libs/common/src/modules/session/entities/index.ts
Normal file
1
libs/common/src/modules/session/entities/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './session.entity'
|
31
libs/common/src/modules/session/entities/session.entity.ts
Normal file
31
libs/common/src/modules/session/entities/session.entity.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { Column, Entity } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { SessionDto } from '../dtos/session.dto';
|
||||
|
||||
@Entity({ name: 'userSession' })
|
||||
export class UserSessionEntity extends AbstractEntity<SessionDto> {
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
public uuid: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
userId: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
public loginTime: Date;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
public isLoggedOut: boolean;
|
||||
|
||||
constructor(partial: Partial<UserSessionEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { UserSessionEntity } from '../entities';
|
||||
|
||||
@Injectable()
|
||||
export class UserSessionRepository extends Repository<UserSessionEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(UserSessionEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
11
libs/common/src/modules/session/session.repository.module.ts
Normal file
11
libs/common/src/modules/session/session.repository.module.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UserSessionEntity } from './entities';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [TypeOrmModule.forFeature([UserSessionEntity])],
|
||||
})
|
||||
export class UserSessionRepositoryModule {}
|
1
libs/common/src/modules/user-otp/dtos/index.ts
Normal file
1
libs/common/src/modules/user-otp/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './user-otp.dto'
|
19
libs/common/src/modules/user-otp/dtos/user-otp.dto.ts
Normal file
19
libs/common/src/modules/user-otp/dtos/user-otp.dto.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class UserOtpDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public email: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public otpCode: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public expiryTime: string;
|
||||
}
|
1
libs/common/src/modules/user-otp/entities/index.ts
Normal file
1
libs/common/src/modules/user-otp/entities/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './user-otp.entity'
|
32
libs/common/src/modules/user-otp/entities/user-otp.entity.ts
Normal file
32
libs/common/src/modules/user-otp/entities/user-otp.entity.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { Column, Entity } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { UserOtpDto } from '../dtos';
|
||||
import { OtpType } from '@app/common/constants/otp-type.enum';
|
||||
|
||||
@Entity({ name: 'user-otp' })
|
||||
export class UserOtpEntity extends AbstractEntity<UserOtpDto> {
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
public uuid: string;
|
||||
|
||||
@Column({ nullable: false })
|
||||
email: string;
|
||||
|
||||
@Column({ nullable: false })
|
||||
otpCode: string;
|
||||
|
||||
@Column({ nullable: false })
|
||||
expiryTime: Date;
|
||||
|
||||
@Column({
|
||||
type: 'enum',
|
||||
enum: Object.values(OtpType),
|
||||
})
|
||||
type: OtpType;
|
||||
|
||||
constructor(partial: Partial<UserOtpEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { UserOtpEntity } from '../entities';
|
||||
|
||||
@Injectable()
|
||||
export class UserOtpRepository extends Repository<UserOtpEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(UserOtpEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UserOtpEntity } from './entities';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [TypeOrmModule.forFeature([UserOtpEntity])],
|
||||
})
|
||||
export class UserOtpRepositoryModule {}
|
1
libs/common/src/modules/user/dtos/index.ts
Normal file
1
libs/common/src/modules/user/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './user.dto';
|
23
libs/common/src/modules/user/dtos/user.dto.ts
Normal file
23
libs/common/src/modules/user/dtos/user.dto.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class UserDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public email: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public password: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public firstName: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public lastName: string;
|
||||
}
|
0
libs/common/src/modules/user/entities/index.ts
Normal file
0
libs/common/src/modules/user/entities/index.ts
Normal file
41
libs/common/src/modules/user/entities/user.entity.ts
Normal file
41
libs/common/src/modules/user/entities/user.entity.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import { Column, Entity } from 'typeorm';
|
||||
import { UserDto } from '../dtos';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
|
||||
@Entity({ name: 'user' })
|
||||
export class UserEntity extends AbstractEntity<UserDto> {
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
public uuid: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
unique: true,
|
||||
})
|
||||
email: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
public password: string;
|
||||
|
||||
@Column()
|
||||
public firstName: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
public lastName: string;
|
||||
|
||||
@Column({
|
||||
nullable: true,
|
||||
default: false,
|
||||
})
|
||||
public isUserVerified: boolean;
|
||||
|
||||
constructor(partial: Partial<UserEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
1
libs/common/src/modules/user/repositories/index.ts
Normal file
1
libs/common/src/modules/user/repositories/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './user.repository';
|
10
libs/common/src/modules/user/repositories/user.repository.ts
Normal file
10
libs/common/src/modules/user/repositories/user.repository.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { UserEntity } from '../entities/user.entity';
|
||||
|
||||
@Injectable()
|
||||
export class UserRepository extends Repository<UserEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(UserEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
11
libs/common/src/modules/user/user.repository.module.ts
Normal file
11
libs/common/src/modules/user/user.repository.module.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UserEntity } from './entities/user.entity';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [TypeOrmModule.forFeature([UserEntity])],
|
||||
})
|
||||
export class UserRepositoryModule {}
|
Reference in New Issue
Block a user