Permalink
Browse files

feat(authenticateStep): use authService.authenticate

  • Loading branch information...
doktordirk committed Jun 2, 2016
1 parent 7e64bbf commit 5b9306fee4bdf15031f8e98e7cc27406910321ab
Showing with 23 additions and 20 deletions.
  1. +10 −9 src/authenticateStep.js
  2. +13 −11 test/authenticateStep.spec.js
@@ -1,23 +1,24 @@
import {inject} from 'aurelia-dependency-injection';
import {Authentication} from './authentication';
import {Redirect} from 'aurelia-router';
@inject(Authentication)
import {AuthService} from './authService';
@inject(AuthService)
export class AuthenticateStep {
constructor(authentication) {
this.authentication = authentication;
constructor(authService) {
this.authService = authService;
}
run(routingContext, next) {
const isLoggedIn = this.authentication.isAuthenticated();
const loginRoute = this.authentication.config.loginRoute;
const isLoggedIn = this.authService.authenticated;

This comment has been minimized.

@siimv

siimv Dec 29, 2016

I wonder why this change? I believe it causes a bug when using sessionStorage.

When I am using sessionStorage (and auth token is in the store) and I refresh the page, then this.authService.authenticated is false but this.authService.isAuthenticated() is true.

This comment has been minimized.

@RWOverdijk

RWOverdijk Jan 3, 2017

Member

I think this is an oversight. I'm sure there's a reason for it, so we'll have to check.

const loginRoute = this.authService.config.loginRoute;
if (routingContext.getAllInstructions().some(i => i.config.settings.authenticate === true)) {
if (routingContext.getAllInstructions().some(route => route.config.settings.authenticate === true)) {
if (!isLoggedIn) {
return next.cancel(new Redirect(loginRoute));
}
} else if (isLoggedIn && routingContext.getAllInstructions().some(i => i.fragment === loginRoute)) {
return next.cancel(new Redirect( this.authentication.config.loginRedirect ));
} else if (isLoggedIn && routingContext.getAllInstructions().some(route => route.fragment === loginRoute)) {
return next.cancel(new Redirect( this.authService.config.loginRedirect ));
}
return next();
@@ -24,16 +24,15 @@ const routes = {
describe('AuthenticateStep', () => {
describe('.run()', () => {
const authenticateStep = new Container().get(AuthenticateStep);
function next() {return;}
let loginRoute = authenticateStep.authentication.config.loginRoute;
let loginRoute = authenticateStep.authService.config.loginRoute;
it('should not redirect when not authenticated and no route requires it', () => {
let routingContext = {
getAllInstructions: () => routes.authenticateNone
};
function next() {return;}
next.cancel = redirect => {throw new Error();};
spyOn(next, 'cancel');
authenticateStep.run(routingContext, next);
@@ -46,6 +45,7 @@ describe('AuthenticateStep', () => {
getAllInstructions: () => routes.authenticateChild
};
function next() {return;}
next.cancel = redirect => {
expect(redirect.url).toBe(loginRoute);
done();
@@ -59,6 +59,7 @@ describe('AuthenticateStep', () => {
getAllInstructions: () => routes.authenticateParent
};
function next() {return;}
next.cancel = redirect => {
expect(redirect.url).toBe(loginRoute);
done();
@@ -72,11 +73,11 @@ describe('AuthenticateStep', () => {
getAllInstructions: () => routes.authenticateNone
};
function next() {return;}
next.cancel = redirect => {throw new Error();};
spyOn(next, 'cancel');
authenticateStep.authentication.isAuthenticated = () => true;
authenticateStep.authService.authenticated = true;
authenticateStep.run(routingContext, next);
@@ -88,11 +89,11 @@ describe('AuthenticateStep', () => {
getAllInstructions: () => routes.authenticateChild
};
function next() {return;}
next.cancel = redirect => {throw new Error();};
spyOn(next, 'cancel');
authenticateStep.authentication.isAuthenticated = () => true;
authenticateStep.authService.authenticated = true;
authenticateStep.run(routingContext, next);
@@ -104,11 +105,11 @@ describe('AuthenticateStep', () => {
getAllInstructions: () => routes.authenticateParent
};
function next() {return;}
next.cancel = redirect => {throw new Error();};
spyOn(next, 'cancel');
authenticateStep.authentication.isAuthenticated = () => true;
authenticateStep.authService.authenticated = true;
authenticateStep.run(routingContext, next);
@@ -120,12 +121,13 @@ describe('AuthenticateStep', () => {
getAllInstructions: () => routes.onLoginRoute
};
function next() {return;}
next.cancel = redirect => {
expect(redirect.url).toBe(authenticateStep.authentication.config.loginRedirect);
expect(redirect.url).toBe(authenticateStep.authService.config.loginRedirect);
done();
};
authenticateStep.authentication.isAuthenticated = () => true;
authenticateStep.authService.authenticated = true;
authenticateStep.run(routingContext, next);
});

0 comments on commit 5b9306f

Please sign in to comment.