API_URL-Token erstellt und über Url-Dienst injiziert.
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import { ApplicationConfig } from '@angular/core';
|
||||
import { provideRouter } from '@angular/router';
|
||||
|
||||
import { routes } from './app.routes';
|
||||
import { provideClientHydration } from '@angular/platform-browser';
|
||||
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
|
||||
import { APP_BASE_HREF } from '@angular/common';
|
||||
import { BaseHrefService } from './services/base-href.service';
|
||||
|
||||
import { UrlService } from './services/url.service';
|
||||
import { API_URL } from './tokens/index'
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideRouter(routes),
|
||||
@@ -14,8 +13,13 @@ export const appConfig: ApplicationConfig = {
|
||||
provideAnimationsAsync(),
|
||||
{
|
||||
provide: APP_BASE_HREF,
|
||||
useFactory: (baseHrefService: BaseHrefService) => baseHrefService.getBaseHref(),
|
||||
deps: [BaseHrefService]
|
||||
useFactory: (urlService: UrlService) => urlService.getBaseHref(),
|
||||
deps: [UrlService]
|
||||
},
|
||||
{
|
||||
provide: API_URL,
|
||||
useFactory: (urlService: UrlService) => urlService.getApiUrl(),
|
||||
deps: [UrlService]
|
||||
}
|
||||
]
|
||||
};
|
||||
};
|
||||
@@ -1,28 +1,28 @@
|
||||
import { Injectable, Inject, inject } from '@angular/core';
|
||||
import { Injectable, inject } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { APP_BASE_HREF } from '@angular/common';
|
||||
import { API_URL } from '../tokens/index';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthService {
|
||||
private baseUrl: string;
|
||||
private url: string;
|
||||
|
||||
constructor(private http: HttpClient) {
|
||||
const baseHref = inject(APP_BASE_HREF);
|
||||
this.baseUrl = `${baseHref}auth`;
|
||||
const api_url = inject(API_URL);
|
||||
this.url = `${api_url}/auth`;
|
||||
}
|
||||
|
||||
login(credentials: { username: string; password: string }): Observable<any> {
|
||||
return this.http.post(`${this.baseUrl}login`, credentials);
|
||||
return this.http.post(`${this.url}/login`, credentials);
|
||||
}
|
||||
|
||||
logout(): Observable<any> {
|
||||
return this.http.post(`${this.baseUrl}logout`, {});
|
||||
return this.http.post(`${this.url}/logout`, {});
|
||||
}
|
||||
|
||||
isAuthenticated(): Observable<boolean> {
|
||||
return this.http.get<boolean>(`${this.baseUrl}check`);
|
||||
return this.http.get<boolean>(`${this.url}/check`);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { BaseHrefService } from './base-href.service';
|
||||
import { UrlService } from './url.service';
|
||||
|
||||
describe('BaseHrefService', () => {
|
||||
let service: BaseHrefService;
|
||||
describe('UrlService', () => {
|
||||
let service: UrlService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(BaseHrefService);
|
||||
service = TestBed.inject(UrlService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
@@ -1,18 +1,26 @@
|
||||
// base-href.service.ts
|
||||
import { Injectable, Inject, inject } from '@angular/core';
|
||||
import { DOCUMENT } from '@angular/common';
|
||||
import { Meta } from '@angular/platform-browser';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class BaseHrefService {
|
||||
export class UrlService {
|
||||
document: Document;
|
||||
meta: Meta;
|
||||
|
||||
constructor() {
|
||||
this.document = inject(DOCUMENT)
|
||||
this.meta = inject(Meta)
|
||||
}
|
||||
|
||||
getBaseHref(): string {
|
||||
const baseElement = this.document.querySelector('base');
|
||||
return baseElement?.getAttribute('href') || '/';
|
||||
}
|
||||
|
||||
getApiUrl(): string | null {
|
||||
const apiMetaTag = this.meta.getTag('name="api-url"');
|
||||
return apiMetaTag ? apiMetaTag.content : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import { InjectionToken } from '@angular/core';
|
||||
|
||||
export const API_URL = new InjectionToken<string>('API_URL');
|
||||
@@ -4,6 +4,7 @@
|
||||
<meta charset="utf-8">
|
||||
<title>signFlow</title>
|
||||
<base href="/">
|
||||
<meta name="api-url" content="/api">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico">
|
||||
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
|
||||
|
||||
Reference in New Issue
Block a user