refactor: 重构后端鉴权方式
This commit is contained in:
@@ -17,32 +17,19 @@ export class UserSessionService {
|
||||
return this.userSessionRepository.save(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws string 无效原因
|
||||
*/
|
||||
async isSessionValid(userId: string, sessionId: string): Promise<void> {
|
||||
async getSession(sessionId: string) {
|
||||
const session = await this.userSessionRepository.findOne({
|
||||
where: {
|
||||
userId,
|
||||
sessionId,
|
||||
},
|
||||
withDeleted: true,
|
||||
});
|
||||
|
||||
if (session === null) {
|
||||
throw '登陆凭证无效';
|
||||
}
|
||||
|
||||
if (session.deletedAt !== null) {
|
||||
throw session.disabledReason || '登陆凭证无效';
|
||||
}
|
||||
|
||||
return null;
|
||||
return session;
|
||||
}
|
||||
|
||||
async invalidateSession(userId: string, sessionId: string, reason?: string): Promise<void> {
|
||||
async invalidateSession(sessionId: string, reason?: string): Promise<void> {
|
||||
await this.userSessionRepository.update(
|
||||
{ userId, sessionId, deletedAt: null },
|
||||
{ sessionId, deletedAt: null },
|
||||
{
|
||||
deletedAt: new Date(),
|
||||
disabledReason: reason || null,
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
import { Body, Controller, Get, Put, Request, UseGuards } from '@nestjs/common';
|
||||
import { UserService } from './user.service';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { UpdateUserPasswordDto } from './dto/update-user-password.dto';
|
||||
import { AuthService } from 'src/auth/auth.service';
|
||||
import { AuthGuard } from 'src/auth/guards/auth.guard';
|
||||
|
||||
@Controller('user')
|
||||
export class UserController {
|
||||
constructor(
|
||||
private readonly userService: UserService,
|
||||
private readonly authService: AuthService,
|
||||
) {}
|
||||
) { }
|
||||
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@UseGuards(AuthGuard)
|
||||
@Get('me')
|
||||
async getMe(@Request() req) {
|
||||
const { user } = req;
|
||||
return this.userService.findOne({ userId: user.userId });
|
||||
return this.userService.findById(user.userId);
|
||||
}
|
||||
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@UseGuards(AuthGuard)
|
||||
@Put('password')
|
||||
async update(@Request() req, @Body() dto: UpdateUserPasswordDto) {
|
||||
return this.userService.setPassword(req.user.userId, dto.password);
|
||||
|
||||
Reference in New Issue
Block a user