Skip to content

Commit

Permalink
chore: adding project test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sma1lboy committed Oct 23, 2024
1 parent 16fe2ef commit 1ff324d
Show file tree
Hide file tree
Showing 8 changed files with 494 additions and 125 deletions.
103 changes: 51 additions & 52 deletions backend/src/guard/project.guard.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
import {
Injectable,
CanActivate,
ExecutionContext,
UnauthorizedException,
} from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
Injectable,
CanActivate,
ExecutionContext,
UnauthorizedException,
} from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { JwtService } from '@nestjs/jwt';
import { ProjectsService } from '../project/project.service';

@Injectable()
export class ProjectGuard implements CanActivate {
constructor(
private readonly projectsService: ProjectsService,
private readonly jwtService: JwtService,
) {}

async canActivate(context: ExecutionContext): Promise<boolean> {
const gqlContext = GqlExecutionContext.create(context);
const request = gqlContext.getContext().req;

// Extract the authorization header
const authHeader = request.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
throw new UnauthorizedException('Authorization token is missing');
}

// Decode the token to get user information
const token = authHeader.split(' ')[1];
let user: any;
try {
user = this.jwtService.verify(token);
} catch (error) {
throw new UnauthorizedException('Invalid token');
}

// Extract projectId from the request arguments
const args = gqlContext.getArgs();
const { projectId } = args;

// Fetch the project and check if the userId matches the project's userId
const project = await this.projectsService.getProjectById(projectId);
if (!project) {
throw new UnauthorizedException('Project not found');
}

//To do: In the feature when we need allow teams add check here

if (project.user_id !== user.userId) {
throw new UnauthorizedException('User is not the owner of the project');
}

return true;
import { ProjectService } from '../project/project.service';

@Injectable()
export class ProjectGuard implements CanActivate {
constructor(
private readonly projectsService: ProjectService,
private readonly jwtService: JwtService,
) {}

async canActivate(context: ExecutionContext): Promise<boolean> {
const gqlContext = GqlExecutionContext.create(context);
const request = gqlContext.getContext().req;

// Extract the authorization header
const authHeader = request.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
throw new UnauthorizedException('Authorization token is missing');
}

// Decode the token to get user information
const token = authHeader.split(' ')[1];
let user: any;
try {
user = this.jwtService.verify(token);
} catch (error) {
throw new UnauthorizedException('Invalid token');
}

// Extract projectId from the request arguments
const args = gqlContext.getArgs();
const { projectId } = args;

// Fetch the project and check if the userId matches the project's userId
const project = await this.projectsService.getProjectById(projectId);
if (!project) {
throw new UnauthorizedException('Project not found');
}

//To do: In the feature when we need allow teams add check here

if (project.user_id !== user.userId) {
throw new UnauthorizedException('User is not the owner of the project');
}

return true;
}
}
Loading

0 comments on commit 1ff324d

Please sign in to comment.