Refresh api token
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import moment, { Moment } from 'moment';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { User } from '../models/user.model';
|
||||
|
||||
@@ -8,8 +9,11 @@ import { User } from '../models/user.model';
|
||||
})
|
||||
export class AuthService {
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
public googleCredential!: any;
|
||||
apiToken!: string;
|
||||
user!: User;
|
||||
expirationTime!: Moment;
|
||||
|
||||
constructor(
|
||||
private http$: HttpClient,
|
||||
@@ -30,14 +34,19 @@ export class AuthService {
|
||||
});
|
||||
}
|
||||
|
||||
getAPIToken(credentials: string): Promise<void> {
|
||||
getAPIToken(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const header = new HttpHeaders().set('Content-type', 'application/json');
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
this.http$.post<any>(`${environment.api.url}/auth/apiToken`, JSON.stringify(credentials), { headers: header }).subscribe({
|
||||
this.http$.post<any>(`${environment.api.url}/auth/apiToken`, JSON.stringify(this.googleCredential), { headers: header }).subscribe({
|
||||
next: (data) => {
|
||||
this.user.id = data.id;
|
||||
this.apiToken = data.token;
|
||||
this.expirationTime = moment(data.expirationTime);
|
||||
const ms = this.expirationTime.subtract(29, 'minutes').diff(moment.utc(), 'milliseconds');
|
||||
setTimeout(() => {
|
||||
this.getAPIToken();
|
||||
}, ms);
|
||||
resolve(data);
|
||||
},
|
||||
error: (e: HttpErrorResponse) => {
|
||||
|
||||
@@ -62,8 +62,8 @@ export class LoginPageComponent implements OnInit {
|
||||
this.ngZone$.run(() => {
|
||||
this.loading = true;
|
||||
});
|
||||
await this.sleep(2500);
|
||||
await this.auth$.getAPIToken(response.credential);
|
||||
this.auth$.googleCredential = response.credential;
|
||||
await this.auth$.getAPIToken();
|
||||
this.ngZone$.run(() => {
|
||||
this.router$.navigate(['/app']);
|
||||
});
|
||||
@@ -94,8 +94,5 @@ export class LoginPageComponent implements OnInit {
|
||||
this.loading = false;
|
||||
}
|
||||
}
|
||||
sleep(ms: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ export const environment = {
|
||||
appEnvironment: "local",
|
||||
production: false,
|
||||
api: {
|
||||
//url: "http://localhost:5400/api"
|
||||
url: "https://diunabi.bim-it.pl/api"
|
||||
url: "http://localhost:5400/api"
|
||||
//url: "https://diunabi.bim-it.pl/api"
|
||||
},
|
||||
google: {
|
||||
clientId: "107631825312-bkfe438ehr9k9ecb2h76g802tj6advma.apps.googleusercontent.com"
|
||||
|
||||
Reference in New Issue
Block a user