Backend authentication
This commit is contained in:
@@ -10,6 +10,7 @@ import { LoginPageComponent } from './components/login-page/login-page.component
|
||||
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
|
||||
import { ServiceWorkerModule } from '@angular/service-worker';
|
||||
import { LoaderInterceptor } from './interceptors/loader.interceptor';
|
||||
import { AuthInterceptor } from './interceptors/auth.interceptor';
|
||||
|
||||
|
||||
@NgModule({
|
||||
@@ -37,6 +38,11 @@ import { LoaderInterceptor } from './interceptors/loader.interceptor';
|
||||
useClass: LoaderInterceptor,
|
||||
multi: true
|
||||
},
|
||||
{
|
||||
provide: HTTP_INTERCEPTORS,
|
||||
useClass: AuthInterceptor,
|
||||
multi: true
|
||||
},
|
||||
],
|
||||
bootstrap: [AppComponent]
|
||||
})
|
||||
|
||||
@@ -15,10 +15,9 @@ export class AuthService {
|
||||
private http$: HttpClient
|
||||
) { }
|
||||
|
||||
loadDbUser() {
|
||||
ping() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const headers = new HttpHeaders({
|
||||
// 'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${this.apiToken}`
|
||||
})
|
||||
this.http$.get<any>(`${environment.api.url}/ping/ping`, {
|
||||
@@ -43,7 +42,7 @@ export class AuthService {
|
||||
const header = new HttpHeaders().set('Content-type', 'application/json');
|
||||
this.http$.post<any>(`${environment.api.url}/auth/apiToken`, JSON.stringify(credentials), { headers: header }).subscribe({
|
||||
next: (data) => {
|
||||
console.log('apiToken', data);
|
||||
if (this.user) { this.user.id = data.id }
|
||||
this.apiToken = data.token;
|
||||
resolve(data);
|
||||
},
|
||||
|
||||
@@ -43,7 +43,6 @@ export class LoginPageComponent implements OnInit {
|
||||
|
||||
async handleCredentialResponse(response: any) {
|
||||
try {
|
||||
console.log("Google Response", response);
|
||||
const responsePayload: any = jwt_decode(response.credential);
|
||||
this.data$.currentUser = new User({
|
||||
googleCredentials: response.credential,
|
||||
@@ -51,10 +50,8 @@ export class LoginPageComponent implements OnInit {
|
||||
email: responsePayload.email,
|
||||
avatar: responsePayload.picture
|
||||
});
|
||||
// this.auth$.loadDbUser();
|
||||
console.log("USer", this.data$.currentUser);
|
||||
await this.auth$.getAPIToken(response.credential);
|
||||
// this.auth$.loadDbUser();
|
||||
this.auth$.ping();
|
||||
this.ngZone$.run(() => {
|
||||
this.router$.navigate(['/app']);
|
||||
});
|
||||
|
||||
26
Frontend/src/app/interceptors/auth.interceptor.ts
Normal file
26
Frontend/src/app/interceptors/auth.interceptor.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {
|
||||
HttpRequest,
|
||||
HttpHandler,
|
||||
HttpEvent,
|
||||
HttpInterceptor
|
||||
} from '@angular/common/http';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
@Injectable()
|
||||
export class AuthInterceptor implements HttpInterceptor {
|
||||
constructor(
|
||||
private auth$: AuthService
|
||||
) { }
|
||||
|
||||
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
||||
if (!request.url.includes('/auth/apiToken')) {
|
||||
return next.handle(request.clone({
|
||||
headers: request.headers.set('Authorization', `Bearer ${this.auth$.apiToken}`),
|
||||
}));
|
||||
} else {
|
||||
return next.handle(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,8 @@
|
||||
export const environment = {
|
||||
production: false,
|
||||
api: {
|
||||
//url: "http://localhost:5183/api"
|
||||
url: "https://diuna.bim-it.pl/api"
|
||||
url: "http://localhost:5400/api"
|
||||
//url: "https://diuna.bim-it.pl/api"
|
||||
},
|
||||
google: {
|
||||
clientId: "107631825312-bkfe438ehr9k9ecb2h76g802tj6advma.apps.googleusercontent.com"
|
||||
|
||||
Reference in New Issue
Block a user