Agregar servicio de loggeo Google

This commit is contained in:
Paola 2021-07-06 20:49:15 -06:00
parent b35d77f637
commit 08a1cc5c32
2 changed files with 16 additions and 26 deletions

View File

@ -4,7 +4,7 @@ import { Router } from '@angular/router';
import { LoginService } from 'app/login/login.service'; import { LoginService } from 'app/login/login.service';
import { AccountService } from 'app/core/auth/account.service'; import { AccountService } from 'app/core/auth/account.service';
import { SocialAuthService } from 'angularx-social-login'; import { SocialAuthService, SocialUser } from 'angularx-social-login';
import { GoogleLoginProvider } from 'angularx-social-login'; import { GoogleLoginProvider } from 'angularx-social-login';
@Component({ @Component({
@ -24,6 +24,9 @@ export class LoginComponent implements OnInit, AfterViewInit {
rememberMe: [false], rememberMe: [false],
}); });
user: SocialUser = new SocialUser();
loggedIn: boolean = false;
constructor( constructor(
private accountService: AccountService, private accountService: AccountService,
private loginService: LoginService, private loginService: LoginService,
@ -33,7 +36,12 @@ export class LoginComponent implements OnInit, AfterViewInit {
) {} ) {}
ngOnInit(): void { ngOnInit(): void {
//console.log("Google ID: "+ GoogleLoginProvider.PROVIDER_ID) //Servicio para verificar si el usuario se encuentra loggeado
this.authService.authState.subscribe(user => {
this.user = user;
this.loggedIn = user != null;
});
// if already authenticated then navigate to home page // if already authenticated then navigate to home page
this.accountService.identity().subscribe(() => { this.accountService.identity().subscribe(() => {
if (this.accountService.isAuthenticated()) { if (this.accountService.isAuthenticated()) {
@ -46,10 +54,15 @@ export class LoginComponent implements OnInit, AfterViewInit {
this.username.nativeElement.focus(); this.username.nativeElement.focus();
} }
//Inicio Google
signInWithGoogle(): void { signInWithGoogle(): void {
this.authService.signIn(GoogleLoginProvider.PROVIDER_ID); this.authService.signIn(GoogleLoginProvider.PROVIDER_ID);
} }
refreshToken(): void {
this.authService.refreshAuthToken(GoogleLoginProvider.PROVIDER_ID);
}
login(): void { login(): void {
this.loginService this.loginService
.login({ .login({

View File

@ -5,28 +5,5 @@ import { Observable, ReplaySubject } from 'rxjs';
providedIn: 'root', providedIn: 'root',
}) })
export class UsuarioGoogleLogInService { export class UsuarioGoogleLogInService {
constructor(private auth2: gapi.auth2.GoogleAuth, private subject: ReplaySubject<gapi.auth2.GoogleUser>) { constructor() {}
gapi.load('auth2', () => {
this.auth2 = gapi.auth2.init({
client_id: '178178891217-b517thad8f15d4at2vk2410v7a09dcvt.apps.googleusercontent.com',
});
});
}
public sigIn() {
this.auth2
.signIn({
//
})
.then(user => {
this.subject.next(user);
})
.catch(() => {
this.subject.next(); //NULL
});
}
public observable(): Observable<gapi.auth2.GoogleUser> {
return this.subject.asObservable();
}
} }