refactor: 调整userSession服务及实体至Auth模块下

This commit is contained in:
2025-12-18 12:16:09 +08:00
parent d6bf4d3cb3
commit 77b7bf8ab2
8 changed files with 17 additions and 16 deletions

View File

@@ -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 {

View File

@@ -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 { }

View File

@@ -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';

View 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;
}

View File

@@ -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()

View 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,
}
)
}
}