Files
DiunaBI/Frontend/src/app/components/login-page/login-page.component.ts

99 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-12-06 16:28:59 +01:00
import { Component, NgZone, OnInit } from '@angular/core';
2022-12-05 17:42:52 +01:00
import { Router } from '@angular/router';
import jwt_decode from "jwt-decode";
2022-12-06 12:27:09 +01:00
import { AuthService } from 'src/app/auth/auth.service';
2022-12-11 23:40:16 +01:00
import { User } from 'src/app/models/user.model';
2023-01-08 16:57:21 +01:00
import { NotificationsService } from 'src/app/services/notifications.service';
2022-12-06 12:27:09 +01:00
import { environment } from 'src/environments/environment';
2022-11-30 10:44:58 +01:00
@Component({
selector: 'app-login-page',
templateUrl: './login-page.component.html',
styleUrls: ['./login-page.component.scss']
})
2022-12-05 17:42:52 +01:00
2022-12-05 14:20:34 +01:00
export class LoginPageComponent implements OnInit {
constructor(
2022-12-06 12:27:09 +01:00
private router$: Router,
2022-12-06 16:28:59 +01:00
private auth$: AuthService,
2022-12-19 18:36:57 +01:00
private ngZone$: NgZone,
2023-01-08 16:57:21 +01:00
private notifications$: NotificationsService
2022-12-19 18:36:57 +01:00
) { }
2023-01-06 11:55:01 +01:00
loading = false;
2022-11-30 10:44:58 +01:00
2022-12-05 14:20:34 +01:00
ngOnInit(): void {
2023-01-06 11:55:01 +01:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2022-12-19 18:36:57 +01:00
// @ts-ignore
window.onGoogleLibraryLoad = () => {
2023-01-06 11:55:01 +01:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2022-12-05 14:20:34 +01:00
// @ts-ignore
2022-12-19 18:36:57 +01:00
google.accounts.id.initialize({
client_id: environment.google.clientId,
callback: this.handleCredentialResponse.bind(this),
auto_select: true,
cancel_on_tap_outside: true
});
2023-01-06 11:55:01 +01:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2022-12-19 18:36:57 +01:00
// @ts-ignore
google.accounts.id.renderButton(
2023-01-06 11:55:01 +01:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2022-12-06 12:27:09 +01:00
// @ts-ignore
document.getElementById("google-button"),
2022-12-19 18:36:57 +01:00
{ theme: "outline", size: "large", width: "100%" }
);
2023-01-06 11:55:01 +01:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2022-12-19 18:36:57 +01:00
// @ts-ignore
2023-01-06 11:55:01 +01:00
google.accounts.id.prompt();
2022-12-19 18:36:57 +01:00
};
2022-12-05 14:20:34 +01:00
}
2023-01-06 11:55:01 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2022-12-19 18:36:57 +01:00
async handleCredentialResponse(response: any) {
2022-12-05 17:42:52 +01:00
try {
2023-01-06 11:55:01 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2022-12-05 17:42:52 +01:00
const responsePayload: any = jwt_decode(response.credential);
2022-12-11 23:40:16 +01:00
this.auth$.user = new User({
2022-12-06 12:27:09 +01:00
googleCredentials: response.credential,
2022-12-05 17:42:52 +01:00
userName: `${responsePayload.given_name} ${responsePayload.family_name}`,
email: responsePayload.email,
avatar: responsePayload.picture
});
2023-01-08 17:33:22 +01:00
this.ngZone$.run(() => {
this.loading = true;
});
2023-01-08 18:19:49 +01:00
this.auth$.googleCredential = response.credential;
await this.auth$.getAPIToken();
2022-12-19 18:36:57 +01:00
this.ngZone$.run(() => {
2022-12-06 16:28:59 +01:00
this.router$.navigate(['/app']);
});
2023-01-08 16:57:21 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2022-12-19 18:36:57 +01:00
} catch (e: any) {
2023-01-08 16:57:21 +01:00
console.error('handleCredentialResponse', e);
2023-01-08 17:33:22 +01:00
this.ngZone$.run(() => {
this.loading = false;
});
2022-12-19 18:36:57 +01:00
if (e.status === 401) {
2023-01-08 17:33:22 +01:00
this.ngZone$.run(() => {
this.notifications$.add({
2023-01-12 14:47:38 +01:00
text: "User not exists in DiunaBI database.",
2023-01-08 17:33:22 +01:00
btn: "OK",
duration: 15000
});
2023-01-08 16:57:21 +01:00
});
2022-12-19 18:36:57 +01:00
} else {
2023-01-08 17:33:22 +01:00
this.ngZone$.run(() => {
this.notifications$.add({
2023-01-12 18:14:40 +01:00
text: "DiunaBI server not responded.",
2023-01-08 17:33:22 +01:00
btn: "OK",
duration: 15000
});
2023-01-08 16:57:21 +01:00
});
2022-12-19 18:36:57 +01:00
}
} finally {
this.loading = false;
2022-12-05 17:42:52 +01:00
}
2022-12-05 14:20:34 +01:00
}
2022-11-30 10:44:58 +01:00
}
2022-12-05 14:20:34 +01:00