Skip to content

Commit

Permalink
Add token #172
Browse files Browse the repository at this point in the history
  • Loading branch information
mapeveri committed Dec 20, 2024
1 parent 7eef85a commit 85cfab5
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@ import { firstValueFrom } from 'rxjs';
import CollaboratorId from '@src/languages/domain/collaborator/collaboratorId';
import Collaborator from '@src/languages/domain/collaborator/collaborator';
import { CollaboratorTranslator } from '@src/languages/infrastructure/persistence/http/collaboratorTranslator';
import { Inject } from '@src/shared/domain/injector/inject.decorator';
import { USER_AUTHENTICATOR, UserAuthenticator } from '@src/shared/domain/auth/userAuthenticator';

@Injectable()
export class HttpCollaboratorAdapter {
URL = '/api/v1/users/{id}';
constructor(private readonly httpService: HttpService) {}
constructor(
private readonly httpService: HttpService,
@Inject(USER_AUTHENTICATOR) private readonly userAuthenticator: UserAuthenticator,
) {}

async toCollaborator(id: CollaboratorId): Promise<Collaborator | null> {
const { data } = await firstValueFrom(this.httpService.get(this.URL.replace('{id}', id.toString())));
const m2mToken = this.userAuthenticator.sign({});

const { data } = await firstValueFrom(
this.httpService.get(this.URL.replace('{id}', id.toString()), {
headers: {
Authorization: `Bearer ${m2mToken.token}`,
},
}),
);

if (!data) return null;

return CollaboratorTranslator.toCollaborator(data);
Expand Down

0 comments on commit 85cfab5

Please sign in to comment.