2021-07-03 21:48:27 +00:00
|
|
|
import { Component, ViewChild, OnInit, AfterViewInit, ElementRef } from '@angular/core';
|
|
|
|
import { FormBuilder, Validators } from '@angular/forms';
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
|
|
|
import { LoginService } from 'app/login/login.service';
|
|
|
|
import { AccountService } from 'app/core/auth/account.service';
|
2021-07-07 02:49:15 +00:00
|
|
|
import { SocialAuthService, SocialUser } from 'angularx-social-login';
|
2021-07-07 02:19:14 +00:00
|
|
|
import { GoogleLoginProvider } from 'angularx-social-login';
|
2021-07-07 19:31:07 +00:00
|
|
|
import { RegisterService } from '../account/register/register.service';
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2021-07-08 23:25:46 +00:00
|
|
|
import { HttpErrorResponse } from '@angular/common/http';
|
2021-07-23 01:32:39 +00:00
|
|
|
import { EMAIL_ALREADY_USED_TYPE, LOGIN_ALREADY_USED_TYPE, USER_IS_SUSPENDED } from '../config/error.constants';
|
2021-07-11 21:09:53 +00:00
|
|
|
import { LocalStorageService } from 'ngx-webstorage';
|
2021-07-23 19:50:18 +00:00
|
|
|
import { UsuarioExtra } from '../entities/usuario-extra/usuario-extra.model';
|
|
|
|
import { Account } from '../core/auth/account.model';
|
2021-07-03 21:48:27 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'jhi-login',
|
|
|
|
templateUrl: './login.component.html',
|
2021-07-04 01:44:15 +00:00
|
|
|
styleUrls: ['./login.component.scss'],
|
2021-07-03 21:48:27 +00:00
|
|
|
})
|
|
|
|
export class LoginComponent implements OnInit, AfterViewInit {
|
|
|
|
@ViewChild('username', { static: false })
|
|
|
|
username!: ElementRef;
|
|
|
|
|
|
|
|
authenticationError = false;
|
2021-07-08 23:25:46 +00:00
|
|
|
error = false;
|
|
|
|
errorEmailExists = false;
|
|
|
|
errorUserExists = false;
|
2021-07-23 01:32:39 +00:00
|
|
|
userSuspended = false;
|
|
|
|
imprimir = false;
|
2021-07-03 21:48:27 +00:00
|
|
|
|
|
|
|
loginForm = this.fb.group({
|
2021-07-12 05:32:23 +00:00
|
|
|
username: [null, [Validators.required, Validators.email, Validators.maxLength(254)]],
|
2021-07-12 02:35:10 +00:00
|
|
|
password: [null, [Validators.required, Validators.minLength(8), Validators.maxLength(50)]],
|
2021-07-03 21:48:27 +00:00
|
|
|
rememberMe: [false],
|
|
|
|
});
|
|
|
|
|
2021-07-07 02:49:15 +00:00
|
|
|
user: SocialUser = new SocialUser();
|
|
|
|
loggedIn: boolean = false;
|
2021-07-07 19:31:07 +00:00
|
|
|
success = false;
|
2021-07-07 02:49:15 +00:00
|
|
|
|
2021-07-03 21:48:27 +00:00
|
|
|
constructor(
|
2021-07-11 21:09:53 +00:00
|
|
|
private localStorageService: LocalStorageService,
|
2021-07-03 21:48:27 +00:00
|
|
|
private accountService: AccountService,
|
|
|
|
private loginService: LoginService,
|
|
|
|
private router: Router,
|
2021-07-07 02:19:14 +00:00
|
|
|
private fb: FormBuilder,
|
2021-07-07 19:31:07 +00:00
|
|
|
private authService: SocialAuthService,
|
|
|
|
private registerService: RegisterService,
|
2021-07-08 23:25:46 +00:00
|
|
|
private translateService: TranslateService
|
2021-07-03 21:48:27 +00:00
|
|
|
) {}
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
// if already authenticated then navigate to home page
|
|
|
|
this.accountService.identity().subscribe(() => {
|
|
|
|
if (this.accountService.isAuthenticated()) {
|
2021-07-29 05:26:29 +00:00
|
|
|
this.router.navigate(['/pagina-principal']);
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ngAfterViewInit(): void {
|
2021-07-11 23:45:21 +00:00
|
|
|
// this.username.nativeElement.focus();
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 02:49:15 +00:00
|
|
|
//Inicio Google
|
2021-07-07 02:19:14 +00:00
|
|
|
signInWithGoogle(): void {
|
2021-07-09 23:35:18 +00:00
|
|
|
this.authService.signIn(GoogleLoginProvider.PROVIDER_ID).then(() => {
|
|
|
|
this.authService.authState.subscribe(user => {
|
|
|
|
this.user = user;
|
|
|
|
this.loggedIn = user != null;
|
|
|
|
|
|
|
|
/* console.log('correo: ' + user.email);
|
|
|
|
console.log('correo: ' + user.name);
|
|
|
|
console.log('ID: ' + this.user.id);*/
|
|
|
|
|
|
|
|
this.authenticacionGoogle();
|
|
|
|
});
|
|
|
|
});
|
2021-07-07 02:19:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 19:31:07 +00:00
|
|
|
authenticacionGoogle(): void {
|
2021-07-25 04:53:38 +00:00
|
|
|
this.error = false;
|
|
|
|
this.userSuspended = false;
|
|
|
|
|
2021-07-23 21:59:49 +00:00
|
|
|
this.loginService.login({ username: this.user.email, password: this.user.id, rememberMe: false }).subscribe(
|
2021-07-07 19:31:07 +00:00
|
|
|
() => {
|
|
|
|
this.authenticationError = false;
|
|
|
|
if (!this.router.getCurrentNavigation()) {
|
2021-07-11 21:09:53 +00:00
|
|
|
this.localStorageService.store('IsGoogle', 'true');
|
2021-07-07 19:31:07 +00:00
|
|
|
// There were no routing during login (eg from navigationToStoredUrl)
|
2021-07-29 05:26:29 +00:00
|
|
|
this.router.navigate(['/pagina-principal']);
|
2021-07-07 19:31:07 +00:00
|
|
|
}
|
|
|
|
},
|
2021-07-23 19:50:18 +00:00
|
|
|
response => {
|
|
|
|
debugger;
|
|
|
|
if (response.status == 401 && response.error.detail == 'Bad credentials') {
|
|
|
|
this.activateGoogle();
|
|
|
|
} else {
|
|
|
|
this.processError(response);
|
|
|
|
}
|
|
|
|
}
|
2021-07-07 19:31:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
randomProfilePic(): number {
|
|
|
|
return Math.floor(Math.random() * (28 - 1 + 1)) + 1;
|
|
|
|
}
|
|
|
|
|
2021-07-08 23:25:46 +00:00
|
|
|
processError(response: HttpErrorResponse): void {
|
2021-07-23 19:50:18 +00:00
|
|
|
debugger;
|
2021-07-08 23:25:46 +00:00
|
|
|
if (response.status === 400 && response.error.type === LOGIN_ALREADY_USED_TYPE) {
|
|
|
|
this.errorUserExists = true;
|
|
|
|
} else if (response.status === 400 && response.error.type === EMAIL_ALREADY_USED_TYPE) {
|
|
|
|
this.errorEmailExists = true;
|
2021-07-23 19:50:18 +00:00
|
|
|
} else if (response.status === 401) {
|
2021-07-23 01:32:39 +00:00
|
|
|
this.userSuspended = true;
|
2021-07-08 23:25:46 +00:00
|
|
|
} else {
|
|
|
|
this.error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 02:49:15 +00:00
|
|
|
refreshToken(): void {
|
|
|
|
this.authService.refreshAuthToken(GoogleLoginProvider.PROVIDER_ID);
|
|
|
|
}
|
|
|
|
|
2021-07-09 20:32:27 +00:00
|
|
|
activateGoogle(): void {
|
2021-07-25 04:53:38 +00:00
|
|
|
this.error = false;
|
|
|
|
this.userSuspended = false;
|
|
|
|
|
2021-07-09 20:32:27 +00:00
|
|
|
this.registerService
|
|
|
|
.save({
|
|
|
|
login: this.user.email,
|
|
|
|
email: this.user.email,
|
|
|
|
password: this.user.id,
|
|
|
|
langKey: this.translateService.currentLang,
|
|
|
|
name: this.user.name,
|
2021-07-19 00:02:06 +00:00
|
|
|
firstName: 'IsGoogle',
|
2021-07-09 20:32:27 +00:00
|
|
|
profileIcon: this.randomProfilePic(),
|
|
|
|
isAdmin: 0,
|
2021-07-09 23:35:18 +00:00
|
|
|
isGoogle: 1,
|
2021-07-09 20:32:27 +00:00
|
|
|
})
|
|
|
|
.subscribe(
|
2021-07-09 23:35:18 +00:00
|
|
|
() => {
|
|
|
|
this.success = true;
|
|
|
|
this.authenticacionGoogle();
|
|
|
|
},
|
2021-07-09 20:32:27 +00:00
|
|
|
response => this.processError(response)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-03 21:48:27 +00:00
|
|
|
login(): void {
|
2021-07-25 04:53:38 +00:00
|
|
|
this.error = false;
|
|
|
|
this.userSuspended = false;
|
2021-07-23 06:41:12 +00:00
|
|
|
debugger;
|
2021-07-03 21:48:27 +00:00
|
|
|
this.loginService
|
|
|
|
.login({
|
|
|
|
username: this.loginForm.get('username')!.value,
|
|
|
|
password: this.loginForm.get('password')!.value,
|
|
|
|
rememberMe: this.loginForm.get('rememberMe')!.value,
|
|
|
|
})
|
|
|
|
.subscribe(
|
2021-07-23 19:50:18 +00:00
|
|
|
value => {
|
|
|
|
debugger;
|
|
|
|
console.log(value);
|
|
|
|
|
|
|
|
/*if (value?.activated == false){
|
|
|
|
this.userSuspended = true;
|
|
|
|
|
|
|
|
console.log(value.activated)
|
|
|
|
}else {*/
|
2021-07-03 21:48:27 +00:00
|
|
|
this.authenticationError = false;
|
|
|
|
if (!this.router.getCurrentNavigation()) {
|
|
|
|
// There were no routing during login (eg from navigationToStoredUrl)
|
2021-07-29 05:26:29 +00:00
|
|
|
this.router.navigate(['/pagina-principal']);
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|
2021-07-23 19:50:18 +00:00
|
|
|
// }
|
2021-07-03 21:48:27 +00:00
|
|
|
},
|
2021-07-25 04:53:38 +00:00
|
|
|
response => {
|
|
|
|
debugger;
|
|
|
|
if (response.status == 401 && response.error.detail == 'Bad credentials') {
|
|
|
|
this.error = true;
|
|
|
|
} else {
|
|
|
|
this.processError(response);
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 21:48:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|