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