|
@ -1,33 +1,46 @@ |
|
|
import { UserService } from '@ghostfolio/api/app/user/user.service'; |
|
|
import { UserService } from '@ghostfolio/api/app/user/user.service'; |
|
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service'; |
|
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service'; |
|
|
import { PrismaService } from '@ghostfolio/api/services/prisma.service'; |
|
|
import { PrismaService } from '@ghostfolio/api/services/prisma.service'; |
|
|
|
|
|
import { HEADER_KEY_TIMEZONE } from '@ghostfolio/common/config'; |
|
|
import { Injectable, UnauthorizedException } from '@nestjs/common'; |
|
|
import { Injectable, UnauthorizedException } from '@nestjs/common'; |
|
|
import { PassportStrategy } from '@nestjs/passport'; |
|
|
import { PassportStrategy } from '@nestjs/passport'; |
|
|
|
|
|
import * as countriesAndTimezones from 'countries-and-timezones'; |
|
|
import { ExtractJwt, Strategy } from 'passport-jwt'; |
|
|
import { ExtractJwt, Strategy } from 'passport-jwt'; |
|
|
|
|
|
|
|
|
@Injectable() |
|
|
@Injectable() |
|
|
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') { |
|
|
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') { |
|
|
public constructor( |
|
|
public constructor( |
|
|
readonly configurationService: ConfigurationService, |
|
|
private readonly configurationService: ConfigurationService, |
|
|
private readonly prismaService: PrismaService, |
|
|
private readonly prismaService: PrismaService, |
|
|
private readonly userService: UserService |
|
|
private readonly userService: UserService |
|
|
) { |
|
|
) { |
|
|
super({ |
|
|
super({ |
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), |
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), |
|
|
|
|
|
passReqToCallback: true, |
|
|
secretOrKey: configurationService.get('JWT_SECRET_KEY') |
|
|
secretOrKey: configurationService.get('JWT_SECRET_KEY') |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async validate({ id }: { id: string }) { |
|
|
public async validate(request: Request, { id }: { id: string }) { |
|
|
try { |
|
|
try { |
|
|
|
|
|
const timezone = request.headers[HEADER_KEY_TIMEZONE.toLowerCase()]; |
|
|
const user = await this.userService.user({ id }); |
|
|
const user = await this.userService.user({ id }); |
|
|
|
|
|
|
|
|
if (user) { |
|
|
if (user) { |
|
|
|
|
|
if (this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) { |
|
|
|
|
|
const country = |
|
|
|
|
|
countriesAndTimezones.getCountryForTimezone(timezone)?.id; |
|
|
|
|
|
|
|
|
await this.prismaService.analytics.upsert({ |
|
|
await this.prismaService.analytics.upsert({ |
|
|
create: { User: { connect: { id: user.id } } }, |
|
|
create: { country, User: { connect: { id: user.id } } }, |
|
|
update: { activityCount: { increment: 1 }, updatedAt: new Date() }, |
|
|
update: { |
|
|
|
|
|
country, |
|
|
|
|
|
activityCount: { increment: 1 }, |
|
|
|
|
|
updatedAt: new Date() |
|
|
|
|
|
}, |
|
|
where: { userId: user.id } |
|
|
where: { userId: user.id } |
|
|
}); |
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return user; |
|
|
return user; |
|
|
} else { |
|
|
} else { |
|
|