|
@@ -3,13 +3,11 @@ import { PassportStrategy } from '@nestjs/passport'
|
|
|
import { Inject, Injectable, Logger, UnauthorizedException } from '@nestjs/common'
|
|
import { Inject, Injectable, Logger, UnauthorizedException } from '@nestjs/common'
|
|
|
import jwtconfig from './jwt.config'
|
|
import jwtconfig from './jwt.config'
|
|
|
import { ConfigType } from '@nestjs/config'
|
|
import { ConfigType } from '@nestjs/config'
|
|
|
-import { Redis } from 'ioredis'
|
|
|
|
|
import { UsersService } from 'src/users/users.service'
|
|
import { UsersService } from 'src/users/users.service'
|
|
|
import { Role } from 'src/model/role.enum'
|
|
import { Role } from 'src/model/role.enum'
|
|
|
|
|
|
|
|
@Injectable()
|
|
@Injectable()
|
|
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
|
- private redis: Redis
|
|
|
|
|
constructor(
|
|
constructor(
|
|
|
@Inject(jwtconfig.KEY)
|
|
@Inject(jwtconfig.KEY)
|
|
|
private readonly jwtConfiguration: ConfigType<typeof jwtconfig>,
|
|
private readonly jwtConfiguration: ConfigType<typeof jwtconfig>,
|
|
@@ -20,7 +18,6 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
|
ignoreExpiration: false,
|
|
ignoreExpiration: false,
|
|
|
secretOrKey: jwtConfiguration.secret
|
|
secretOrKey: jwtConfiguration.secret
|
|
|
})
|
|
})
|
|
|
- this.redis = new Redis(process.env.REDIS_URI)
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async validate(payload: any) {
|
|
async validate(payload: any) {
|
|
@@ -28,14 +25,14 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
|
if (!user) {
|
|
if (!user) {
|
|
|
throw new UnauthorizedException('User not found')
|
|
throw new UnauthorizedException('User not found')
|
|
|
}
|
|
}
|
|
|
- if (!(payload.roles.includes(Role.Admin) || payload.roles.includes(Role.Api))) {
|
|
|
|
|
- if (!user.iat) {
|
|
|
|
|
- throw new UnauthorizedException('用户身份已过期,请重新登录')
|
|
|
|
|
- }
|
|
|
|
|
- if (payload.iat < user.iat) {
|
|
|
|
|
- throw new UnauthorizedException('用户身份已过期,请重新登录')
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // if (!(payload.roles.includes(Role.Admin) || payload.roles.includes(Role.Api))) {
|
|
|
|
|
+ // if (!user.iat) {
|
|
|
|
|
+ // throw new UnauthorizedException('用户身份已过期,请重新登录')
|
|
|
|
|
+ // }
|
|
|
|
|
+ // if (payload.iat < user.iat) {
|
|
|
|
|
+ // throw new UnauthorizedException('用户身份已过期,请重新登录')
|
|
|
|
|
+ // }
|
|
|
|
|
+ // }
|
|
|
return {
|
|
return {
|
|
|
id: payload.sub,
|
|
id: payload.sub,
|
|
|
userId: payload.sub,
|
|
userId: payload.sub,
|