Login fix and rename columns in DB

This commit is contained in:
2022-12-19 18:36:57 +01:00
parent 120abcaf1d
commit db13b1ab1b
18 changed files with 480 additions and 56 deletions

View File

@@ -1,4 +1,6 @@
import { HttpErrorResponse } from '@angular/common/http';
import { Component, NgZone, OnInit } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { Router } from '@angular/router';
import jwt_decode from "jwt-decode";
import { AuthService } from 'src/app/auth/auth.service';
@@ -14,34 +16,37 @@ import { environment } from 'src/environments/environment';
export class LoginPageComponent implements OnInit {
constructor(
private data$: DataService,
private router$: Router,
private auth$: AuthService,
private ngZone$: NgZone
) {}
private ngZone$: NgZone,
private snackBar$: MatSnackBar
) { }
loading: boolean = false;
ngOnInit(): void {
this.snackBar$.open("", "", { duration: 1 });
// @ts-ignore
window.onGoogleLibraryLoad = () => {
// @ts-ignore
window.onGoogleLibraryLoad = () => {
// @ts-ignore
google.accounts.id.initialize({
client_id: environment.google.clientId,
callback: this.handleCredentialResponse.bind(this),
auto_select: true,
cancel_on_tap_outside: true
});
// @ts-ignore
google.accounts.id.renderButton(
google.accounts.id.initialize({
client_id: environment.google.clientId,
callback: this.handleCredentialResponse.bind(this),
auto_select: true,
cancel_on_tap_outside: true
});
// @ts-ignore
google.accounts.id.renderButton(
// @ts-ignore
document.getElementById("google-button"),
{ theme: "outline", size: "large", width: "100%" }
);
// @ts-ignore
google.accounts.id.prompt((notification: PromptMomentNotification) => {});
};
{ theme: "outline", size: "large", width: "100%" }
);
// @ts-ignore
google.accounts.id.prompt((notification: PromptMomentNotification) => { });
};
}
async handleCredentialResponse(response: any) {
async handleCredentialResponse(response: any) {
try {
const responsePayload: any = jwt_decode(response.credential);
this.auth$.user = new User({
@@ -50,12 +55,20 @@ export class LoginPageComponent implements OnInit {
email: responsePayload.email,
avatar: responsePayload.picture
});
this.ngZone$.run(() => {
this.loading = true;
await this.auth$.getAPIToken(response.credential);
this.ngZone$.run(() => {
this.router$.navigate(['/app']);
});
await this.auth$.getAPIToken(response.credential);
} catch (e) {
console.error('Get user error', e);
} catch (e: any) {
this.loading = false;
if (e.status === 401) {
this.snackBar$.open("Użytkownik nie istnieje w bazie danych aplikacji Diuna.", "", { duration: 3000 });
} else {
this.snackBar$.open("Błąd połączenia z serwerem. Skontaktuj się z administratorem.", "", { duration: 3000 });
}
} finally {
this.loading = false;
}
}
}