2022-12-05 14:20:34 +01:00
|
|
|
import { Component, 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-05 17:42:52 +01:00
|
|
|
import { User } from 'src/app/models/user';
|
|
|
|
|
import { DataService } from 'src/app/services/data.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-05 17:42:52 +01:00
|
|
|
private data$: DataService,
|
2022-12-06 12:27:09 +01:00
|
|
|
private router$: Router,
|
|
|
|
|
private auth$: AuthService
|
2022-12-05 14:20:34 +01:00
|
|
|
) {}
|
2022-11-30 10:44:58 +01:00
|
|
|
|
2022-12-05 14:20:34 +01:00
|
|
|
ngOnInit(): void {
|
|
|
|
|
// @ts-ignore
|
2022-12-06 12:27:09 +01:00
|
|
|
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(
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
document.getElementById("google-button"),
|
|
|
|
|
{ theme: "outline", size: "large", width: "100%" }
|
|
|
|
|
);
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
google.accounts.id.prompt((notification: PromptMomentNotification) => {});
|
|
|
|
|
};
|
2022-12-05 14:20:34 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-05 17:42:52 +01:00
|
|
|
async handleCredentialResponse(response: any) {
|
|
|
|
|
try {
|
2022-12-06 12:27:09 +01:00
|
|
|
console.log("Google Response", response);
|
2022-12-05 17:42:52 +01:00
|
|
|
const responsePayload: any = jwt_decode(response.credential);
|
|
|
|
|
this.data$.currentUser = 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-06 12:27:09 +01:00
|
|
|
// this.auth$.loadDbUser();
|
2022-12-05 17:42:52 +01:00
|
|
|
this.router$.navigate(['/app']);
|
2022-12-06 12:27:09 +01:00
|
|
|
console.log("USer", this.data$.currentUser);
|
|
|
|
|
await this.auth$.getAPIToken(response.credential);
|
|
|
|
|
this.auth$.loadDbUser();
|
2022-12-05 17:42:52 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Get user error', e);
|
|
|
|
|
}
|
2022-12-05 14:20:34 +01:00
|
|
|
}
|
2022-11-30 10:44:58 +01:00
|
|
|
}
|
2022-12-05 14:20:34 +01:00
|
|
|
|