diff --git a/apps/users/src/app/app.module.ts b/apps/users/src/app/app.module.ts index bc5de0c..31f1953 100644 --- a/apps/users/src/app/app.module.ts +++ b/apps/users/src/app/app.module.ts @@ -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'; @@ -18,6 +19,7 @@ import { AppService } from './app.service'; load: [userAppConfig, databaseConfig, awsConfig, authConfig], }), UsersModule, + AuthModule, ], controllers: [AppController], providers: [AppService], diff --git a/libs/auth/application/src/lib/auth.service.ts b/libs/auth/application/src/lib/auth.service.ts index 9412771..7a0fd14 100644 --- a/libs/auth/application/src/lib/auth.service.ts +++ b/libs/auth/application/src/lib/auth.service.ts @@ -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 diff --git a/libs/auth/interface-adapters/src/lib/auth.module.ts b/libs/auth/interface-adapters/src/lib/auth.module.ts index f1d7b84..29ee385 100644 --- a/libs/auth/interface-adapters/src/lib/auth.module.ts +++ b/libs/auth/interface-adapters/src/lib/auth.module.ts @@ -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 {} diff --git a/libs/auth/interface-adapters/src/lib/dto/sign-in-input.dto.ts b/libs/auth/interface-adapters/src/lib/dto/sign-in-input.dto.ts index 34293f1..58f4ede 100644 --- a/libs/auth/interface-adapters/src/lib/dto/sign-in-input.dto.ts +++ b/libs/auth/interface-adapters/src/lib/dto/sign-in-input.dto.ts @@ -1,5 +1,6 @@ -import { Field } from '@nestjs/graphql'; +import { Field, InputType } from '@nestjs/graphql'; +@InputType() export class SignInInputDto { @Field() email!: string; diff --git a/libs/auth/interface-adapters/src/lib/resolver/auth.resolver.ts b/libs/auth/interface-adapters/src/lib/resolver/auth.resolver.ts index 39dad6e..2b58926 100644 --- a/libs/auth/interface-adapters/src/lib/resolver/auth.resolver.ts +++ b/libs/auth/interface-adapters/src/lib/resolver/auth.resolver.ts @@ -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 { return this.authService.signIn(signInInput.email, signInInput.password); } } diff --git a/libs/users/interface-adapters/src/lib/dto/user.dto.ts b/libs/users/interface-adapters/src/lib/dto/user.dto.ts index fd61646..108d6c7 100644 --- a/libs/users/interface-adapters/src/lib/dto/user.dto.ts +++ b/libs/users/interface-adapters/src/lib/dto/user.dto.ts @@ -1,4 +1,4 @@ -import { IsEmail, IsString } from 'class-validator'; +import { IsEmail, IsOptional } from 'class-validator'; import { Field, ID, ObjectType } from '@nestjs/graphql'; @ObjectType() @@ -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(