From 0c8382eaf292646158a38c0e239ad93fc3767efe Mon Sep 17 00:00:00 2001 From: Felipe Luna Date: Tue, 27 Feb 2024 12:49:34 -0300 Subject: [PATCH 1/3] feat(aut-data-access): add lib --- modules/data-access/auth/.eslintrc.json | 36 +++++++++++++++++++ modules/data-access/auth/README.md | 7 ++++ modules/data-access/auth/jest.config.ts | 22 ++++++++++++ modules/data-access/auth/project.json | 27 ++++++++++++++ modules/data-access/auth/src/index.ts | 1 + .../auth-data-access.component.css | 0 .../auth-data-access.component.html | 1 + .../auth-data-access.component.spec.ts | 21 +++++++++++ .../auth-data-access.component.ts | 11 ++++++ modules/data-access/auth/src/test-setup.ts | 8 +++++ modules/data-access/auth/tsconfig.json | 29 +++++++++++++++ modules/data-access/auth/tsconfig.lib.json | 17 +++++++++ modules/data-access/auth/tsconfig.spec.json | 16 +++++++++ tsconfig.base.json | 1 + 14 files changed, 197 insertions(+) create mode 100644 modules/data-access/auth/.eslintrc.json create mode 100644 modules/data-access/auth/README.md create mode 100644 modules/data-access/auth/jest.config.ts create mode 100644 modules/data-access/auth/project.json create mode 100644 modules/data-access/auth/src/index.ts create mode 100644 modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.css create mode 100644 modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.html create mode 100644 modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.spec.ts create mode 100644 modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.ts create mode 100644 modules/data-access/auth/src/test-setup.ts create mode 100644 modules/data-access/auth/tsconfig.json create mode 100644 modules/data-access/auth/tsconfig.lib.json create mode 100644 modules/data-access/auth/tsconfig.spec.json diff --git a/modules/data-access/auth/.eslintrc.json b/modules/data-access/auth/.eslintrc.json new file mode 100644 index 0000000..39c7aa9 --- /dev/null +++ b/modules/data-access/auth/.eslintrc.json @@ -0,0 +1,36 @@ +{ + "extends": ["../../../.eslintrc.base.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts"], + "extends": [ + "plugin:@nx/angular", + "plugin:@angular-eslint/template/process-inline-templates" + ], + "rules": { + "@angular-eslint/directive-selector": [ + "error", + { + "type": "attribute", + "prefix": "lib", + "style": "camelCase" + } + ], + "@angular-eslint/component-selector": [ + "error", + { + "type": "element", + "prefix": "lib", + "style": "kebab-case" + } + ] + } + }, + { + "files": ["*.html"], + "extends": ["plugin:@nx/angular-template"], + "rules": {} + } + ] +} diff --git a/modules/data-access/auth/README.md b/modules/data-access/auth/README.md new file mode 100644 index 0000000..5a78db3 --- /dev/null +++ b/modules/data-access/auth/README.md @@ -0,0 +1,7 @@ +# auth-data-access + +This library was generated with [Nx](https://nx.dev). + +## Running unit tests + +Run `nx test auth-data-access` to execute the unit tests. diff --git a/modules/data-access/auth/jest.config.ts b/modules/data-access/auth/jest.config.ts new file mode 100644 index 0000000..1c1de17 --- /dev/null +++ b/modules/data-access/auth/jest.config.ts @@ -0,0 +1,22 @@ +/* eslint-disable */ +export default { + displayName: 'auth-data-access', + preset: '../../../jest.preset.js', + setupFilesAfterEnv: ['/src/test-setup.ts'], + coverageDirectory: '../../../coverage/modules/data-access/auth', + transform: { + '^.+\\.(ts|mjs|js|html)$': [ + 'jest-preset-angular', + { + tsconfig: '/tsconfig.spec.json', + stringifyContentPathRegex: '\\.(html|svg)$', + }, + ], + }, + transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'], + snapshotSerializers: [ + 'jest-preset-angular/build/serializers/no-ng-attributes', + 'jest-preset-angular/build/serializers/ng-snapshot', + 'jest-preset-angular/build/serializers/html-comment', + ], +}; diff --git a/modules/data-access/auth/project.json b/modules/data-access/auth/project.json new file mode 100644 index 0000000..d48a032 --- /dev/null +++ b/modules/data-access/auth/project.json @@ -0,0 +1,27 @@ +{ + "name": "auth-data-access", + "$schema": "../../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "modules/data-access/auth/src", + "prefix": "lib", + "tags": [], + "projectType": "library", + "targets": { + "test": { + "executor": "@nx/jest:jest", + "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], + "options": { + "jestConfig": "modules/data-access/auth/jest.config.ts" + } + }, + "lint": { + "executor": "@nx/eslint:lint", + "outputs": ["{options.outputFile}"], + "options": { + "lintFilePatterns": [ + "modules/data-access/auth/**/*.ts", + "modules/data-access/auth/**/*.html" + ] + } + } + } +} diff --git a/modules/data-access/auth/src/index.ts b/modules/data-access/auth/src/index.ts new file mode 100644 index 0000000..aeca1c6 --- /dev/null +++ b/modules/data-access/auth/src/index.ts @@ -0,0 +1 @@ +export * from './lib/auth-data-access/auth-data-access.component'; diff --git a/modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.css b/modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.css new file mode 100644 index 0000000..e69de29 diff --git a/modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.html b/modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.html new file mode 100644 index 0000000..1095cc4 --- /dev/null +++ b/modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.html @@ -0,0 +1 @@ +

