📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
import { Injectable, UnauthorizedException } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { PassportStrategy } from '@nestjs/passport'
import { Strategy, ExtractJwt } from 'passport-jwt'
import { JWT_ACCESS_TOKEN_SECRET } from '../../common/constants'
import { UserService } from '../../user/user.service'
import { IJwtPayload } from '../interfaces'
@Injectable()
export class JwtAccessTokenStrategy extends PassportStrategy(
Strategy,
'jwt-access-token',
) {
constructor(
public readonly configService: ConfigService,
private readonly userService: UserService,
) {
super({
jwtFromRequest: ExtractJwt.fromExtractors([
ExtractJwt.fromAuthHeaderAsBearerToken(),
(request: any) => request?.cookies?.token,
]),
ignoreExpiration: false,
secretOrKey: JWT_ACCESS_TOKEN_SECRET,
})
}
public async validate(payload: IJwtPayload) {
const user = await this.userService.findUserById(payload.sub)
if (!user) {
throw new UnauthorizedException()
}
return {
...user,
isSecondFactorAuthenticated: payload.isSecondFactorAuthenticated,
}
}
}