Frontent login

This commit is contained in:
2022-12-05 17:42:52 +01:00
parent 9f6c99a12e
commit 7330fb90f2
11 changed files with 65 additions and 96 deletions

View File

@@ -1,14 +1,19 @@
import { GoogleLoginProvider, SocialAuthService } from '@abacritt/angularx-social-login';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import jwt_decode from "jwt-decode";
import { User } from 'src/app/models/user';
import { DataService } from 'src/app/services/data.service';
@Component({
selector: 'app-login-page',
templateUrl: './login-page.component.html',
styleUrls: ['./login-page.component.scss']
})
export class LoginPageComponent implements OnInit {
constructor(
private authService: SocialAuthService
private data$: DataService,
private router$: Router
) {}
ngOnInit(): void {
@@ -16,9 +21,8 @@ export class LoginPageComponent implements OnInit {
google.accounts.id.initialize({
client_id: "107631825312-bkfe438ehr9k9ecb2h76g802tj6advma.apps.googleusercontent.com",
callback: this.handleCredentialResponse.bind(this),
auto_select: false,
auto_select: true,
cancel_on_tap_outside: true,
});
// @ts-ignore
google.accounts.id.renderButton(
@@ -27,17 +31,23 @@ export class LoginPageComponent implements OnInit {
{ theme: "outline", size: "large", width: "100%" }
);
// @ts-ignore
google.accounts.id.prompt((notification: PromptMomentNotification) => { });
google.accounts.id.prompt();
}
async handleCredentialResponse(response: any) {
// Here will be your response from Google.
console.log(response);
// const responsePayload = decodeJwtResponse(response.credential);
}
login() {
this.authService.signIn(GoogleLoginProvider.PROVIDER_ID);
async handleCredentialResponse(response: any) {
try {
const responsePayload: any = jwt_decode(response.credential);
this.data$.currentUser = new User({
id: 1,
googledId: responsePayload.aud,
userName: `${responsePayload.given_name} ${responsePayload.family_name}`,
email: responsePayload.email,
avatar: responsePayload.picture
});
this.router$.navigate(['/app']);
} catch (e) {
console.error('Get user error', e);
}
}
}