auth-data-access works!

diff --git a/modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.spec.ts b/modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.spec.ts new file mode 100644 index 0000000..7ac2ef9 --- /dev/null +++ b/modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.spec.ts @@ -0,0 +1,21 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { AuthDataAccessComponent } from './auth-data-access.component'; + +describe('AuthDataAccessComponent', () => { + let component: AuthDataAccessComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [AuthDataAccessComponent], + }).compileComponents(); + + fixture = TestBed.createComponent(AuthDataAccessComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.ts b/modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.ts new file mode 100644 index 0000000..4e58d67 --- /dev/null +++ b/modules/data-access/auth/src/lib/auth-data-access/auth-data-access.component.ts @@ -0,0 +1,11 @@ +import { Component } from '@angular/core'; +import { CommonModule } from '@angular/common'; + +@Component({ + selector: 'lib-auth-data-access', + standalone: true, + imports: [CommonModule], + templateUrl: './auth-data-access.component.html', + styleUrl: './auth-data-access.component.css', +}) +export class AuthDataAccessComponent {} diff --git a/modules/data-access/auth/src/test-setup.ts b/modules/data-access/auth/src/test-setup.ts new file mode 100644 index 0000000..ab1eeeb --- /dev/null +++ b/modules/data-access/auth/src/test-setup.ts @@ -0,0 +1,8 @@ +// @ts-expect-error https://thymikee.github.io/jest-preset-angular/docs/getting-started/test-environment +globalThis.ngJest = { + testEnvironmentOptions: { + errorOnUnknownElements: true, + errorOnUnknownProperties: true, + }, +}; +import 'jest-preset-angular/setup-jest'; diff --git a/modules/data-access/auth/tsconfig.json b/modules/data-access/auth/tsconfig.json new file mode 100644 index 0000000..5cf0a16 --- /dev/null +++ b/modules/data-access/auth/tsconfig.json @@ -0,0 +1,29 @@ +{ + "compilerOptions": { + "target": "es2022", + "useDefineForClassFields": false, + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ], + "extends": "../../../tsconfig.base.json", + "angularCompilerOptions": { + "enableI18nLegacyMessageIdFormat": false, + "strictInjectionParameters": true, + "strictInputAccessModifiers": true, + "strictTemplates": true + } +} diff --git a/modules/data-access/auth/tsconfig.lib.json b/modules/data-access/auth/tsconfig.lib.json new file mode 100644 index 0000000..9b49be7 --- /dev/null +++ b/modules/data-access/auth/tsconfig.lib.json @@ -0,0 +1,17 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../../dist/out-tsc", + "declaration": true, + "declarationMap": true, + "inlineSources": true, + "types": [] + }, + "exclude": [ + "src/**/*.spec.ts", + "src/test-setup.ts", + "jest.config.ts", + "src/**/*.test.ts" + ], + "include": ["src/**/*.ts"] +} diff --git a/modules/data-access/auth/tsconfig.spec.json b/modules/data-access/auth/tsconfig.spec.json new file mode 100644 index 0000000..f858ef7 --- /dev/null +++ b/modules/data-access/auth/tsconfig.spec.json @@ -0,0 +1,16 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../../dist/out-tsc", + "module": "commonjs", + "target": "es2016", + "types": ["jest", "node"] + }, + "files": ["src/test-setup.ts"], + "include": [ + "jest.config.ts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.d.ts" + ] +} diff --git a/tsconfig.base.json b/tsconfig.base.json index d6ebc7b..cd6852f 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -15,6 +15,7 @@ "skipDefaultLibCheck": true, "baseUrl": ".", "paths": { + "auth-data-access": ["modules/data-access/auth/src/index.ts"], "auth-form": ["modules/feature/auth/form/src/index.ts"], "home": ["modules/feature/home/src/index.ts"], "modules/layout": ["modules/feature/src/index.ts"], From 203976032f6c8448fe123685ac3744f4db7e1224 Mon Sep 17 00:00:00 2001 From: Felipe Luna Date: Tue, 27 Feb 2024 12:59:36 -0300 Subject: [PATCH 2/3] feat(auth-data-access): add auth service --- modules/data-access/auth/src/index.ts | 3 ++- .../auth/src/lib/auth/auth.service.spec.ts | 16 ++++++++++++++++ .../auth/src/lib/auth/auth.service.ts | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 modules/data-access/auth/src/lib/auth/auth.service.spec.ts create mode 100644 modules/data-access/auth/src/lib/auth/auth.service.ts diff --git a/modules/data-access/auth/src/index.ts b/modules/data-access/auth/src/index.ts index aeca1c6..b918a6b 100644 --- a/modules/data-access/auth/src/index.ts +++ b/modules/data-access/auth/src/index.ts @@ -1 +1,2 @@ -export * from './lib/auth-data-access/auth-data-access.component'; +//export * from './lib/auth-data-access/auth-data-access.component'; +export * from './lib/auth/auth.service'; diff --git a/modules/data-access/auth/src/lib/auth/auth.service.spec.ts b/modules/data-access/auth/src/lib/auth/auth.service.spec.ts new file mode 100644 index 0000000..f1251ca --- /dev/null +++ b/modules/data-access/auth/src/lib/auth/auth.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AuthService } from './auth.service'; + +describe('AuthService', () => { + let service: AuthService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(AuthService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/modules/data-access/auth/src/lib/auth/auth.service.ts b/modules/data-access/auth/src/lib/auth/auth.service.ts new file mode 100644 index 0000000..ce9247a --- /dev/null +++ b/modules/data-access/auth/src/lib/auth/auth.service.ts @@ -0,0 +1,14 @@ +import { Injectable } from '@angular/core'; +import { BehaviorSubject } from 'rxjs'; + +@Injectable({ + providedIn: 'root', +}) +export class AuthService { + private emailSubject = new BehaviorSubject(null); + email$ = this.emailSubject.asObservable(); + + setEmail(email: string): void { + this.emailSubject.next(email); + } +} From 2ef1ac693186d4f370183abb01bf4ef5b49a7a83 Mon Sep 17 00:00:00 2001 From: Felipe Luna Date: Tue, 27 Feb 2024 17:02:23 -0300 Subject: [PATCH 3/3] feat(data-access-auth): add auth guard --- modules/data-access/auth/project.json | 2 +- modules/data-access/auth/src/index.ts | 1 + .../src/lib/guards/auth/auth.guard.spec.ts | 35 +++++++++++++++++++ .../auth/src/lib/guards/auth/auth.guard.ts | 15 ++++++++ .../auth-form-password.component.html | 2 +- .../auth-form-password.component.ts | 10 ++++++ .../feature/auth/form/src/lib/lib.routes.ts | 2 ++ src/app/app.component.html | 9 +++-- 8 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 modules/data-access/auth/src/lib/guards/auth/auth.guard.spec.ts create mode 100644 modules/data-access/auth/src/lib/guards/auth/auth.guard.ts diff --git a/modules/data-access/auth/project.json b/modules/data-access/auth/project.json index d48a032..082307d 100644 --- a/modules/data-access/auth/project.json +++ b/modules/data-access/auth/project.json @@ -3,7 +3,7 @@ "$schema": "../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "modules/data-access/auth/src", "prefix": "lib", - "tags": [], + "tags": ["type:data-access"], "projectType": "library", "targets": { "test": { diff --git a/modules/data-access/auth/src/index.ts b/modules/data-access/auth/src/index.ts index b918a6b..37b0348 100644 --- a/modules/data-access/auth/src/index.ts +++ b/modules/data-access/auth/src/index.ts @@ -1,2 +1,3 @@ //export * from './lib/auth-data-access/auth-data-access.component'; export * from './lib/auth/auth.service'; +export * from './lib/guards/auth/auth.guard'; diff --git a/modules/data-access/auth/src/lib/guards/auth/auth.guard.spec.ts b/modules/data-access/auth/src/lib/guards/auth/auth.guard.spec.ts new file mode 100644 index 0000000..83db6d3 --- /dev/null +++ b/modules/data-access/auth/src/lib/guards/auth/auth.guard.spec.ts @@ -0,0 +1,35 @@ +import { TestBed } from '@angular/core/testing'; +import { RouterTestingModule } from '@angular/router/testing'; +import { AuthService, authGuard } from 'auth-data-access'; +import { of } from 'rxjs'; + +describe('authGuard', () => { + it('should return true when user is not truthy', () => { + TestBed.configureTestingModule({ + imports: [RouterTestingModule], + }); + + TestBed.overrideProvider(AuthService, { + useValue: { email$: of(null) }, + }); + + const guard = TestBed.runInInjectionContext(authGuard()); + guard.subscribe((isActivated) => { + expect(isActivated).toBe(true); + }); + }); + + it('should not return true when user is truthy', () => { + TestBed.configureTestingModule({ + imports: [RouterTestingModule], + }); + + TestBed.overrideProvider(AuthService, { + useValue: { email$: of('mail@mail.com') }, + }); + const guard = TestBed.runInInjectionContext(authGuard()); + guard.subscribe((isActivated) => { + expect(isActivated).not.toBe(true); + }); + }); +}); diff --git a/modules/data-access/auth/src/lib/guards/auth/auth.guard.ts b/modules/data-access/auth/src/lib/guards/auth/auth.guard.ts new file mode 100644 index 0000000..e96b77a --- /dev/null +++ b/modules/data-access/auth/src/lib/guards/auth/auth.guard.ts @@ -0,0 +1,15 @@ +import { inject } from '@angular/core'; +import { Router } from '@angular/router'; +import { AuthService } from '../../auth/auth.service'; +import { map } from 'rxjs'; + +export function authGuard() { + return () => { + const router = inject(Router); + const email$ = inject(AuthService).email$; + + return email$.pipe( + map((email) => (!email ? true : router.createUrlTree(['/']))) + ); + }; +} diff --git a/modules/feature/auth/form/src/lib/auth-form/auth-form-password/auth-form-password.component.html b/modules/feature/auth/form/src/lib/auth-form/auth-form-password/auth-form-password.component.html index 3a06c49..9416161 100644 --- a/modules/feature/auth/form/src/lib/auth-form/auth-form-password/auth-form-password.component.html +++ b/modules/feature/auth/form/src/lib/auth-form/auth-form-password/auth-form-password.component.html @@ -9,5 +9,5 @@
- +
diff --git a/modules/feature/auth/form/src/lib/auth-form/auth-form-password/auth-form-password.component.ts b/modules/feature/auth/form/src/lib/auth-form/auth-form-password/auth-form-password.component.ts index 11093dc..410e066 100644 --- a/modules/feature/auth/form/src/lib/auth-form/auth-form-password/auth-form-password.component.ts +++ b/modules/feature/auth/form/src/lib/auth-form/auth-form-password/auth-form-password.component.ts @@ -4,6 +4,8 @@ import { MatButtonModule } from '@angular/material/button'; import { MatInputModule } from '@angular/material/input'; import { AuthFormComponent } from '../auth-form.component'; import { ReactiveFormsModule } from '@angular/forms'; +import { AuthService } from 'modules/data-access/auth/src/lib/auth/auth.service'; +import { Router } from '@angular/router'; @Component({ selector: 'lib-auth-form-password', @@ -13,5 +15,13 @@ import { ReactiveFormsModule } from '@angular/forms'; styleUrl: './auth-form-password.component.scss', }) export class AuthFormPasswordComponent { + email = inject(AuthFormComponent).form.controls.email.value; control = inject(AuthFormComponent).form.controls.password; + authService = inject(AuthService); + router = inject(Router); + + login(): void { + this.authService.setEmail(this.email); + this.router.navigate(['/']); + } } diff --git a/modules/feature/auth/form/src/lib/lib.routes.ts b/modules/feature/auth/form/src/lib/lib.routes.ts index 9c2cf65..a98c518 100644 --- a/modules/feature/auth/form/src/lib/lib.routes.ts +++ b/modules/feature/auth/form/src/lib/lib.routes.ts @@ -1,9 +1,11 @@ import { Route } from '@angular/router'; import { AuthFormComponent } from './auth-form/auth-form.component'; +import { authGuard } from 'auth-data-access'; export const authFormRoutes: Route[] = [ { path: '', + canActivate: [authGuard()], component: AuthFormComponent, children: [ { diff --git a/src/app/app.component.html b/src/app/app.component.html index 4b8cf7c..e1a51f4 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,7 +1,10 @@ - Fazer Login +
+ Fazer Login + Carrinho de Compras +