refactor: 调整userSession服务及实体至Auth模块下
This commit is contained in:
@@ -10,14 +10,14 @@ import {
|
||||
} from '@nestjs/common';
|
||||
import { LoginByPasswordDto } from './dto/login.dto';
|
||||
import { AuthService } from './auth.service';
|
||||
import { UserSessionService } from 'src/user/services/user-session.service';
|
||||
import { UserSessionService } from 'src/auth/service/user-session.service';
|
||||
import { Throttle, ThrottlerGuard } from '@nestjs/throttler';
|
||||
import { Response } from 'express';
|
||||
import { UserService } from 'src/user/user.service';
|
||||
import { AuthGuard } from './guards/auth.guard';
|
||||
import { SmsLoginDto } from './dto/sms-login.dto';
|
||||
import { SmsService } from 'src/sms/sms.service';
|
||||
import { UserSession } from 'src/user/entities/user-session.entity';
|
||||
import { UserSession } from 'src/auth/entity/user-session.entity';
|
||||
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
|
||||
@@ -3,23 +3,25 @@ import { AuthController } from './auth.controller';
|
||||
import { AuthService } from './auth.service';
|
||||
import { UserModule } from 'src/user/user.module';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UserSession } from 'src/user/entities/user-session.entity';
|
||||
import { UserSession } from 'src/auth/entity/user-session.entity';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { VerificationModule } from 'src/verification/verification.module';
|
||||
import { AuthGuard } from './guards/auth.guard';
|
||||
import { OptionalAuthGuard } from './guards/optional-auth.guard';
|
||||
import { SmsModule } from 'src/sms/sms.module';
|
||||
import { PasskeyCredential } from './entity/passkey-credential.entity';
|
||||
import { UserSessionService } from './service/user-session.service';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule,
|
||||
forwardRef(() => UserModule),
|
||||
TypeOrmModule.forFeature([UserSession]),
|
||||
TypeOrmModule.forFeature([UserSession, PasskeyCredential]),
|
||||
VerificationModule,
|
||||
SmsModule,
|
||||
],
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService, AuthGuard, OptionalAuthGuard],
|
||||
exports: [AuthService, AuthGuard, OptionalAuthGuard],
|
||||
providers: [AuthService, AuthGuard, OptionalAuthGuard, UserSessionService],
|
||||
exports: [AuthService, AuthGuard, OptionalAuthGuard, UserSessionService],
|
||||
})
|
||||
export class AuthModule { }
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { createHash } from 'crypto';
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { UserService } from 'src/user/user.service';
|
||||
import { UserSessionService } from 'src/user/services/user-session.service';
|
||||
import { VerificationService } from 'src/verification/verification.service';
|
||||
import { UserSessionService } from 'src/auth/service/user-session.service';
|
||||
import { BusinessException } from 'src/common/exceptions/business.exception';
|
||||
import { ErrorCode } from 'src/common/constants/error-codes';
|
||||
|
||||
|
||||
27
apps/backend/src/auth/entity/user-session.entity.ts
Normal file
27
apps/backend/src/auth/entity/user-session.entity.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
DeleteDateColumn,
|
||||
Entity,
|
||||
Index,
|
||||
PrimaryGeneratedColumn,
|
||||
} from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
@Index(['sessionId', 'userId'])
|
||||
export class UserSession {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
sessionId: string;
|
||||
|
||||
@Column({ length: 36 })
|
||||
userId: string;
|
||||
|
||||
@Column({ nullable: true })
|
||||
disabledReason?: string;
|
||||
|
||||
@CreateDateColumn({ precision: 3 })
|
||||
createdAt: Date;
|
||||
|
||||
@DeleteDateColumn({ nullable: true, precision: 3 })
|
||||
deletedAt: Date;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// auth.guard.ts
|
||||
import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common';
|
||||
import { Request } from 'express';
|
||||
import { UserSessionService } from 'src/user/services/user-session.service';
|
||||
import { UserSessionService } from 'src/auth/service/user-session.service';
|
||||
import { UserService } from 'src/user/user.service';
|
||||
|
||||
@Injectable()
|
||||
|
||||
39
apps/backend/src/auth/service/user-session.service.ts
Normal file
39
apps/backend/src/auth/service/user-session.service.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { UserSession } from '../entity/user-session.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class UserSessionService {
|
||||
constructor(
|
||||
@InjectRepository(UserSession)
|
||||
private readonly userSessionRepository: Repository<UserSession>,
|
||||
) { }
|
||||
|
||||
async createSession(userId: string): Promise<UserSession> {
|
||||
const session = this.userSessionRepository.create({
|
||||
userId,
|
||||
});
|
||||
return this.userSessionRepository.save(session);
|
||||
}
|
||||
|
||||
async getSession(sessionId: string) {
|
||||
const session = await this.userSessionRepository.findOne({
|
||||
where: {
|
||||
sessionId,
|
||||
},
|
||||
});
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
async invalidateSession(sessionId: string, reason?: string): Promise<void> {
|
||||
await this.userSessionRepository.update(
|
||||
{ sessionId, deletedAt: null },
|
||||
{
|
||||
deletedAt: new Date(),
|
||||
disabledReason: reason || null,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user