refactor: 调整userSession服务及实体至Auth模块下
This commit is contained in:
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