Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Commit

Permalink
huge refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
thejohnnybot committed Jun 2, 2016
1 parent 5954830 commit 43c17ec
Show file tree
Hide file tree
Showing 34 changed files with 208 additions and 380 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@
/libpeerconnection.log
npm-debug.log
testem.log
.DS_Store
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"predef": [
"server",
"document",
"window",
"-Promise"
Expand Down
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
language: node_js
node_js:
- "5.11.1"
- "0.12"

sudo: false

Expand All @@ -11,6 +11,7 @@ cache:

env:
- EMBER_TRY_SCENARIO=default
- EMBER_TRY_SCENARIO=ember-1.13
- EMBER_TRY_SCENARIO=ember-release
- EMBER_TRY_SCENARIO=ember-beta
- EMBER_TRY_SCENARIO=ember-canary
Expand All @@ -23,7 +24,7 @@ matrix:
before_install:
- export PATH=/usr/local/phantomjs-2.0.0/bin:$PATH
- "npm config set spin false"
- "npm install -g npm@^3"
- "npm install -g npm@^2"

install:
- npm install -g bower
Expand Down
74 changes: 14 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,72 +1,26 @@
# ember-simple-token
# Simple-token

This README outlines the details of collaborating on this Ember addon.

## Installation

`ember install ember-simple-token`
* `git clone` this repository
* `npm install`
* `bower install`

## Running

### Code to put in your component
* `ember server`
* Visit your app at http://localhost:4200.

```javascript
import Ember from 'ember';
## Running Tests

const { service } = Ember.inject;
* `npm test` (Runs `ember try:testall` to test your addon against multiple Ember versions)
* `ember test`
* `ember test --server`

export default Ember.Component.extend({
session: service(),
## Building

actions: {
authenticate() {
let credentials = this.getProperties('identification', 'password');
this.get('session').authenticate('authenticator:token', credentials).catch((reason) => {
this.set('errorMessage', reason.error);
});
}
}
});
```
* `ember build`

### Code for the template

```html
<form {{action 'authenticate' on='submit'}}>
<div class="form-group">
<label for="identification">Login</label>
{{input value=identification placeholder='Enter Login' class='form-control'}}
</div>
<div class="form-group">
<label for="password">Password</label>
{{input value=password placeholder='Enter Password' class='form-control' type='password'}}
</div>
<button type="submit" class="btn btn-default">Login</button>
</form>
```

### Ember-Data usage

```javascript
import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';

export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
authorizer: 'authorizer:token'
});
```
### Change Configuration

```javascript
ENV['ember-simple-token'] = {
serverTokenEndpoint: "/another-token",
identificationAttributeName: "email"
};
```

### Generators
`ember g login-scaffold <name>`

Will generate all the code seen above in pod structure in a component

### TODO:
- [] Test
For more information on using ember-cli, visit [http://ember-cli.com/](http://ember-cli.com/).
51 changes: 17 additions & 34 deletions addon/authenticators/token.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,35 @@
import Ember from 'ember';
import BaseAuthenticator from 'ember-simple-auth/authenticators/base';
import Base from 'ember-simple-auth/authenticators/base';
import Configuration from '../configuration';

const { get, isEmpty, inject: { service }, RSVP } = Ember;
const { resolve, reject } = RSVP;
const { get, isEmpty, inject: { service }, RSVP: { resolve, reject } } = Ember;

export default BaseAuthenticator.extend({
export default Base.extend({
ajax: service(),

serverTokenEndpoint: '/token',
serverTokenEndpoint: Configuration.serverTokenEndpoint,

tokenAttributeName: 'token',
tokenAttributeName: Configuration.tokenAttributeName,

identificationAttributeName: 'email',
identificationAttributeName: Configuration.identificationAttributeName,

init() {
this._super(...arguments);
const config = Ember.getOwner(this).resolveRegistration('config:environment')['ember-simple-token'];
if (config !== undefined) {
this.serverTokenEndpoint = config.serverTokenEndpoint;
this.tokenAttributeName = config.tokenAttributeName;
this.identificationAttributeName = config.identificationAttributeName;
restore(data) {
const token = get(data, this.tokenAttributeName);
if (!isEmpty(token)) {
return resolve(data);
} else {
return reject();
}
},

authenticate(data) {
return get(this, 'ajax').post(this.serverTokenEndpoint, {
data: JSON.stringify(data)
}).then((response) => {
return response.json().then((json) => {
if (response.status >= 200 && response.status < 300) {
return resolve(json);
} else {
return reject(json);
}
});
});
},

restore(data) {
const token = get(data, this.tokenAttributeName);
if (isEmpty(token)) {
return resolve(response);
}).catch((error) => {
Ember.Logger.warn(error);
return reject();
} else {
return resolve(data);
}
},

invalidate() {
return resolve();
});
}
});
15 changes: 4 additions & 11 deletions addon/authorizers/token.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import Ember from 'ember';
import BaseAuthorizer from 'ember-simple-auth/authorizers/base';
import Base from 'ember-simple-auth/authorizers/base';
import Configuration from '../configuration';

const { get, isEmpty } = Ember;

export default BaseAuthorizer.extend({
tokenAttributeName: 'token',

init() {
this._super(...arguments);
const config = Ember.getOwner(this).resolveRegistration('config:environment')['ember-simple-token'];
if (config !== undefined) {
this.tokenAttributeName = config.tokenAttributeName;
}
},
export default Base.extend({
tokenAttributeName: Configuration.tokenAttributeName,

authorize(data, block) {
const token = get(data, this.tokenAttributeName);
Expand Down
25 changes: 25 additions & 0 deletions addon/configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Ember from 'ember';

const { getWithDefault } = Ember;

const DEFAULTS = {
serverTokenEndpoint: '/token',
tokenAttributeName: 'token',
identificationAttributeName: 'email'
};

export default {
serverTokenEndpoint: DEFAULTS.serverTokenEndpoint,

tokenAttributeName: DEFAULTS.tokenAttributeName,

identificationAttributeName: DEFAULTS.identificationAttributeName,

load(config) {
for (let property in this) {
if (this.hasOwnProperty(property) && Ember.typeOf(property) !== 'function') {
this[property] = getWithDefault(config, property, DEFAULTS[property]);
}
}
}
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import TokenAuthenticator from '../authenticators/token';
import TokenAuthorizer from '../authorizers/token';

export function initialize(application) {
export default function setupRegistry(application) {
application.register('authenticator:token', TokenAuthenticator);
application.register('authorizer:token', TokenAuthorizer);
}

export default {
name: 'simple-token',
initialize
};
1 change: 1 addition & 0 deletions app/authenticators/token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'simple-token/authenticators/token';
1 change: 1 addition & 0 deletions app/authorizers/token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'simple-token/authorizers/token';
13 changes: 12 additions & 1 deletion app/initializers/simple-token.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
export { default, initialize } from 'ember-simple-token/initializers/simple-token';
import ENV from '../config/environment';
import setupRegistry from 'simple-token/initializers/setup-registry';
import Configuration from 'simple-token/configuration';

export default {
name: 'simple-token',
initialize(registry) {
const config = ENV['simple-auth'] || {};
Configuration.load(config);
setupRegistry(registry);
}
}
9 changes: 0 additions & 9 deletions blueprints/ember-simple-token/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import Ember from 'ember';

const { service } = Ember.inject;
const { get, getProperties, inject: { service }, set } = Ember;

export default Ember.Component.extend({
session: service(),

actions: {
authenticate() {
let credentials = this.getProperties('email', 'password');
this.get('session').authenticate('authenticator:token', credentials).catch((reason) => {
this.set('errorMessage', reason.error);
let credentials = getProperties(this, 'email', 'password');
get(this, 'session').authenticate('authenticator:token', credentials).catch((reason) => {
set(this, 'errorMessage', reason.error);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion blueprints/login-scaffold/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ module.exports = {
}
};
}
};
};;
17 changes: 17 additions & 0 deletions blueprints/simple-token/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*jshint node:true*/
module.exports = {
description: '',

// locals: function(options) {
// // Return custom template variables here.
// return {
// foo: options.entity.options.foo
// };
// }

afterInstall() {
return this.addPackagesToProject([
{ name: 'ember-simple-auth', target: '1.1.0' }
]);
}
};
6 changes: 4 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "ember-simple-token",
"name": "simple-token",
"dependencies": {
"ember": "~2.5.0",
"ember-cli-shims": "0.1.1",
"ember-cli-test-loader": "0.2.2",
"ember-qunit-notifications": "0.1.0"
"ember-qunit-notifications": "0.1.0",
"pretender": "~1.1.0",
"Faker": "~3.1.0"
}
}
6 changes: 1 addition & 5 deletions config/environment.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/*jshint node:true*/
'use strict';

module.exports = function(environment, appConfig) {
appConfig['ember-simple-token'] = {
serverTokenEndpoint: 'token',
identificationAttributeName: 'email'
};
module.exports = function(/* environment, appConfig */) {
return { };
};
8 changes: 1 addition & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,5 @@
'use strict';

module.exports = {
name: 'ember-simple-token',

included: function(app) {
this._super.included(app);

app.import('vendor/ember-simple-token/register-version.js');
}
name: 'simple-token'
};
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ember-simple-token",
"name": "simple-token",
"version": "0.0.0",
"description": "The default blueprint for ember-cli addons.",
"description": "A simple token auth implementation for ember-simple-auth",
"directories": {
"doc": "doc",
"test": "tests"
Expand All @@ -13,7 +13,7 @@
},
"repository": "",
"engines": {
"node": ">= 5.11.1"
"node": ">= 0.10.0"
},
"author": "",
"license": "MIT",
Expand All @@ -27,6 +27,7 @@
"ember-cli-htmlbars-inline-precompile": "^0.3.1",
"ember-cli-inject-live-reload": "^1.4.0",
"ember-cli-jshint": "^1.0.0",
"ember-cli-mirage": "0.2.0",
"ember-cli-qunit": "^1.4.0",
"ember-cli-release": "0.2.8",
"ember-cli-sri": "^2.1.0",
Expand Down
Loading

0 comments on commit 43c17ec

Please sign in to comment.