Skip to main content
Glama
jwt.strategy.ts1.48 kB
import { Injectable, UnauthorizedException } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { ConfigService } from '@nestjs/config'; import { UserService } from '../services/user.service'; import { User } from '../../../database/entities/user.entity'; export interface JwtPayload { sub: string; // 用户ID email: string; roles: string[]; permissions: string[]; iat?: number; exp?: number; } @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor( private readonly configService: ConfigService, private readonly userService: UserService, ) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey: configService.get<string>('JWT_SECRET') || 'default-secret-key', }); } async validate(payload: JwtPayload): Promise<User> { const { sub: userId } = payload; try { const user = await this.userService.findUserById(userId); if (!user) { throw new UnauthorizedException('用户不存在'); } if (!user.isActive) { throw new UnauthorizedException('用户账户已被禁用'); } if (user.isLocked) { throw new UnauthorizedException('用户账户已被锁定'); } return user; } catch (error) { throw new UnauthorizedException('无效的访问令牌'); } } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/zaizaizhao/mcp-swagger-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server