Skip to content

Commit

Permalink
Merge pull request #93 from zhumeisongsong/feature/fix-user-app-serve…
Browse files Browse the repository at this point in the history
…-error

fix: 🐛 serve error of users app
  • Loading branch information
zhumeisongsong authored Dec 3, 2024
2 parents 9e117d1 + 5ca9dc8 commit 1ac6065
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 20 deletions.
2 changes: 2 additions & 0 deletions apps/users/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
authConfig,
} from '@shared/config';
import { UsersModule } from '@users/interface-adapters';
import { AuthModule } from '@auth/interface-adapters';

import { AppController } from './app.controller';
import { AppService } from './app.service';
Expand All @@ -18,6 +19,7 @@ import { AppService } from './app.service';
load: [userAppConfig, databaseConfig, awsConfig, authConfig],
}),
UsersModule,
AuthModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
5 changes: 3 additions & 2 deletions libs/auth/application/src/lib/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Logger, UnauthorizedException } from '@nestjs/common';
import { Injectable, Logger, UnauthorizedException } from '@nestjs/common';
import { UsersService } from '@users/application';
import { JwtService } from '@nestjs/jwt';
import { AwsCognitoService } from '@shared/infrastructure-aws-cognito';

// Implement the authentication logic
@Injectable()
export class AuthService {
private readonly logger = new Logger(AuthService.name);
constructor(
private awsCognitoService: AwsCognitoService,
private usersService: UsersService,
private jwtService: JwtService,
private readonly logger = new Logger(AuthService.name),
) {}

// Retrieving a user and verifying the password
Expand Down
39 changes: 33 additions & 6 deletions libs/auth/interface-adapters/src/lib/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
import { AuthService } from '@auth/application';
import { Module } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MongooseModule } from '@nestjs/mongoose';
import { JwtModule, JwtService } from '@nestjs/jwt';
import { AwsCognitoService } from '@shared/infrastructure-aws-cognito';
import { DatabaseModule } from '@shared/infrastructure-mongoose';
import { GetUserUseCase, UsersService } from '@users/application';
import { USERS_REPOSITORY } from '@users/domain';
import {
MongooseUsersRepository,
UserDocument,
UserSchema,
} from '@users/infrastructure-mongoose';
import { UsersModule } from '@users/interface-adapters';

import { AuthResolver } from './resolver/auth.resolver';

@Module({
providers: [
AuthResolver,
AuthService,
AwsCognitoService,
UsersService,
JwtService,
GetUserUseCase,
{
provide: USERS_REPOSITORY,
useClass: MongooseUsersRepository,
},
],
imports: [
UsersModule,
JwtModule.registerAsync({
global: true,
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => {
const jwtConfig = configService.get('jwt');
const authConfig = configService.get('auth');

return {
secret: jwtConfig.secret,
secret: authConfig.secret,
signOptions: { expiresIn: '60s' },
};
},
inject: [ConfigService],
}),
DatabaseModule,
MongooseModule.forFeature([
{ name: UserDocument.name, schema: UserSchema },
]),
],
providers: [AuthResolver, AuthService],
exports: [AuthService],
})
export class AuthModule {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Field } from '@nestjs/graphql';
import { Field, InputType } from '@nestjs/graphql';

@InputType()
export class SignInInputDto {
@Field()
email!: string;
Expand Down
14 changes: 9 additions & 5 deletions libs/auth/interface-adapters/src/lib/resolver/auth.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { AuthService } from '@auth/application';
import { Resolver, Mutation, Args } from '@nestjs/graphql';
import { Resolver, Mutation, Args, Field, ObjectType } from '@nestjs/graphql';

import { SignInInputDto } from '../dto/sign-in-input.dto';

@ObjectType()
export class Response {
@Field()
accessToken!: string;
}

// Expose the authentication endpoints
@Resolver()
export class AuthResolver {
constructor(private authService: AuthService) {}

@Mutation()
@Mutation(() => Response)
async signIn(
@Args({ name: 'signInInput', type: () => SignInInputDto })
signInInput: SignInInputDto,
): Promise<{
accessToken: string;
}> {
): Promise<Response> {
return this.authService.signIn(signInInput.email, signInInput.password);
}
}
12 changes: 6 additions & 6 deletions libs/users/interface-adapters/src/lib/dto/user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsEmail, IsString } from 'class-validator';
import { IsEmail, IsOptional } from 'class-validator';
import { Field, ID, ObjectType } from '@nestjs/graphql';

@ObjectType()
Expand All @@ -10,12 +10,12 @@ export class UserDto {
@IsEmail()
email: string;

@Field()
@IsString()
firstName?: string | null;
@Field(() => String, { nullable: true })
@IsOptional()
firstName: string | null;

@Field()
@IsString()
@Field(() => String, { nullable: true })
@IsOptional()
lastName: string | null;

constructor(
Expand Down

0 comments on commit 1ac6065

Please sign in to comment.