Skip to content
Merged

Djh #22

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 44 additions & 20 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UsercenterService } from './../usercenter/usercenter.service';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { Injectable, UnauthorizedException, NotFoundException, BadRequestException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';

@Injectable()
Expand All @@ -10,32 +10,51 @@ export class AuthService {
) {}

async signIn(username: string, pass: string): Promise<any> {
const user = await this.usersService.findOne(username);
try {
const user = await this.usersService.findOne(username);

// 直接使用用户密码进行验证
if (user?.userPassword !== pass) {
throw new UnauthorizedException();
}
// 直接使用用户密码进行验证
if (user?.userPassword !== pass) {
throw new UnauthorizedException('用户名或密码错误');
}

const payload = { sub: user.userId, username: user.userName };
const refreshPayload = { sub: user.userId };

return {
userId: user.userId,
access_token: await this.jwtService.signAsync(payload, {
expiresIn: '7d',
}),
refresh_token: await this.jwtService.signAsync(refreshPayload, {
expiresIn: '7d',
}),
};
const payload = { sub: user.userId, username: user.userName };
const refreshPayload = { sub: user.userId };

return {
userId: user.userId,
access_token: await this.jwtService.signAsync(payload, {
expiresIn: '7d',
}),
refresh_token: await this.jwtService.signAsync(refreshPayload, {
expiresIn: '7d',
}),
};
} catch (error) {
// 捕获findOne方法抛出的NotFoundException异常并将其转换为UnauthorizedException
// 这样用户名不存在和密码错误都返回相同的401状态码
if (error instanceof NotFoundException) {
throw new UnauthorizedException('用户名或密码错误');
}
throw error;
}
}

async refreshToken(refresh_token: string) {
try {
// 如果refresh_token为空或无效格式,返回400错误
if (!refresh_token || typeof refresh_token !== 'string') {
throw new BadRequestException('无效的refresh_token格式');
}

// 验证token
const decoded = await this.jwtService.verifyAsync(refresh_token);

// 确保用户存在
const user = await this.usersService.findOne(decoded.sub);
if (!user) {
throw new NotFoundException('用户不存在');
}

const access_token = await this.jwtService.signAsync(
{ id: decoded.sub, userName: user.userName },
Expand All @@ -47,8 +66,13 @@ export class AuthService {
{ expiresIn: '7d' },
);
return { refresh_token: newRefresh_token, access_token };
} catch {
throw new UnauthorizedException('refresh_token已过期');
} catch (error) {
// 区分不同类型的错误
if (error instanceof BadRequestException || error instanceof NotFoundException) {
throw error; // 重新抛出原始错误
}
// JWT相关错误统一处理为401未授权
throw new UnauthorizedException('refresh_token无效或已过期');
}
}
}
4 changes: 4 additions & 0 deletions src/usercenter/dto/create-usercenter.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ export class CreateUsercenterDto {
@Min(0, { message: '性别值必须大于等于0' })
@Max(2, { message: '性别值必须小于等于2' })
sex?: number; // 性别,0未知,1男,2女

@IsOptional()
@IsString()
avatar?: string; // 用户头像
}
7 changes: 7 additions & 0 deletions src/usercenter/entities/usercenter.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ export class UserEntity {
})
userPassword: string; // 用户密码

@Column({
type: 'varchar',
name: 'avatar',
default: ''
})
avatar: string; // 头像

// 关联文章,一个用户可以有多篇文章
@OneToMany(() => ArticleEntity, (article) => article.user)
articles: ArticleEntity[];
Expand Down
15 changes: 9 additions & 6 deletions src/usercenter/usercenter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
BadRequestException,
Injectable,
InternalServerErrorException,
NotFoundException,
ConflictException
} from '@nestjs/common';
import { CreateUsercenterDto } from './dto/create-usercenter.dto';
import { UpdateUsercenterDto } from './dto/update-usercenter.dto';
Expand Down Expand Up @@ -29,15 +31,15 @@ export class UsercenterService {
where: [{ userName: createUsercenterDto.userName }],
});
if (existingUser) {
throw new BadRequestException('用户名已存在');
throw new ConflictException('用户名已存在');
}

// 检查邮箱是否已存在
const existingEmail = await this.userRepository.findOne({
where: [{ userEmail: createUsercenterDto.userEmail }],
});
if (existingEmail) {
throw new BadRequestException('邮箱已被注册');
throw new ConflictException('邮箱已被注册');
}

await validateOrReject(createUsercenterDto);
Expand Down Expand Up @@ -74,8 +76,9 @@ export class UsercenterService {
take: limit, // 每页记录数
order: { createTime: 'DESC' }, // 按创建时间倒序排列
});
// 如果没有数据,返回空数组,不抛出异常
if (total === 0) {
throw new InternalServerErrorException(`数量为0`);
return { total: 0, data: [], message: '没有数据', status: 200 };
}

return { total, data, message: '查询成功', status: 200 };
Expand Down Expand Up @@ -146,7 +149,7 @@ export class UsercenterService {
});
}
if (!user) {
throw new InternalServerErrorException(`未找到匹配 ${identifier} 的记录`);
throw new NotFoundException(`未找到匹配 ${identifier} 的记录`);
}

return user;
Expand All @@ -155,7 +158,7 @@ export class UsercenterService {
async update(id: number, updateUsercenterDto: UpdateUsercenterDto) {
const user = await this.userRepository.findOneBy({ userId: id });
if (!user) {
throw new InternalServerErrorException(`用户 ID 为 ${id} 的记录不存在`);
throw new NotFoundException(`用户 ID 为 ${id} 的记录不存在`);
}

// 合并更新数据
Expand All @@ -172,7 +175,7 @@ export class UsercenterService {
async remove(id: number) {
const user = await this.userRepository.findOneBy({ userId: id });
if (!user) {
throw new InternalServerErrorException(`用户 ID 为 ${id} 的记录不存在`);
throw new NotFoundException(`用户 ID 为 ${id} 的记录不存在`);
}
const data = await this.userRepository.delete(id);

Expand Down
Loading