-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: type errors with my-server-api now using correct types and added…
… decorator to unwrap jwt for user query to do easier ssr
- Loading branch information
1 parent
86fa329
commit fc624ef
Showing
4 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
import { GqlExecutionContext } from '@nestjs/graphql'; | ||
|
||
export const CurrentUser = createParamDecorator((data: unknown, context: ExecutionContext) => { | ||
const ctx = GqlExecutionContext.create(context); | ||
return ctx.getContext().req.user; | ||
}); |
23 changes: 23 additions & 0 deletions
23
apps/my-backend/src/app/resources/user/user.interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { GqlExecutionContext } from '@nestjs/graphql'; | ||
import { HttpException, HttpStatus, Logger } from '@nestjs/common'; | ||
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
|
||
@Injectable() | ||
export class OnlySameUserByIdAllowed implements NestInterceptor { | ||
async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> { | ||
const context_ = GqlExecutionContext.create(context); | ||
const { req } = context_.getContext(); | ||
const requestedFromUserId = req.body?.variables?.args?.id; | ||
const requestedForUserId = req?.user?.id; | ||
try { | ||
if (requestedFromUserId === requestedForUserId) { | ||
return next.handle(); | ||
} else { | ||
throw new Error('Unauthorized'); | ||
} | ||
} catch { | ||
throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters