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

chore(ng-auth): handle refresh token with tokenLogin function #22

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/ng-auth/src/lib/services/authentication.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,46 @@ describe('AuthenticationService', () => {
})
})

it('should login with tokenLogin if accessToken and refreshToken are valid', (done) => {
const user = { name: 'Foo', surname: 'Bar', email: '[email protected]' }
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' }

Expand Down
14 changes: 11 additions & 3 deletions packages/ng-auth/src/lib/services/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@ export class AuthenticationService {
)
}

public tokenLogin(token: string): Observable<UserType> {
this.setAuthenticationToken(token)
public tokenLogin(
token: string,
refreshToken?: string
): Observable<UserType> {
this.setAuthenticationToken(token, refreshToken)
return this.getAuthenticatedUser(true).pipe(
catchError((error) => {
this.events$.next(new AuthenticationEvent('login-failed', null))
Expand All @@ -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)
}
}

Expand Down
Loading