-
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
22 changed files
with
298 additions
and
74 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,90 @@ | ||
import { type Response } from 'express'; | ||
import { firstValueFrom, Observable } from 'rxjs'; | ||
import { Body, Controller, Logger, Post, Res } from '@nestjs/common'; | ||
import { ApiBody, ApiOperation } from '@nestjs/swagger'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { | ||
AuthRequestDto, | ||
AuthResponse, | ||
AuthTokenPayload, | ||
LoginUserRequestDto, | ||
RegisterUserRequestDto, | ||
VerifyUserRequestDto, | ||
} from '@bitsacco/common'; | ||
import { AuthService } from './auth.service'; | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
private readonly logger = new Logger(AuthController.name); | ||
|
||
constructor( | ||
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, | ||
) { | ||
const auth = this.authService.loginUser(req); | ||
return this.setAuthCookie(auth, res); | ||
} | ||
|
||
@Post('register') | ||
@ApiOperation({ summary: 'Register user' }) | ||
@ApiBody({ | ||
type: RegisterUserRequestDto, | ||
}) | ||
register(@Body() req: RegisterUserRequestDto) { | ||
return firstValueFrom(this.authService.registerUser(req)).then((user) => ({ | ||
user, | ||
})); | ||
} | ||
|
||
@Post('verify') | ||
@ApiOperation({ summary: 'Register user' }) | ||
@ApiBody({ | ||
type: VerifyUserRequestDto, | ||
}) | ||
verify(@Body() req: VerifyUserRequestDto) { | ||
return firstValueFrom(this.authService.verifyUser(req)).then((user) => ({ | ||
user, | ||
})); | ||
} | ||
|
||
@Post('authenticate') | ||
@ApiOperation({ summary: 'Authenticate user' }) | ||
@ApiBody({ | ||
type: AuthRequestDto, | ||
}) | ||
async authenticate( | ||
@Body() req: AuthRequestDto, | ||
@Res({ passthrough: true }) res: Response, | ||
) { | ||
const auth = this.authService.authenticate(req); | ||
return this.setAuthCookie(auth, res); | ||
} | ||
|
||
private async setAuthCookie(auth: Observable<AuthResponse>, res: Response) { | ||
return firstValueFrom(auth).then(({ token }) => { | ||
const { user, expires } = this.jwtService.decode<AuthTokenPayload>(token); | ||
|
||
res.cookie('Authentication', token, { | ||
httpOnly: true, | ||
expires: new Date(expires), | ||
}); | ||
|
||
return { | ||
user, | ||
token, | ||
}; | ||
}); | ||
} | ||
} |
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) { | ||
return this.client.registerUser(req); | ||
} | ||
|
||
verifyUser(req: VerifyUserRequest) { | ||
return this.client.verifyUser(req); | ||
} | ||
|
||
authenticate(req: AuthRequest) { | ||
return 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 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
Oops, something went wrong.