From cc92a110608b9ec33cf8a14f50d68bd375254fb7 Mon Sep 17 00:00:00 2001 From: u80864958 Date: Mon, 28 Apr 2025 13:32:51 +0200 Subject: [PATCH] create function with auth --- .gitignore | 1 + backend/.gitignore | 1 + backend/blog.db | Bin 20480 -> 20480 bytes frontend/src/app/app.config.ts | 11 +++++-- .../src/app/routes/admin/admin.component.ts | 2 +- .../post/create-post/create-post.component.ts | 4 --- .../interceptors/auth.interceptor.spec.ts | 16 ++++++++++ .../shared/interceptors/auth.interceptor.ts | 29 ++++++++++++++++++ .../src/app/shared/services/auth.service.ts | 2 -- .../src/app/shared/services/posts.service.ts | 7 +++-- 10 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 .gitignore create mode 100644 backend/.gitignore create mode 100644 frontend/src/app/shared/interceptors/auth.interceptor.spec.ts create mode 100644 frontend/src/app/shared/interceptors/auth.interceptor.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..79b5594 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/.DS_Store diff --git a/backend/.gitignore b/backend/.gitignore new file mode 100644 index 0000000..2fdf6da --- /dev/null +++ b/backend/.gitignore @@ -0,0 +1 @@ +blog.db diff --git a/backend/blog.db b/backend/blog.db index 0a59c2a498bad7cf130ea758ac4e94f36c133269..15e0e603abc9a655346eb5fb243a8a3df5301f57 100644 GIT binary patch literal 20480 zcmeI3O>7%Q6vx-G6Wg2E)6`9^DvCNNms&}d{q)+7M4J}31UF9W7`0V_*zK-UC;n)> zTUw!90;gWMaDWRJE`S6Q9Ee1@AXQwrAS5naxNt#8y&xf^iUV&Zc4K=td`VoYH`;wO z-naXI@BL=Rmh-W8`C{F*X|3I9R$ZzIM}$B?I76u*2#auz!r5yf7_hU~0=>as^oxS@ z+Wi>Z5Q=>$kkj!;@%u2qh6IoR5>Q;r;Tduufcj%?k^2$=_8ZG3n(WRHl ztII`rZY5tV&sT1@9e2I&-ua$Lg}U}Vcbm*i)C7=ZLPD%o%Msb z=T}SlC_YP6fNb?=S%tGx%?XCSDgyaFqOIy3@43bV0X0Mvg~_x>t0vVvKzK* zTkBPqhX{2GT`oSKze=Gv?|l`zT09Uxc)pUlG%}W)n+sgy2%YT)eDhs*?Cm>t>!v;Q zGB^Nq$a$(&ZQAqjWNP=hKscF92Hv>NHF=8Mj0`;AE%l$z#?6jh-LJYrEf~3V2w1su zn~fQZ{;Pcz_TB5&-G<$rtBTvOI=%70G^W^h^JMrzApRX$jd$W=?9K$$n01&dB5pRWh@3+9xp|H%QfhTHm<=?aEP}>U3;oNZo`0lA)#*Mf0W# z@*Y3vb9u|*ePPm%0e?`|C0#e=Z2A!4AwLMO9pPfLgHsJ+FqMjA%8I7@tR+8xFG~P% ziEz;wkEj9FNt&T6X`l52q8>kpTC5e~htv#DD3YdS4cTYj_Z0{Ipje0%9?3e)_v3W_#qH&-Q8XZ^L?L)O$}wP@!0fU1Q5n($c1;`p%fw|AO6UwCR<0r(vCM_gb~P?XQmVFo~&Q-bzl*z%1&f z>bnIrkH4EYr0!H}7M!7dz$a6KJk)H?&?VJ0GluU>di{_!JzMQK?WRqgTXlDKmiE>3 zO0ZjjHx9fp8QCmsW50=nI75D@VEeA!*|zU?SigVl1R}!e6(RBLby=2`1%6OjhL1rw z=D6*vPtN2tgMD7Y7B#coEeiR+c*k)mFQ8jYZOhuETbnibptLq?jjc_q@ota7cM-&%&QP~I&X9`IX>PnKg1o8*@o%~83k)O#= delta 141 zcmZozz}T>Wae_3X%0wAwMwN{TOZYjM_%1Q>UE=rOEU4hYH~FHRwveHrf}w$xv5A$5 ziJpO(rIE48WIlOEwrWNW27B+x*X2|Np(;$RjEwY5EG^B=feP|q`oN;528IU4=9~NE f3k7(YxEL51nE1al@c-fezFE-WHUH-C_M8F$)2t(= diff --git a/frontend/src/app/app.config.ts b/frontend/src/app/app.config.ts index 3c0cff3..ccf6af2 100644 --- a/frontend/src/app/app.config.ts +++ b/frontend/src/app/app.config.ts @@ -2,12 +2,19 @@ import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core'; import { provideRouter, withComponentInputBinding } from '@angular/router'; import { routes } from './app.routes'; -import { provideHttpClient } from '@angular/common/http'; +import { + HTTP_INTERCEPTORS, + provideHttpClient, + withInterceptors, + withInterceptorsFromDi, +} from '@angular/common/http'; +import { AuthInterceptor } from './shared/interceptors/auth.interceptor'; export const appConfig: ApplicationConfig = { providers: [ provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes, withComponentInputBinding()), - provideHttpClient(), + provideHttpClient(withInterceptorsFromDi()), + { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }, ], }; diff --git a/frontend/src/app/routes/admin/admin.component.ts b/frontend/src/app/routes/admin/admin.component.ts index f326132..3a0bebe 100644 --- a/frontend/src/app/routes/admin/admin.component.ts +++ b/frontend/src/app/routes/admin/admin.component.ts @@ -1,4 +1,4 @@ -import { Component, inject } from '@angular/core'; +import { Component, effect, inject } from '@angular/core'; import { PostEditorComponent } from '../../components/post-editor/post-editor.component'; import { NgFor } from '@angular/common'; import { PostsService } from '../../shared/services/posts.service'; diff --git a/frontend/src/app/routes/post/create-post/create-post.component.ts b/frontend/src/app/routes/post/create-post/create-post.component.ts index b032e08..5928d48 100644 --- a/frontend/src/app/routes/post/create-post/create-post.component.ts +++ b/frontend/src/app/routes/post/create-post/create-post.component.ts @@ -20,10 +20,6 @@ export class CreatePostComponent { }); constructor() { - effect(() => { - console.log('create', this.post()); - }); - setTimeout(() => { this.post.set({ ...this.post(), title: 'adf' }); }, 1000); diff --git a/frontend/src/app/shared/interceptors/auth.interceptor.spec.ts b/frontend/src/app/shared/interceptors/auth.interceptor.spec.ts new file mode 100644 index 0000000..7ab58db --- /dev/null +++ b/frontend/src/app/shared/interceptors/auth.interceptor.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AuthInterceptor } from './auth.interceptor'; + +describe('AuthInterceptor', () => { + beforeEach(() => TestBed.configureTestingModule({ + providers: [ + AuthInterceptor + ] + })); + + it('should be created', () => { + const interceptor: AuthInterceptor = TestBed.inject(AuthInterceptor); + expect(interceptor).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/shared/interceptors/auth.interceptor.ts b/frontend/src/app/shared/interceptors/auth.interceptor.ts new file mode 100644 index 0000000..4720419 --- /dev/null +++ b/frontend/src/app/shared/interceptors/auth.interceptor.ts @@ -0,0 +1,29 @@ +import { inject, Injectable } from '@angular/core'; +import { + HttpRequest, + HttpHandler, + HttpEvent, + HttpInterceptor, +} from '@angular/common/http'; +import { Observable } from 'rxjs'; +import { AuthService } from '../services/auth.service'; + +@Injectable() +export class AuthInterceptor implements HttpInterceptor { + private jwt = inject(AuthService).jwt; + + intercept( + req: HttpRequest, + next: HttpHandler, + ): Observable> { + const token = this.jwt(); + if (!token) { + return next.handle(req); + } + const reqWithAuth = req.clone({ + headers: req.headers.set('Authorization', `Bearer ${token}`), + }); + + return next.handle(reqWithAuth); + } +} diff --git a/frontend/src/app/shared/services/auth.service.ts b/frontend/src/app/shared/services/auth.service.ts index a0daa81..c6a0e12 100644 --- a/frontend/src/app/shared/services/auth.service.ts +++ b/frontend/src/app/shared/services/auth.service.ts @@ -61,8 +61,6 @@ export class AuthService { } login(user: User) { - console.log(user); - this.http .post(`${environment.apiRoot}/login`, user) .subscribe((res) => this.jwt.set(res.token)); diff --git a/frontend/src/app/shared/services/posts.service.ts b/frontend/src/app/shared/services/posts.service.ts index 79e96c4..82f3362 100644 --- a/frontend/src/app/shared/services/posts.service.ts +++ b/frontend/src/app/shared/services/posts.service.ts @@ -1,6 +1,7 @@ import { HttpClient } from '@angular/common/http'; import { computed, + effect, inject, Injectable, Signal, @@ -27,7 +28,9 @@ export class PostsService { .subscribe((res) => this.posts.set(new Map(res.map((p) => [p.id, p])))); } getPosts(): Signal { - return computed(() => Array.from(this.posts().values())); + return computed(() => + Array.from(this.posts().values()).sort((a, b) => b.id - a.id), + ); } getPost(id: number): Signal { @@ -38,7 +41,7 @@ export class PostsService { this.http .post(`${environment.apiRoot}/posts`, post) .subscribe((res) => { - this.posts.set(this.posts().set(res.id, res)); + this.posts.update((p) => new Map(p.set(res.id, res))); }); } }