Files
DiunaBI/Frontend/src/app/views/login/login-view.component.ts

101 lines
3.0 KiB
TypeScript
Raw Normal View History

2023-02-22 12:12:38 +01:00
import { Component, NgZone, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from 'src/app/auth/auth.service';
import { NotificationsService } from 'src/app/services/notifications.service';
2023-06-23 15:53:55 +02:00
import { environment } from 'src/environments/environment';
import { MatCardModule } from '@angular/material/card';
2023-06-23 15:32:59 +02:00
import { NgIf } from '@angular/common';
2023-06-23 15:53:55 +02:00
import { MatBottomSheetModule } from '@angular/material/bottom-sheet';
2023-02-22 12:12:38 +01:00
2023-06-23 15:53:55 +02:00
@Component({
2023-11-07 21:32:27 +01:00
selector: 'diunabi-view-page',
templateUrl: './login-view.component.html',
styleUrls: ['./login-view.component.scss'],
standalone: true,
imports: [NgIf, MatCardModule, MatBottomSheetModule]
2023-02-22 12:12:38 +01:00
})
2023-08-22 18:32:17 +02:00
export class LoginViewComponent implements OnInit {
2023-02-22 12:12:38 +01:00
constructor(
private router$: Router,
private auth$: AuthService,
private ngZone$: NgZone,
private notifications$: NotificationsService
) { }
loading = false;
ngOnInit(): void {
2023-11-07 21:32:27 +01:00
this.auth$.isGoogleLibLoaded.subscribe((isLoaded) => {
if (isLoaded) {
this.initGoogleLogin();
}
});
}
initGoogleLogin() {
2023-02-22 12:12:38 +01:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
2023-11-07 21:32:27 +01:00
google.accounts.id.initialize({
client_id: environment.google.clientId,
callback: this.handleCredentialResponse.bind(this),
auto_select: true,
cancel_on_tap_outside: true
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
google.accounts.id.renderButton(
2023-02-22 12:12:38 +01:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
2023-11-07 21:32:27 +01:00
document.getElementById("google-button"),
{ theme: "outline", size: "large", width: "100%" }
);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
google.accounts.id.prompt();
2023-02-22 12:12:38 +01:00
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async handleCredentialResponse(response: any) {
try {
2023-11-07 14:29:58 +01:00
this.auth$.retrieveUserFromCredentials(response.credential);
this.auth$.webCredentials = response.credential;
2023-06-24 13:46:37 +02:00
this.ngZone$.run(() => {
this.loading = true;
});
2023-02-22 12:12:38 +01:00
await this.auth$.getAPIToken();
2023-06-24 13:59:01 +02:00
this.ngZone$.run(() => {
this.router$.navigate(['/app']);
});
2023-02-22 12:12:38 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
console.error('handleCredentialResponse', e);
this.ngZone$.run(() => {
this.loading = false;
});
if (e.status === 401) {
this.ngZone$.run(() => {
this.notifications$.add({
text: "User not exists in DiunaBI database.",
btn: "OK",
duration: 15000
});
});
} else {
this.ngZone$.run(() => {
this.notifications$.add({
text: "DiunaBI server not responded.",
btn: "OK",
duration: 15000
});
});
}
} finally {
this.loading = false;
}
2023-11-07 21:32:27 +01:00
2023-02-22 12:12:38 +01:00
}
}