create function with auth
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
**/.DS_Store
|
1
backend/.gitignore
vendored
Normal file
1
backend/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
blog.db
|
BIN
backend/blog.db
BIN
backend/blog.db
Binary file not shown.
@ -2,12 +2,19 @@ import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
|
|||||||
import { provideRouter, withComponentInputBinding } from '@angular/router';
|
import { provideRouter, withComponentInputBinding } from '@angular/router';
|
||||||
|
|
||||||
import { routes } from './app.routes';
|
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 = {
|
export const appConfig: ApplicationConfig = {
|
||||||
providers: [
|
providers: [
|
||||||
provideZoneChangeDetection({ eventCoalescing: true }),
|
provideZoneChangeDetection({ eventCoalescing: true }),
|
||||||
provideRouter(routes, withComponentInputBinding()),
|
provideRouter(routes, withComponentInputBinding()),
|
||||||
provideHttpClient(),
|
provideHttpClient(withInterceptorsFromDi()),
|
||||||
|
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
@ -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 { PostEditorComponent } from '../../components/post-editor/post-editor.component';
|
||||||
import { NgFor } from '@angular/common';
|
import { NgFor } from '@angular/common';
|
||||||
import { PostsService } from '../../shared/services/posts.service';
|
import { PostsService } from '../../shared/services/posts.service';
|
||||||
|
@ -20,10 +20,6 @@ export class CreatePostComponent {
|
|||||||
});
|
});
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
effect(() => {
|
|
||||||
console.log('create', this.post());
|
|
||||||
});
|
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.post.set({ ...this.post(), title: 'adf' });
|
this.post.set({ ...this.post(), title: 'adf' });
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
@ -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();
|
||||||
|
});
|
||||||
|
});
|
29
frontend/src/app/shared/interceptors/auth.interceptor.ts
Normal file
29
frontend/src/app/shared/interceptors/auth.interceptor.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -61,8 +61,6 @@ export class AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
login(user: User) {
|
login(user: User) {
|
||||||
console.log(user);
|
|
||||||
|
|
||||||
this.http
|
this.http
|
||||||
.post<LoginResponse>(`${environment.apiRoot}/login`, user)
|
.post<LoginResponse>(`${environment.apiRoot}/login`, user)
|
||||||
.subscribe((res) => this.jwt.set(res.token));
|
.subscribe((res) => this.jwt.set(res.token));
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { HttpClient } from '@angular/common/http';
|
import { HttpClient } from '@angular/common/http';
|
||||||
import {
|
import {
|
||||||
computed,
|
computed,
|
||||||
|
effect,
|
||||||
inject,
|
inject,
|
||||||
Injectable,
|
Injectable,
|
||||||
Signal,
|
Signal,
|
||||||
@ -27,7 +28,9 @@ export class PostsService {
|
|||||||
.subscribe((res) => this.posts.set(new Map(res.map((p) => [p.id, p]))));
|
.subscribe((res) => this.posts.set(new Map(res.map((p) => [p.id, p]))));
|
||||||
}
|
}
|
||||||
getPosts(): Signal<Post[]> {
|
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> {
|
getPost(id: number): Signal<Post | undefined> {
|
||||||
@ -38,7 +41,7 @@ export class PostsService {
|
|||||||
this.http
|
this.http
|
||||||
.post<Post>(`${environment.apiRoot}/posts`, post)
|
.post<Post>(`${environment.apiRoot}/posts`, post)
|
||||||
.subscribe((res) => {
|
.subscribe((res) => {
|
||||||
this.posts.set(this.posts().set(res.id, res));
|
this.posts.update((p) => new Map(p.set(res.id, res)));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user