Skip to content

Commit

Permalink
Rename class to socialAuthenticationVerifier
Browse files Browse the repository at this point in the history
  • Loading branch information
mapeveri committed Dec 7, 2024
1 parent 7e56082 commit 79fc9f1
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import GetUserLoginQuery from './getUserLoginQuery';
import LoginException from '@src/shared/domain/auth/loginException';
import { Inject } from '@src/shared/domain/injector/inject.decorator';
import { SOCIAL_AUTHENTICATOR, SocialAuthenticator } from '@src/shared/domain/auth/socialAuthenticator';
import {
SOCIAL_AUTHENTICATION_VERIFIER,
SocialAuthenticationVerifier,
} from '@src/shared/domain/auth/socialAuthenticationVerifier';
import { IQueryHandler, QueryHandler } from '@src/shared/domain/bus/queryBus/queryHandler';
import { USER_AUTHENTICATOR, UserAuthenticator } from '@src/shared/domain/auth/userAuthenticator';
import GetUserLoginQueryResponse from '@src/languages/application/auth/query/getUserLoginQueryResponse';

@QueryHandler(GetUserLoginQuery)
export default class GetUserLoginQueryHandler implements IQueryHandler<GetUserLoginQuery> {
constructor(
@Inject(SOCIAL_AUTHENTICATOR) private readonly socialAuthenticator: SocialAuthenticator,
@Inject(SOCIAL_AUTHENTICATION_VERIFIER) private readonly socialAuthenticationVerifier: SocialAuthenticationVerifier,
@Inject(USER_AUTHENTICATOR) private readonly userAuthenticator: UserAuthenticator,
) {}

Expand All @@ -27,7 +30,7 @@ export default class GetUserLoginQueryHandler implements IQueryHandler<GetUserLo
}

