11import { UsercenterService } from './../usercenter/usercenter.service' ;
2- import { Injectable , UnauthorizedException } from '@nestjs/common' ;
2+ import { Injectable , UnauthorizedException , NotFoundException , BadRequestException } from '@nestjs/common' ;
33import { JwtService } from '@nestjs/jwt' ;
44
55@Injectable ( )
@@ -10,32 +10,51 @@ export class AuthService {
1010 ) { }
1111
1212 async signIn ( username : string , pass : string ) : Promise < any > {
13- const user = await this . usersService . findOne ( username ) ;
13+ try {
14+ const user = await this . usersService . findOne ( username ) ;
1415
15- // 直接使用用户密码进行验证
16- if ( user ?. userPassword !== pass ) {
17- throw new UnauthorizedException ( ) ;
18- }
16+ // 直接使用用户密码进行验证
17+ if ( user ?. userPassword !== pass ) {
18+ throw new UnauthorizedException ( '用户名或密码错误' ) ;
19+ }
1920
20- const payload = { sub : user . userId , username : user . userName } ;
21- const refreshPayload = { sub : user . userId } ;
22-
23- return {
24- userId : user . userId ,
25- access_token : await this . jwtService . signAsync ( payload , {
26- expiresIn : '7d' ,
27- } ) ,
28- refresh_token : await this . jwtService . signAsync ( refreshPayload , {
29- expiresIn : '7d' ,
30- } ) ,
31- } ;
21+ const payload = { sub : user . userId , username : user . userName } ;
22+ const refreshPayload = { sub : user . userId } ;
23+
24+ return {
25+ userId : user . userId ,
26+ access_token : await this . jwtService . signAsync ( payload , {
27+ expiresIn : '7d' ,
28+ } ) ,
29+ refresh_token : await this . jwtService . signAsync ( refreshPayload , {
30+ expiresIn : '7d' ,
31+ } ) ,
32+ } ;
33+ } catch ( error ) {
34+ // 捕获findOne方法抛出的NotFoundException异常并将其转换为UnauthorizedException
35+ // 这样用户名不存在和密码错误都返回相同的401状态码
36+ if ( error instanceof NotFoundException ) {
37+ throw new UnauthorizedException ( '用户名或密码错误' ) ;
38+ }
39+ throw error ;
40+ }
3241 }
3342
3443 async refreshToken ( refresh_token : string ) {
3544 try {
45+ // 如果refresh_token为空或无效格式,返回400错误
46+ if ( ! refresh_token || typeof refresh_token !== 'string' ) {
47+ throw new BadRequestException ( '无效的refresh_token格式' ) ;
48+ }
49+
50+ // 验证token
3651 const decoded = await this . jwtService . verifyAsync ( refresh_token ) ;
3752
53+ // 确保用户存在
3854 const user = await this . usersService . findOne ( decoded . sub ) ;
55+ if ( ! user ) {
56+ throw new NotFoundException ( '用户不存在' ) ;
57+ }
3958
4059 const access_token = await this . jwtService . signAsync (
4160 { id : decoded . sub , userName : user . userName } ,
@@ -47,8 +66,13 @@ export class AuthService {
4766 { expiresIn : '7d' } ,
4867 ) ;
4968 return { refresh_token : newRefresh_token , access_token } ;
50- } catch {
51- throw new UnauthorizedException ( 'refresh_token已过期' ) ;
69+ } catch ( error ) {
70+ // 区分不同类型的错误
71+ if ( error instanceof BadRequestException || error instanceof NotFoundException ) {
72+ throw error ; // 重新抛出原始错误
73+ }
74+ // JWT相关错误统一处理为401未授权
75+ throw new UnauthorizedException ( 'refresh_token无效或已过期' ) ;
5276 }
5377 }
5478}
0 commit comments