Skip to content

Commit

Permalink
feat: add ctx-user-id decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Feb 12, 2024
1 parent c4fe5f9 commit b91f969
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions libs/api/auth/data-access/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './lib/api-auth-data-access.module'
export * from './lib/api-auth.service'
export * from './lib/contexts/ctx-user'
export * from './lib/contexts/ctx-user-id'
export * from './lib/dto/login.input'
export * from './lib/dto/register.input'
export * from './lib/guards/api-anon-jwt-guard'
Expand Down
7 changes: 7 additions & 0 deletions libs/api/auth/data-access/src/lib/contexts/ctx-user-id.ts
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 CtxUserId = createParamDecorator((data: unknown, context: ExecutionContext) => {
const ctx = GqlExecutionContext.create(context)
return ctx.getContext().req.user?.id
})
18 changes: 9 additions & 9 deletions libs/api/identity/feature/src/lib/api-user-identity.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UseGuards } from '@nestjs/common'
import { Args, Context, Mutation, Query, Resolver } from '@nestjs/graphql'
import { ApiAuthGraphQLUserGuard, CtxUser } from '@pubkey-stack/api-auth-data-access'
import { ApiAuthGraphQLUserGuard, CtxUserId } from '@pubkey-stack/api-auth-data-access'
import { BaseContext } from '@pubkey-stack/api-core-data-access'
import {
ApiIdentityService,
Expand All @@ -18,31 +18,31 @@ export class ApiUserIdentityResolver {
constructor(private readonly service: ApiIdentityService) {}

@Mutation(() => Boolean, { nullable: true })
userDeleteIdentity(@CtxUser() user: { id: string }, @Args('identityId') identityId: string) {
return this.service.user.deleteIdentity(user.id, identityId)
userDeleteIdentity(@CtxUserId() userId: string, @Args('identityId') identityId: string) {
return this.service.user.deleteIdentity(userId, identityId)
}

@Query(() => IdentityChallenge, { nullable: true })
userRequestIdentityChallenge(
@Context() ctx: BaseContext,
@CtxUser() user: { id: string },
@CtxUserId() userId: string,
@Args('input') input: RequestIdentityChallengeInput,
) {
return this.service.user.requestIdentityChallenge(ctx, user.id, input)
return this.service.user.requestIdentityChallenge(ctx, userId, input)
}

@Mutation(() => Identity, { nullable: true })
userLinkIdentity(@CtxUser() user: { id: string }, @Args('input') input: LinkIdentityInput) {
return this.service.user.linkIdentity(user.id, input)
userLinkIdentity(@CtxUserId() userId: string, @Args('input') input: LinkIdentityInput) {
return this.service.user.linkIdentity(userId, input)
}

@Mutation(() => IdentityChallenge, { nullable: true })
userVerifyIdentityChallenge(
@Context() ctx: BaseContext,
@CtxUser() user: { id: string },
@CtxUserId() userId: string,
@Args('input') input: VerifyIdentityChallengeInput,
) {
return this.service.user.verifyIdentityChallenge(ctx, user.id, input)
return this.service.user.verifyIdentityChallenge(ctx, userId, input)
}

@Query(() => [Identity], { nullable: true })
Expand Down
6 changes: 3 additions & 3 deletions libs/api/user/feature/src/lib/api-user-user.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UseGuards } from '@nestjs/common'
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'
import { ApiAuthGraphQLUserGuard, CtxUser } from '@pubkey-stack/api-auth-data-access'
import { ApiAuthGraphQLUserGuard, CtxUserId } from '@pubkey-stack/api-auth-data-access'
import {
ApiUserService,
User,
Expand All @@ -25,7 +25,7 @@ export class ApiUserUserResolver {
}

@Mutation(() => User, { nullable: true })
userUpdateUser(@CtxUser() user: User, @Args('input') input: UserUpdateUserInput) {
return this.service.user.updateUser(user.id as string, input)
userUpdateUser(@CtxUserId() userId: string, @Args('input') input: UserUpdateUserInput) {
return this.service.user.updateUser(userId as string, input)
}
}

0 comments on commit b91f969

Please sign in to comment.