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

Commit

Permalink
feat(value-converters): move/rename/add valueConverters
Browse files Browse the repository at this point in the history
isAuth(isAuthenicated):  isAuthenicated is optional. defaults to authService.authenticated
authenticated(): valueConverted added. returns authService.authenticated
made configurable which value converters should be global
  • Loading branch information
doktordirk committed Jun 3, 2016
1 parent caf4a3b commit 4d2ee93
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 12 deletions.
4 changes: 2 additions & 2 deletions build/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ module.exports = {
source: appRoot + '**/*.js',
tsSource: [
appRoot + '**/*.js', // list files to parse for d.ts
'!' + appRoot + entryFileName // exclude entry file
'!' + appRoot + 'value-converters/*js' // exclude entry file
],
resources: 'authFilter.js', // relative to root, resources can not that easily be bundled into a single file (due to naming conventions)
resources: 'value-converters/*js', // relative to root, resources can not that easily be bundled into a single file (due to naming conventions)
html: appRoot + '**/*.html',
style: 'styles/**/*.css',
output: 'dist/',
Expand Down
12 changes: 9 additions & 3 deletions src/aurelia-authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {AuthorizeStep} from './authorizeStep';
import {AuthenticateStep} from './authenticateStep';
import {BaseConfig} from './baseConfig';
import {FetchConfig} from './fetchClientConfig';
import './authFilter';
import * as LogManager from 'aurelia-logging';
// import to ensure value-converters get bundled
import './authFilterValueConverter';
import './authenticatedValueConverter';

/**
* Configure the plugin.
Expand All @@ -20,16 +23,19 @@ function configure(aurelia, config) {
PLATFORM.location.origin = PLATFORM.location.protocol + '//' + PLATFORM.location.hostname + (PLATFORM.location.port ? ':' + PLATFORM.location.port : '');
}

aurelia.globalResources('./authFilter');

const baseConfig = aurelia.container.get(BaseConfig);

if (typeof config === 'function') {
config(baseConfig);
} else if (typeof config === 'object') {
baseConfig.configure(config);
}

// after baseConfig was configured
for (let converter of baseConfig.globalValueConverters) {
aurelia.globalResources(`./${converter}`);
LogManager.getLogger('authentication').info(`Add globalResources value-converter: ${converter}`);
}
const fetchConfig = aurelia.container.get(FetchConfig);
const clientConfig = aurelia.container.get(Config);

Expand Down
5 changes: 0 additions & 5 deletions src/authFilter.js

This file was deleted.

19 changes: 19 additions & 0 deletions src/authFilterValueConverter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {inject} from 'aurelia-dependency-injection';
import {AuthService} from './authService';

@inject(AuthService)
export class AuthFilterValueConverter {
constructor(authService) {
this.authService = authService;
}

/**
* route toView predictator on route.config.auth === (parameter || authService.isAuthenticated())
* @param {RouteConfig} routes the routes array to convert
* @param {[Boolean]} [isAuthenticated] optional isAuthenticated value. default: this.authService.authenticated
* @return {Boolean} show/hide element
*/
toView(routes, isAuthenticated = this.authService.authenticated) {
return routes.filter(route => route.config.auth === isAuthenticated);
}
}
17 changes: 17 additions & 0 deletions src/authenticatedValueConverter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {inject} from 'aurelia-dependency-injection';
import {AuthService} from './authService';

@inject(AuthService)
export class AuthenticatedValueConverter {
constructor(authService) {
this.authService = authService;
}

/**
* element toView predictator on authService.isAuthenticated()
* @return {Boolean} show/hide element
*/
toView() {
return this.authService.authenticated;
}
}
5 changes: 4 additions & 1 deletion src/baseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ export class BaseConfig {
// The key used for storing the authentication response locally
storageKey = 'aurelia_authentication';

//OAuth provider specific related configuration
// List of value-converters to make global
globalValueConverters = ['authFilterValueConverter', 'authenticatedValueConverter'];

//OAuth provider specific related configuration
// ============================================
providers = {
facebook: {
Expand Down
8 changes: 7 additions & 1 deletion test/aurelia-authentication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,17 @@ describe('aurelia-authentication', function() {
it('Should call globalResources configuration to be passed as a function.', function() {
let container = new Container();

let globalResources = [];
configure({
container: container, globalResources: resource => {
expect(resource).toEqual('./authFilter');
globalResources.push(resource);
}
}, noop);

const expected = ['./authFilterValueConverter',
'./authenticatedValueConverter'];

expect(globalResources.toString()).toEqual(expected.toString());
});

it('Should allow configuration with a function.', function() {
Expand Down

0 comments on commit 4d2ee93

Please sign in to comment.