Skip to content

Commit

Permalink
Merge pull request #61 from leephan2k1/feature/auth
Browse files Browse the repository at this point in the history
handle get current user info
  • Loading branch information
leephan2k1 authored Aug 3, 2024
2 parents af88c3e + ed804bf commit bed4b06
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/src/main",
"dev": "nest start --watch",
"debug": "nest start --debug --watch",
"prod": "node dist/src/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest --verbose --rootDir=test",
"test:watch": "jest --watch --rootDir=test",
Expand Down
42 changes: 26 additions & 16 deletions src/graphql/resolvers/user.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import { User } from '~/models/user.model';
import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';
import { Inject, UseFilters, UseGuards } from '@nestjs/common';
import { Query, Resolver } from '@nestjs/graphql';
import { Anime } from '~/models';
import { GqlUser } from '~/common/decorators/gql-user.decorator';
import { UserDto } from '~/common/dtos/user-dtos/user.dto';
import { IUserService } from '~/contracts/services/user-service.interface';
import { JwtAuthGuard } from '~/guards';
import { User } from '~/models/user.model';
import { UserActions } from '../types/enums/actions.enum';
import { UnauthorizedExceptionFilter } from '~/common/filters/unauthorized-exception.filter';
import { CurrentUserResultUnion } from '../types/dtos/users/get-current-user-dto.repsonse';

@Resolver()
@Resolver(() => UserDto)
@UseFilters(UnauthorizedExceptionFilter)
export class UserResolver {
@Query((returns) => User, { name: 'User' })
getUser() {
return {
id: 1,
userName: 'userName',
};
}
constructor(
@Inject(IUserService)
private readonly userService: IUserService,

@InjectMapper()
private readonly mapper: Mapper,
) {}

@Query((returns) => Anime, { name: 'Anime' })
test() {
return {
id: 1,
userName: 'userName',
};
@Query((returns) => [CurrentUserResultUnion], {
name: UserActions.CurrentUser,
})
@UseGuards(JwtAuthGuard)
getUserInfo(@GqlUser() currentUser: User) {
return [this.mapper.map(currentUser, User, UserDto)];
}
}
8 changes: 8 additions & 0 deletions src/graphql/types/dtos/users/get-current-user-dto.repsonse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createUnionType } from '@nestjs/graphql';
import { UnauthorizedError } from '~/common/dtos/common-dtos/unauthorized-error.dto';
import { UserDto } from '~/common/dtos/user-dtos/user.dto';

export const CurrentUserResultUnion = createUnionType({
name: 'CurrentUserResultUnion',
types: () => [UserDto, UnauthorizedError],
});
4 changes: 4 additions & 0 deletions src/graphql/types/enums/actions.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ export enum AuthActions {
RefreshToken = 'RefreshToken',
SignUpExternal = 'SignUpExternal',
}

export enum UserActions {
CurrentUser = 'CurrentUser',
}
8 changes: 7 additions & 1 deletion src/modules/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { UserProfile } from '~/common/mapper-profiles/user-profile';
import { IUserRepository } from '~/contracts/repositories/user-repository.interface';
import { IUserService } from '~/contracts/services/user-service.interface';
import { UserResolver } from '~/graphql/resolvers/user.resolver';
import { SocialProvider } from '~/models/social-provider.model';
import { User } from '~/models/user.model';
import { UserRepository } from '~/repositories/user.repository';
Expand All @@ -19,7 +20,12 @@ const userRepositoryProvider: Provider = {
@Module({
imports: [TypeOrmModule.forFeature([User, SocialProvider])],

providers: [userServiceProvider, userRepositoryProvider, UserProfile],
providers: [
UserResolver,
userServiceProvider,
userRepositoryProvider,
UserProfile,
],

exports: [userServiceProvider, userRepositoryProvider],
})
Expand Down

0 comments on commit bed4b06

Please sign in to comment.