Start implementing backends and authentication

This commit is contained in:
Mathias Biilmann Christensen
2016-02-25 12:31:21 -08:00
parent c60d8ba706
commit 67cdd92bfb
13 changed files with 247 additions and 15 deletions

49
src/backends/backend.js Normal file
View File

@ -0,0 +1,49 @@
import TestRepoBackend from './test-repo/Implementation';
export function resolveBackend(config) {
const name = config.getIn(['backend', 'name']);
if (name == null) {
throw 'No backend defined in configuration';
}
switch (name) {
case 'test-repo':
return new Backend(new TestRepoBackend(config));
default:
throw `Backend not found: ${name}`;
}
}
class Backend {
constructor(implementation, authStore = null) {
this.implementation = implementation;
this.authStore = authStore;
if (this.implementation == null) {
throw 'Cannot instantiate a Backend with no implementation';
}
}
currentUser() {
if (this.user) { return this.user; }
return this.authStore && this.authStore.retrieve();
}
authComponent() {
return this.implementation.authComponent();
}
authenticate(state) {
return this.implementation.authenticate(state);
}
}
export const currentBackend = (function() {
let backend = null;
return (config) => {
if (backend) { return backend; }
if (config.get('backend')) {
return backend = resolveBackend(config);
}
};
})();