private async verifySocialLogin(query: GetUserLoginQuery): Promise<void> {
const isValid: boolean = await this.socialAuthenticator.verify(query.token);
const isValid: boolean = await this.socialAuthenticationVerifier.verify(query.token);
if (!isValid) {
throw new LoginException(query.email);
}
Expand Down
8 changes: 4 additions & 4 deletions src/shared/_dependencyInjection/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import NestQueryBusBus from '@src/shared/infrastructure/bus/nestQueryBus';
import MongoEventStoreRepository from '@src/shared/infrastructure/persistence/mongo/repositories/mongoEventStoreRepository';
import { PersistDomainEventsSubscriber } from '@src/shared/infrastructure/subscribers/persistDomainEventsSubscriber';
import MongoConnection, { MONGO_CLIENT } from '@src/shared/infrastructure/persistence/mongo/mongoConnection';
import { SOCIAL_AUTHENTICATOR } from '@src/shared/domain/auth/socialAuthenticator';
import GoogleSocialAuthenticator from '@src/shared/infrastructure/auth/oauth/googleSocialAuthenticator';
import { SOCIAL_AUTHENTICATION_VERIFIER } from '@src/shared/domain/auth/socialAuthenticationVerifier';
import GoogleSocialAuthenticationVerifier from '@src/shared/infrastructure/auth/oauth/googleSocialAuthenticationVerifier';
import { ConfigService } from '@nestjs/config';
import { OAuth2Client } from 'google-auth-library';
import MikroOrmTransactionalDecorator from '../infrastructure/persistence/mikroOrm/decorators/mikroOrmTransactionalDecorator';
Expand Down Expand Up @@ -59,8 +59,8 @@ export const services = [
MongoEventStoreRepository,
PersistDomainEventsSubscriber,
{
provide: SOCIAL_AUTHENTICATOR,
useClass: GoogleSocialAuthenticator,
provide: SOCIAL_AUTHENTICATION_VERIFIER,
useClass: GoogleSocialAuthenticationVerifier,
},
{
provide: USER_AUTHENTICATOR,
Expand Down
5 changes: 5 additions & 0 deletions src/shared/domain/auth/socialAuthenticationVerifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface SocialAuthenticationVerifier {
verify(token: string): Promise<boolean>;
}

export const SOCIAL_AUTHENTICATION_VERIFIER = Symbol('SocialAuthenticationVerifier');
5 changes: 0 additions & 5 deletions src/shared/domain/auth/socialAuthenticator.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable } from '@nestjs/common';
import { SocialAuthenticator } from '@src/shared/domain/auth/socialAuthenticator';
import { SocialAuthenticationVerifier } from '@src/shared/domain/auth/socialAuthenticationVerifier';
import { OAuth2Client } from 'google-auth-library';
import { Inject } from '@src/shared/domain/injector/inject.decorator';
import Logger, { LOGGER } from '@src/shared/domain/logger';

@Injectable()
export default class GoogleSocialAuthenticator implements SocialAuthenticator {
export default class GoogleSocialAuthenticationVerifier implements SocialAuthenticationVerifier {
constructor(
@Inject('GOOGLE_OAUTH_CLIENT') readonly client: OAuth2Client,
@Inject(LOGGER) private readonly logger: Logger,
Expand Down
4 changes: 2 additions & 2 deletions src/shared/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { rabbitMqConfig } from '@src/shared/infrastructure/messenger/rabbitMq/co
import { consumers } from '@src/shared/_dependencyInjection/consumers';
import { services } from '@src/shared/_dependencyInjection/services';
import Environment from '@src/shared/infrastructure/utils/environment';
import { SOCIAL_AUTHENTICATOR } from '@src/shared/domain/auth/socialAuthenticator';
import { SOCIAL_AUTHENTICATION_VERIFIER } from '@src/shared/domain/auth/socialAuthenticationVerifier';
import MongoConnection, { MONGO_CLIENT } from '@src/shared/infrastructure/persistence/mongo/mongoConnection';
import { ConfigModule } from '@nestjs/config';
import { Inject } from '@src/shared/domain/injector/inject.decorator';
Expand Down Expand Up @@ -49,7 +49,7 @@ import { USER_AUTHENTICATOR } from '@src/shared/domain/auth/userAuthenticator';
COMMAND_BUS,
EVENT_BUS,
ASYNC_EVENT_BUS,
SOCIAL_AUTHENTICATOR,
SOCIAL_AUTHENTICATION_VERIFIER,
USER_AUTHENTICATOR,
],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { beforeEach, beforeAll, describe, expect, it, jest } from '@jest/globals
import GetUserLoginQueryHandler from '@src/languages/application/auth/query/getUserLoginQueryHandler';
import { GetUserLoginQueryMother } from './getUserLoginQueryMother';
import LoginException from '@src/shared/domain/auth/loginException';
import { SocialAuthenticatorMock } from '@test/unit/shared/domain/auth/socialAuthenticatorMock';
import { SocialAuthenticationVerifierMock } from '@test/unit/shared/domain/auth/socialAuthenticationVerifierMock';
import GetUserLoginQuery from '@src/languages/application/auth/query/getUserLoginQuery';
import { UserAuthenticatorMock } from '@test/unit/shared/domain/auth/userAuthenticatorMock';

describe('Given a GetUserLoginQueryHandler to handle', () => {
let socialAuthenticator: SocialAuthenticatorMock;
let socialAuthenticationVerifier: SocialAuthenticationVerifierMock;
let userAuthenticator: UserAuthenticatorMock;
let handler: GetUserLoginQueryHandler;

Expand All @@ -18,18 +18,18 @@ describe('Given a GetUserLoginQueryHandler to handle', () => {
const REFRESH_TOKEN = '1234';

const prepareDependencies = () => {
socialAuthenticator = new SocialAuthenticatorMock();
socialAuthenticationVerifier = new SocialAuthenticationVerifierMock();
userAuthenticator = new UserAuthenticatorMock();
};

const initHandler = () => {
handler = new GetUserLoginQueryHandler(socialAuthenticator, userAuthenticator);
handler = new GetUserLoginQueryHandler(socialAuthenticationVerifier, userAuthenticator);

jest.useFakeTimers();
};

const clean = () => {
socialAuthenticator.clean();
socialAuthenticationVerifier.clean();
userAuthenticator.clean();
};

Expand All @@ -47,7 +47,7 @@ describe('Given a GetUserLoginQueryHandler to handle', () => {

function startScenario() {
command = GetUserLoginQueryMother.random();
socialAuthenticator.add(false);
socialAuthenticationVerifier.add(false);
}

beforeEach(startScenario);
Expand All @@ -66,7 +66,7 @@ describe('Given a GetUserLoginQueryHandler to handle', () => {
email: EMAIL,
name: NAME,
});
socialAuthenticator.add(true);
socialAuthenticationVerifier.add(true);
userAuthenticator.add({ token: '123', refreshToken: '1234' });
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SocialAuthenticator } from '@src/shared/domain/auth/socialAuthenticator';
import { SocialAuthenticationVerifier } from '@src/shared/domain/auth/socialAuthenticationVerifier';

export class SocialAuthenticatorMock implements SocialAuthenticator {
export class SocialAuthenticationVerifierMock implements SocialAuthenticationVerifier {
private toReturn: boolean;

constructor() {
Expand Down

0 comments on commit 79fc9f1

Please sign in to comment.