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

83 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-12-06 16:28:59 +01:00
import { Component, NgZone, OnInit } from '@angular/core';
2022-12-19 18:36:57 +01:00
import { MatSnackBar } from '@angular/material/snack-bar';
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';
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,
private snackBar$: MatSnackBar
) { }
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 {
2022-12-19 18:36:57 +01:00
this.snackBar$.open("", "", { duration: 1 });
2023-01-07 13:39:30 +01:00
console.log('Envs', environment);
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
});
2022-12-19 18:36:57 +01:00
this.loading = true;
await this.auth$.getAPIToken(response.credential);
this.ngZone$.run(() => {
2022-12-06 16:28:59 +01:00
this.router$.navigate(['/app']);
});
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
} catch (e: any) {
this.loading = false;
if (e.status === 401) {
2023-01-04 15:45:45 +01:00
this.snackBar$.open("Użytkownik nie istnieje w bazie danych aplikacji DiunaBI.", "", { duration: 3000 });
2022-12-19 18:36:57 +01:00
} else {
this.snackBar$.open("Błąd połączenia z serwerem. Skontaktuj się z administratorem.", "", { duration: 3000 });
}
} 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