Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
feat(project): add AuthenticationStep and deprecate AuthorizeStep
Browse files Browse the repository at this point in the history
DEPRECATION: AuthorizeStep replaced by AuthenticationStep

AuthenticationStep uses {settings:authentication:true}} of a route's configuration
  • Loading branch information
doktordirk committed May 31, 2016
1 parent ca89656 commit baeb35c
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/aurelia-authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Config, Rest} from 'aurelia-api';

import {AuthService} from './authService';
import {AuthorizeStep} from './authorizeStep';
import {AuthenticateStep} from './authenticateStep';
import {BaseConfig} from './baseConfig';
import {FetchConfig} from './fetchClientConfig';
import './authFilter';
Expand Down Expand Up @@ -67,5 +68,6 @@ export {
configure,
FetchConfig,
AuthService,
AuthorizeStep
AuthorizeStep,
AuthenticateStep
};
25 changes: 25 additions & 0 deletions src/authenticateStep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {inject} from 'aurelia-dependency-injection';
import {Authentication} from './authentication';
import {Redirect} from 'aurelia-router';

@inject(Authentication)
export class AuthenticateStep {
constructor(authentication) {
this.authentication = authentication;
}

run(routingContext, next) {
const isLoggedIn = this.authentication.isAuthenticated();
const loginRoute = this.authentication.config.loginRoute;

if (routingContext.getAllInstructions().some(i => i.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 ));
}

return next();
}
}
3 changes: 3 additions & 0 deletions src/authorizeStep.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {inject} from 'aurelia-dependency-injection';
import {Authentication} from './authentication';
import {Redirect} from 'aurelia-router';
import * as LogManager from 'aurelia-logging';

@inject(Authentication)
export class AuthorizeStep {
constructor(authentication) {
LogManager.getLogger('authentication').warn('AuthorizeStep is deprecated. Use AuthenticationStep instead and add {settings: {authenticate: true}} to your router configuration.');

this.authentication = authentication;
}

Expand Down
7 changes: 6 additions & 1 deletion test/aurelia-authentication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
configure,
FetchConfig,
AuthService,
AuthorizeStep
AuthorizeStep,
AuthenticateStep
} from '../src/aurelia-authentication';
import {BaseConfig} from '../src/baseConfig';

Expand Down Expand Up @@ -41,6 +42,10 @@ describe('aurelia-authentication', function() {
it('Should export AuthorizeStep', function() {
expect(AuthorizeStep).toBeDefined();
});

it('Should export AuthenticateStep', function() {
expect(AuthenticateStep).toBeDefined();
});
});

describe('configure()', function() {
Expand Down
6 changes: 3 additions & 3 deletions test/authService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ describe('AuthService', () => {
describe('.updateMe() with PUT', () => {
const container = getContainer();
const authService = container.get(AuthService);

beforeEach(() => {
authService.config.profileMethod = 'put';
});

it('without criteria', done => {
authService.updateMe({data: 'some'})
.then(result => {
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('AuthService', () => {
describe('.updateMe() with PATCH', () => {
const container = getContainer();
const authService = container.get(AuthService);

beforeEach(() => {
authService.config.profileMethod = 'patch';
});
Expand Down
133 changes: 133 additions & 0 deletions test/authenticateStep.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import {Container} from 'aurelia-dependency-injection';

import {AuthenticateStep} from '../src/authenticateStep';


const routes = {
onLoginRoute : [
{name: 'parent', fragment: '/login', config: {settings: {}}},
{name: 'child', fragment: 'childUrl', config: {settings: {}}}
],
authenticateNone : [
{name: 'parent', fragment: 'parentUrl', config: {settings: {}}},
{name: 'child', fragment: 'childUrl', config: {settings: {}}}
],
authenticateChild : [
{name: 'parent', fragment: 'parentUrl', config: {settings: {}}},
{name: 'child', fragment: 'childUrl', config: {settings: {authenticate: true}}}
],
authenticateParent : [
{name: 'parent', fragment: 'parentUrl', config: {settings: {authenticate: true}}},
{name: 'child', fragment: 'childUrl', config: {settings: {}}}
]};

describe('AuthenticateStep', () => {
describe('.run()', () => {
const authenticateStep = new Container().get(AuthenticateStep);
function next() {return;}
let loginRoute = authenticateStep.authentication.config.loginRoute;

it('should not redirect when not authenticated and no route requires it', () => {
let routingContext = {
getAllInstructions: () => routes.authenticateNone
};

next.cancel = redirect => {throw new Error();};

spyOn(next, 'cancel');

authenticateStep.run(routingContext, next);

expect(next.cancel).not.toHaveBeenCalled();
});

it('should redirect to login when not authenticated and child route requires it', done => {
let routingContext = {
getAllInstructions: () => routes.authenticateChild
};

next.cancel = redirect => {
expect(redirect.url).toBe(loginRoute);
done();
};

authenticateStep.run(routingContext, next);
});

it('should redirect to login when not authenticated and parent route requires it', done => {
let routingContext = {
getAllInstructions: () => routes.authenticateParent
};

next.cancel = redirect => {
expect(redirect.url).toBe(loginRoute);
done();
};

authenticateStep.run(routingContext, next);
});

it('should not redirect to login when authenticated and no route requires it', () => {
let routingContext = {
getAllInstructions: () => routes.authenticateNone
};

next.cancel = redirect => {throw new Error();};

spyOn(next, 'cancel');

authenticateStep.authentication.isAuthenticated = () => true;

authenticateStep.run(routingContext, next);

expect(next.cancel).not.toHaveBeenCalled();
});

it('should not redirect when authenticated and child route requires it', () => {
let routingContext = {
getAllInstructions: () => routes.authenticateChild
};

next.cancel = redirect => {throw new Error();};

spyOn(next, 'cancel');

authenticateStep.authentication.isAuthenticated = () => true;

authenticateStep.run(routingContext, next);

expect(next.cancel).not.toHaveBeenCalled();
});

it('should not redirect when not authenticated and parent route requires it', () => {
let routingContext = {
getAllInstructions: () => routes.authenticateParent
};

next.cancel = redirect => {throw new Error();};

spyOn(next, 'cancel');

authenticateStep.authentication.isAuthenticated = () => true;

authenticateStep.run(routingContext, next);

expect(next.cancel).not.toHaveBeenCalled();
});

it('should redirect when authenticated and parent route is login route', done => {
let routingContext = {
getAllInstructions: () => routes.onLoginRoute
};

next.cancel = redirect => {
expect(redirect.url).toBe(authenticateStep.authentication.config.loginRedirect);
done();
};

authenticateStep.authentication.isAuthenticated = () => true;

authenticateStep.run(routingContext, next);
});
});
});
1 change: 0 additions & 1 deletion test/setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {initialize} from 'aurelia-pal-browser';
import 'aurelia-polyfills';
import 'fetch';

initialize();

0 comments on commit baeb35c

Please sign in to comment.