Spaces:
Sleeping
Sleeping
Download backend/src/modules/auth/jwt.strategy.ts from kishankrish19/Chat: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/kishankrish19/Chat/resolve/main/backend/src/modules/auth/jwt.strategy.ts
- Command line
-
hf download hf://spaces/kishankrish19/Chat/backend/src/modules/auth/jwt.strategy.ts
-
curl -L -o jwt.strategy.ts https://huggingface.co/spaces/kishankrish19/Chat/resolve/main/backend/src/modules/auth/jwt.strategy.ts
1.06 kB
| import { Injectable, UnauthorizedException } from '@nestjs/common'; | |
| import { PassportStrategy } from '@nestjs/passport'; | |
| import { ExtractJwt, Strategy } from 'passport-jwt'; | |
| import { Store, secret } from '../../core/store'; | |
| () | |
| export class JwtStrategy extends PassportStrategy(Strategy) { | |
| constructor(private readonly store: Store) { | |
| super({ | |
| jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | |
| ignoreExpiration: false, | |
| secretOrKey: secret('SESSION_SIGNING_KEY'), | |
| }); | |
| } | |
| async validate(payload: { sub: string; sid: string; type: string }) { | |
| const session = this.store.get('sessions', payload.sid || ''); | |
| const user = this.store.get('users', payload.sub || ''); | |
| if ( | |
| payload.type !== 'access' || | |
| !session || | |
| !user || | |
| session.user_id !== user.id || | |
| session.expires < Date.now() | |
| ) | |
| throw new UnauthorizedException('Session expired'); | |
| return { | |
| userId: user.id, | |
| sessionId: session.id, | |
| email: user.email, | |
| role: user.account_type, | |
| }; | |
| } | |
| } | |