Merge branch 'develop' of ssh://ognproject.evlt.uma.es:21987/opengnsys/oggui into develop
testing/ogGui-multibranch/pipeline/head There was a failure building this commit
Details
testing/ogGui-multibranch/pipeline/head There was a failure building this commit
Details
commit
d1bf12dd6a
|
@ -56,7 +56,7 @@ describe('LoginComponent', () => {
|
||||||
|
|
||||||
it('should call onLogin and navigate on successful login', () => {
|
it('should call onLogin and navigate on successful login', () => {
|
||||||
const mockRouter = spyOn(component['router'], 'navigateByUrl');
|
const mockRouter = spyOn(component['router'], 'navigateByUrl');
|
||||||
const mockHttp = spyOn(component['http'], 'post').and.returnValue(of({ token: '123', refreshToken: '456' }));
|
const mockHttp = spyOn(component['http'], 'post').and.returnValue(of({ token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c', refreshToken: '456' }));
|
||||||
|
|
||||||
component.loginObj.username = 'testUser';
|
component.loginObj.username = 'testUser';
|
||||||
component.loginObj.password = 'testPass';
|
component.loginObj.password = 'testPass';
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import { HttpClient } from '@angular/common/http';
|
import { HttpClient } from '@angular/common/http';
|
||||||
import {Component, signal} from '@angular/core';
|
import { Component, signal } from '@angular/core';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
import { TranslateService } from '@ngx-translate/core';
|
import { TranslateService } from '@ngx-translate/core';
|
||||||
import {ToastrService} from "ngx-toastr";
|
import { ToastrService } from "ngx-toastr";
|
||||||
import {jwtDecode} from "jwt-decode";
|
import { jwtDecode } from "jwt-decode";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-login',
|
selector: 'app-login',
|
||||||
|
@ -11,69 +11,69 @@ import {jwtDecode} from "jwt-decode";
|
||||||
styleUrls: ['./login.component.css'],
|
styleUrls: ['./login.component.css'],
|
||||||
})
|
})
|
||||||
export class LoginComponent {
|
export class LoginComponent {
|
||||||
loginObj: any = {
|
loginObj: any = {
|
||||||
"username": "",
|
"username": "",
|
||||||
"password": ""
|
"password": ""
|
||||||
};
|
};
|
||||||
errorMessage: string = '';
|
errorMessage: string = '';
|
||||||
isLoading: boolean = false;
|
isLoading: boolean = false;
|
||||||
decodedToken: any;
|
decodedToken: any;
|
||||||
|
|
||||||
baseUrl: string = import.meta.env.NG_APP_BASE_API_URL;
|
baseUrl: string = import.meta.env.NG_APP_BASE_API_URL;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private http: HttpClient,
|
private http: HttpClient,
|
||||||
private router: Router,
|
private router: Router,
|
||||||
private toastService: ToastrService,
|
private toastService: ToastrService,
|
||||||
private translateService: TranslateService
|
private translateService: TranslateService
|
||||||
) {
|
) {
|
||||||
const savedLanguage = localStorage.getItem('language') || 'es';
|
const savedLanguage = localStorage.getItem('language') || 'es';
|
||||||
this.translateService.use(savedLanguage);
|
this.translateService.use(savedLanguage);
|
||||||
}
|
}
|
||||||
|
|
||||||
onLogin() {
|
onLogin() {
|
||||||
this.errorMessage = '';
|
this.errorMessage = '';
|
||||||
this.isLoading = true;
|
this.isLoading = true;
|
||||||
|
|
||||||
if (!this.loginObj.username || !this.loginObj.password) {
|
if (!this.loginObj.username || !this.loginObj.password) {
|
||||||
if (!this.loginObj.username) {
|
if (!this.loginObj.username) {
|
||||||
document.getElementById('username')?.classList.add('invalid');
|
document.getElementById('username')?.classList.add('invalid');
|
||||||
} else {
|
} else {
|
||||||
document.getElementById('username')?.classList.remove('invalid');
|
document.getElementById('username')?.classList.remove('invalid');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.loginObj.password) {
|
if (!this.loginObj.password) {
|
||||||
document.getElementById('password')?.classList.add('invalid');
|
document.getElementById('password')?.classList.add('invalid');
|
||||||
} else {
|
} else {
|
||||||
document.getElementById('password')?.classList.remove('invalid');
|
document.getElementById('password')?.classList.remove('invalid');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.isLoading = false;
|
this.isLoading = false;
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
this.http.post(`${this.baseUrl}/auth/login`, this.loginObj).subscribe({
|
|
||||||
next: (res: any) => {
|
|
||||||
if (res.token) {
|
|
||||||
localStorage.setItem('loginToken', res.token);
|
|
||||||
localStorage.setItem('refreshToken', res.refreshToken);
|
|
||||||
localStorage.setItem('username', this.loginObj.username);
|
|
||||||
|
|
||||||
this.decodedToken = jwtDecode(res.token);
|
|
||||||
localStorage.setItem('groupsView', this.decodedToken.groupsView);
|
|
||||||
|
|
||||||
this.openSnackBar(false, 'Bienvenido ' + this.loginObj.username);
|
|
||||||
this.router.navigateByUrl('/groups');
|
|
||||||
}
|
|
||||||
this.isLoading = false;
|
|
||||||
},
|
|
||||||
error: (err) => {
|
|
||||||
this.isLoading = false;
|
|
||||||
this.openSnackBar(true, 'Error al iniciar sesión: ' + err.error.message);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.http.post(`${this.baseUrl}/auth/login`, this.loginObj).subscribe({
|
||||||
|
next: (res: any) => {
|
||||||
|
if (res.token) {
|
||||||
|
localStorage.setItem('loginToken', res.token);
|
||||||
|
localStorage.setItem('refreshToken', res.refreshToken);
|
||||||
|
localStorage.setItem('username', this.loginObj.username);
|
||||||
|
|
||||||
|
this.decodedToken = jwtDecode(res.token);
|
||||||
|
localStorage.setItem('groupsView', this.decodedToken.groupsView);
|
||||||
|
|
||||||
|
this.openSnackBar(false, 'Bienvenido ' + this.loginObj.username);
|
||||||
|
this.router.navigateByUrl('/groups');
|
||||||
|
}
|
||||||
|
this.isLoading = false;
|
||||||
|
},
|
||||||
|
error: (err) => {
|
||||||
|
this.isLoading = false;
|
||||||
|
this.openSnackBar(true, 'Error al iniciar sesión: ' + err.error.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
hide = signal(true);
|
hide = signal(true);
|
||||||
clickEvent(event: Event) {
|
clickEvent(event: Event) {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
|
|
Loading…
Reference in New Issue