/* * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) * * SPDX-License-Identifier: AGPL-3.0-only */ import { Strategy } from 'passport-http-bearer'; import { PassportStrategy } from '@nestjs/passport'; import { Injectable, UnauthorizedException } from '@nestjs/common'; import { AuthService } from './auth.service'; import { User } from '../users/user.entity'; @Injectable() export class TokenStrategy extends PassportStrategy(Strategy, 'token') { constructor(private authService: AuthService) { super(); } async validate(token: string): Promise { const user = await this.authService.validateToken(token); if (!user) { throw new UnauthorizedException(); } return user; } }