Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

111 popup para relogar #37

Merged
merged 12 commits into from
Dec 9, 2023
45 changes: 45 additions & 0 deletions src/app/pages/profile/profile.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class AlertServiceMock {

class AuthServiceMock {
logout() { }
refreshToken() { }
showRenewTokenDialog() { }
}

class ConfirmationServiceMock {
Expand Down Expand Up @@ -125,6 +127,49 @@ describe('ProfileComponent', () => {
expect(mySpy).toHaveBeenCalled();
});

it('should call refreshToken and set token in localStorage', () => {
spyOn(authService, 'refreshToken').and.returnValue(of({ access_token: 'new_access_token' }));

component.renewToken();

expect(authService.refreshToken).toHaveBeenCalled();
expect(localStorage.getItem('token')).toEqual('new_access_token');
});

it('should handle error when renewing token', () => {
spyOn(authService, 'refreshToken').and.returnValue(throwError('error'));
spyOn(console, 'error');

component.renewToken();

expect(authService.refreshToken).toHaveBeenCalled();
expect(console.error).toHaveBeenCalledWith('Failed to refresh token:', 'error');
});
it('should show renew token dialog', () => {
const confirmSpy = spyOn(confirmationService, 'confirm').and.callFake((params: any) => {
params.accept();
params.reject();

// Return a mock ConfirmationService instance
return {} as ConfirmationService;
});
const renewTokenSpy = spyOn(component, 'renewToken');
const logoutSpy = spyOn(authService, 'logout');

component.showRenewTokenDialog();

expect(confirmSpy).toHaveBeenCalledWith({
message: 'Deseja se manter logado?',
header: 'Confirmação',
key: 'myDialog',
icon: 'pi pi-exclamation-triangle',
accept: jasmine.any(Function),
reject: jasmine.any(Function),
});
expect(renewTokenSpy).toHaveBeenCalled();
expect(logoutSpy).toHaveBeenCalled();
});

it('should call navigatorEdit when editUser is clicked', () => {
spyOn(component, 'navigatorEdit').and.callThrough();
const navigateSpy = spyOn(router, 'navigate');
Expand Down
35 changes: 35 additions & 0 deletions src/app/pages/profile/profile.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AlertService } from 'src/app/services/alert.service';
import { ConfirmationService, MessageService } from 'primeng/api';
import { AuthService } from 'src/app/services/auth.service';
import { HttpErrorResponse } from '@angular/common/http';
import { take, timer } from 'rxjs';

type ErrorResponseType = HttpErrorResponse;

Expand All @@ -32,6 +33,11 @@ export class ProfileComponent {
ngOnInit(): void {
this.setUserIdFromToken(localStorage.getItem('token') as string);
this.getUser();
timer(15 * 60 * 1000)
.pipe(take(1))
.subscribe(() => {
this.showRenewTokenDialog();
});
}

setUserIdFromToken(token: string) {
Expand Down Expand Up @@ -91,6 +97,35 @@ export class ProfileComponent {
});
}

showRenewTokenDialog() {
this.confirmationService.confirm({
message: 'Deseja se manter logado?',
header: 'Confirmação',
key: 'myDialog',
icon: 'pi pi-exclamation-triangle',
accept: () => {
this.renewToken();
},
reject: () => {
this.authService.logout();
},
});
}

renewToken() {
this.authService.refreshToken().subscribe({
next: (response) => {
if (response?.access_token) {
localStorage.setItem('token', response.access_token);
}
},
error: (error: ErrorResponseType) => {
console.error('Failed to refresh token:', error);
this.authService.logout();
}
});
}

navigatorEdit(): void {
this.router.navigate([`/editUser/${this.user.id}`]);
}
Expand Down
12 changes: 12 additions & 0 deletions src/app/services/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ describe('AuthService', () => {
req.flush(userResponse);
});

it('should refresh token', () => {
const dummyResponse = { access_token: 'dummyToken' };

service.refreshToken().subscribe(res => {
expect(res).toEqual(dummyResponse);
});

const req = httpMock.expectOne(`${service.usersAPIURL}/auth/refresh`);
expect(req.request.method).toBe('POST');
req.flush(dummyResponse);
});

it('should logout', () => {
localStorage.setItem('token', 'testtoken');
service.logout();
Expand Down
6 changes: 5 additions & 1 deletion src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export class AuthService {
const token = localStorage.getItem('token');
return !!token;
}


refreshToken(): Observable<any> {
return this.http.post(`${this.usersAPIURL}/auth/refresh`, null);
}

logout(): void {
localStorage.removeItem('token');
this.router.navigate(['/login']);
Expand Down
Loading