mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-10-29 17:40:28 +00:00
34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
|
import { Repository } from 'typeorm';
|
|
import { JwtPayloadDto } from '../../../api-v1/auth/dto/jwt-payload.dto';
|
|
import { UserEntity } from '../../../api-v1/user/entities/user.entity';
|
|
import { jwtSecret } from '../../../constants/jwt.constant';
|
|
|
|
@Injectable()
|
|
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
|
constructor(
|
|
@InjectRepository(UserEntity)
|
|
private usersRepository: Repository<UserEntity>,
|
|
) {
|
|
super({
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
ignoreExpiration: false,
|
|
secretOrKey: jwtSecret,
|
|
});
|
|
}
|
|
|
|
async validate(payload: JwtPayloadDto) {
|
|
const { userId } = payload;
|
|
const user = await this.usersRepository.findOne({ id: userId });
|
|
|
|
if (!user) {
|
|
throw new UnauthorizedException('Failure to validate JWT payload');
|
|
}
|
|
|
|
return user;
|
|
}
|
|
}
|