-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
236 additions
and
40 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
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,39 @@ | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { TestingModule } from '@nestjs/testing'; | ||
import { createTestingModuleWithValidation } from '@bitsacco/testing'; | ||
|
||
import { AuthController } from './auth.controller'; | ||
import { AuthService } from './auth.service'; | ||
|
||
describe('AuthController', () => { | ||
let controller: AuthController; | ||
let authService: AuthService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await createTestingModuleWithValidation({ | ||
controllers: [AuthController], | ||
providers: [ | ||
ConfigService, | ||
{ | ||
provide: AuthService, | ||
useValue: { | ||
loginUser: jest.fn(), | ||
registerUser: jest.fn(), | ||
verifyUser: jest.fn(), | ||
authenticate: jest.fn(), | ||
}, | ||
}, | ||
JwtService, | ||
], | ||
}); | ||
|
||
controller = module.get<AuthController>(AuthController); | ||
authService = module.get<AuthService>(AuthService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
expect(authService).toBeDefined(); | ||
}); | ||
}); |
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,48 @@ | ||
import { type Response } from 'express'; | ||
import { firstValueFrom } from 'rxjs'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { Body, Controller, Logger, Post, Res } from '@nestjs/common'; | ||
import { ApiBody, ApiOperation } from '@nestjs/swagger'; | ||
import { AuthTokenPayload, LoginUserRequestDto } from '@bitsacco/common'; | ||
import { AuthService } from './auth.service'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
private readonly logger = new Logger(AuthController.name); | ||
|
||
constructor( | ||
private readonly configService: ConfigService, | ||
private readonly authService: AuthService, | ||
private readonly jwtService: JwtService, | ||
) { | ||
this.logger.log('AuthController initialized'); | ||
} | ||
|
||
@Post('login') | ||
@ApiOperation({ summary: 'Login user' }) | ||
@ApiBody({ | ||
type: LoginUserRequestDto, | ||
}) | ||
async login( | ||
@Body() req: LoginUserRequestDto, | ||
@Res({ passthrough: true }) res: Response, | ||
) { | ||
return firstValueFrom(this.authService.loginUser(req)).then( | ||
(authResponse) => { | ||
const { expires } = this.jwtService.decode<AuthTokenPayload>( | ||
authResponse.token, | ||
); | ||
|
||
const env = this.configService.get('NODE_ENV'); | ||
res.cookie('Authentication', authResponse.token, { | ||
httpOnly: true, | ||
secure: env === 'production', | ||
expires, | ||
}); | ||
|
||
return authResponse; | ||
}, | ||
); | ||
} | ||
} |
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,34 @@ | ||
import { TestingModule } from '@nestjs/testing'; | ||
import { AuthServiceClient } from '@bitsacco/common'; | ||
import { createTestingModuleWithValidation } from '@bitsacco/testing'; | ||
import { ClientGrpc } from '@nestjs/microservices'; | ||
import { AuthService } from './auth.service'; | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
let serviceGenerator: ClientGrpc; | ||
let mockAuthServiceClient: Partial<AuthServiceClient>; | ||
|
||
beforeEach(async () => { | ||
serviceGenerator = { | ||
getService: jest.fn().mockReturnValue(mockAuthServiceClient), | ||
getClientByServiceName: jest.fn().mockReturnValue(mockAuthServiceClient), | ||
}; | ||
|
||
const module: TestingModule = await createTestingModuleWithValidation({ | ||
providers: [ | ||
{ | ||
provide: AuthService, | ||
useFactory: () => { | ||
return new AuthService(serviceGenerator); | ||
}, | ||
}, | ||
], | ||
}); | ||
service = module.get<AuthService>(AuthService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,37 @@ | ||
import { | ||
AUTH_SERVICE_NAME, | ||
AuthRequest, | ||
AuthServiceClient, | ||
LoginUserRequest, | ||
RegisterUserRequest, | ||
VerifyUserRequest, | ||
} from '@bitsacco/common'; | ||
import { Inject, Injectable, OnModuleInit } from '@nestjs/common'; | ||
import { type ClientGrpc } from '@nestjs/microservices'; | ||
|
||
@Injectable() | ||
export class AuthService implements OnModuleInit { | ||
private client: AuthServiceClient; | ||
|
||
constructor(@Inject(AUTH_SERVICE_NAME) private readonly grpc: ClientGrpc) {} | ||
|
||
onModuleInit() { | ||
this.client = this.grpc.getService<AuthServiceClient>(AUTH_SERVICE_NAME); | ||
} | ||
|
||
loginUser(req: LoginUserRequest) { | ||
return this.client.loginUser(req); | ||
} | ||
|
||
registerUser(req: RegisterUserRequest) { | ||
this.client.registerUser(req); | ||
} | ||
|
||
verifyUser(req: VerifyUserRequest) { | ||
this.client.verifyUser(req); | ||
} | ||
|
||
authenticate(req: AuthRequest) { | ||
this.client.authenticate(req); | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from './auth'; | ||
export * from './types'; | ||
export * from './logger'; | ||
export * from './utils'; | ||
|
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,6 @@ | ||
import { User } from './proto/auth'; | ||
|
||
export interface AuthTokenPayload { | ||
user: User; | ||
expires: Date; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './currency'; | ||
export { CustomStore } from './cache'; | ||
export { JwtAuthGuard } from './jwt-auth.guard'; |
Oops, something went wrong.