From f32dcc4a684966e070ecee9a5b098c880048c9e2 Mon Sep 17 00:00:00 2001 From: Ivan Morandi Date: Mon, 9 Oct 2023 10:12:54 +0200 Subject: [PATCH 1/2] chore(ng-auth): handle refresh token with tokenLogin function --- .../services/authentication.service.spec.ts | 40 +++++++++++++++++++ .../lib/services/authentication.service.ts | 14 +++++-- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/packages/ng-auth/src/lib/services/authentication.service.spec.ts b/packages/ng-auth/src/lib/services/authentication.service.spec.ts index 7268e4e..339a0e3 100644 --- a/packages/ng-auth/src/lib/services/authentication.service.spec.ts +++ b/packages/ng-auth/src/lib/services/authentication.service.spec.ts @@ -93,6 +93,46 @@ describe('AuthenticationService', () => { }) }) + it('should login if accessToken and refreshToken are valid', (done) => { + const user = { name: 'Foo', surname: 'Bar', email: 'foo@bar.com' } + const tokenPair = { + accessToken: 'access-token', + refreshToken: 'refresh-token' + } + + const fetchUserSpy = jest + .spyOn(authProvider, 'fetchUser') + .mockReturnValueOnce(of(user)) + + expect(service.isAuthenticated()).toBeFalsy() + + service + .tokenLogin(tokenPair.accessToken, tokenPair.refreshToken) + .subscribe((user) => { + expect(user).toEqual(user) + + expect( + storageProvider.retrieve( + storagePrefix + '-' + service.AUTH_ACCESS_TOKEN + ) + ).toEqual(tokenPair.accessToken) + expect( + storageProvider.retrieve( + storagePrefix + '-' + service.AUTH_REFRESH_TOKEN + ) + ).toEqual(tokenPair.refreshToken) + + expect(fetchUserSpy.mock.calls.length).toEqual(1) + + expect(service.isAuthenticated()).toBeTruthy() + service.getAuthenticationState().subscribe((auth) => { + expect(auth).toBeTruthy() + expect(auth).toEqual(user) + done() + }) + }) + }) + it('should not login if credentials are invalid', (done) => { const authError = { error: 'an-error' } diff --git a/packages/ng-auth/src/lib/services/authentication.service.ts b/packages/ng-auth/src/lib/services/authentication.service.ts index c95185a..c7e35d8 100644 --- a/packages/ng-auth/src/lib/services/authentication.service.ts +++ b/packages/ng-auth/src/lib/services/authentication.service.ts @@ -110,8 +110,11 @@ export class AuthenticationService { ) } - public tokenLogin(token: string): Observable { - this.setAuthenticationToken(token) + public tokenLogin( + token: string, + refreshToken?: string + ): Observable { + this.setAuthenticationToken(token, refreshToken) return this.getAuthenticatedUser(true).pipe( catchError((error) => { this.events$.next(new AuthenticationEvent('login-failed', null)) @@ -121,13 +124,18 @@ export class AuthenticationService { ) } - public setAuthenticationToken(authenticationToken: string): void { + public setAuthenticationToken( + authenticationToken: string, + refreshToken?: string + ): void { if (authenticationToken) { this.store(this.AUTH_IS_AUTHENTICATED, JSON.stringify(new Date())) this.store(this.AUTH_ACCESS_TOKEN, authenticationToken) + this.store(this.AUTH_REFRESH_TOKEN, refreshToken) } else { this.clear(this.AUTH_ACCESS_TOKEN) this.clear(this.AUTH_IS_AUTHENTICATED) + this.clear(this.AUTH_REFRESH_TOKEN) } } From 9cc496bb01dfef6934d89925ecc0c85ef514dee4 Mon Sep 17 00:00:00 2001 From: Ivan Morandi Date: Mon, 9 Oct 2023 10:13:25 +0200 Subject: [PATCH 2/2] chore(ng-auth): handle refresh token with tokenLogin function --- .../ng-auth/src/lib/services/authentication.service.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ng-auth/src/lib/services/authentication.service.spec.ts b/packages/ng-auth/src/lib/services/authentication.service.spec.ts index 339a0e3..c8ffcf9 100644 --- a/packages/ng-auth/src/lib/services/authentication.service.spec.ts +++ b/packages/ng-auth/src/lib/services/authentication.service.spec.ts @@ -93,7 +93,7 @@ describe('AuthenticationService', () => { }) }) - it('should login if accessToken and refreshToken are valid', (done) => { + it('should login with tokenLogin if accessToken and refreshToken are valid', (done) => { const user = { name: 'Foo', surname: 'Bar', email: 'foo@bar.com' } const tokenPair = { accessToken: 'access-token',