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

Redirect even when authData is present #505

Closed
wants to merge 1 commit into from
Closed
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
57 changes: 56 additions & 1 deletion projects/angular-token/src/lib/angular-token.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpClientModule } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { Router } from '@angular/router';

import { AngularTokenModule } from './angular-token.module';
import { AngularTokenService } from './angular-token.service';
Expand Down Expand Up @@ -145,7 +146,11 @@ describe('AngularTokenService', () => {
AngularTokenModule.forRoot(serviceConfig)
],
providers: [
AngularTokenService
AngularTokenService,
{
provide: Router,
useValue: jasmine.createSpyObj('Router', ['navigate'])
}
]
});

Expand Down Expand Up @@ -527,4 +532,54 @@ describe('AngularTokenService', () => {
req.flush( userData, { headers: tokenHeaders } );
});
});

describe('#canActivate', () => {
let router: Router;

beforeEach(() => {
initService({
signInRedirect: '/'
});

router = TestBed.get(Router);
});

describe('when the user is thought to be authenticated', () => {
beforeEach(() => {
(service as any).authData.next({ ...service.currentAuthData, ...{ expiry: Date.now() + 100_000_000 } });
});

it('returns true', () => {
expect(service.canActivate(null, null)).toBeTruthy();
});
});

describe('when the user is thought to be unauthenticated', () => {
it('returns false', () => {
expect(service.canActivate(null, { url: '/some/route' } as any)).toBeFalsy();
});

it('redirects to the configured route', () => {
service.canActivate(null, { url: '/some/route' } as any);

expect(router.navigate).toHaveBeenCalledWith([service.tokenOptions.signInRedirect]);
});

it('adds the desired route to local storage when configured', () => {
const url = '/some/route';
service.tokenOptions = { ...service.tokenOptions, ...{ signInStoredUrlStorageKey: 'redirectTo' }};
service.canActivate(null, { url } as any);

expect(localStorage.setItem).toHaveBeenCalledWith(service.tokenOptions.signInStoredUrlStorageKey, url);
});

it('does not add the desired route to local storage when not configured', () => {
const url = '/some/route';
service.tokenOptions = { ...service.tokenOptions, ...{ signInStoredUrlStorageKey: null }};
service.canActivate(null, { url } as any);

expect(localStorage.setItem).not.toHaveBeenCalled();
});
});
});
});
30 changes: 10 additions & 20 deletions projects/angular-token/src/lib/angular-token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,32 +138,22 @@ export class AngularTokenService implements CanActivate {
}

userSignedIn(): boolean {
if (this.authData.value == null) {
return false;
} else {
return true;
}
return !!this.authData.value;
}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.userSignedIn()) {
return true;
} else {
// Store current location in storage (usefull for redirection after signing in)
if (this.options.signInStoredUrlStorageKey) {
this.localStorage.setItem(
this.options.signInStoredUrlStorageKey,
state.url
);
}
canActivate(_route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
const expiration = this.userSignedIn() && !!this.authData.value.expiry ? (+this.authData.value.expiry * 1000) : 0;
const authenticated = Date.now() < expiration;

// Redirect user to sign in if signInRedirect is set
if (this.router && this.options.signInRedirect) {
this.router.navigate([this.options.signInRedirect]);
if (!authenticated) {
if (!!this.options.signInStoredUrlStorageKey) {
localStorage.setItem(this.options.signInStoredUrlStorageKey, state.url);
}

return false;
this.router.navigate([this.options.signInRedirect]);
}

return authenticated;
}


Expand Down