create function with auth

This commit is contained in:
u80864958
2025-04-28 13:32:51 +02:00
parent 4429f2670c
commit cc92a11060
10 changed files with 62 additions and 11 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
**/.DS_Store

1
backend/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
blog.db

Binary file not shown.

View File

@ -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 },
],
};

View File

@ -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';

View File

@ -20,10 +20,6 @@ export class CreatePostComponent {
});
constructor() {
effect(() => {
console.log('create', this.post());
});
setTimeout(() => {
this.post.set({ ...this.post(), title: 'adf' });
}, 1000);

View File

@ -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();
});
});

View File

@ -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<unknown>,
next: HttpHandler,
): Observable<HttpEvent<unknown>> {
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);
}
}

View File

@ -61,8 +61,6 @@ export class AuthService {
}
login(user: User) {
console.log(user);
this.http
.post<LoginResponse>(`${environment.apiRoot}/login`, user)
.subscribe((res) => this.jwt.set(res.token));

View File

@ -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<Post[]> {
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<Post | undefined> {
@ -38,7 +41,7 @@ export class PostsService {
this.http
.post<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)));
});
}
}