Chat / backend /src /modules /auth /jwt.strategy.ts
kishankrish19's picture
Deploy Stillwave fullstack clean
b0798af
Raw History Blame Contribute Delete
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';
@Injectable()
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,
};
}
}