teableio/teable

View on GitHub
apps/nestjs-backend/src/features/share/guard/share-auth-local.guard.ts

Summary

Maintainability
A
0 mins
Test Coverage
import type { CanActivate, ExecutionContext } from '@nestjs/common';
import { BadRequestException, Injectable } from '@nestjs/common';
import { ShareAuthService } from '../share-auth.service';

@Injectable()
export class ShareAuthLocalGuard implements CanActivate {
  constructor(private readonly shareAuthService: ShareAuthService) {}

  async canActivate(context: ExecutionContext) {
    const req = context.switchToHttp().getRequest();
    const shareId = req.params.shareId;
    const password = req.body.password;
    const authShareId = await this.shareAuthService.authShareView(shareId, password);
    req.shareId = authShareId;
    req.password = password;
    if (!authShareId) {
      throw new BadRequestException('Incorrect password.');
    }
    return true;
  }
}