Skip to content

Commit

Permalink
Authenticator working
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanlabouve committed Apr 18, 2016
1 parent ecd2cbf commit 680376e
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 35 deletions.
48 changes: 47 additions & 1 deletion app/authenticators/knockjwt.js
@@ -1,13 +1,59 @@
import Ember from 'ember';
import Base from 'ember-simple-auth/authenticators/base';
import config from '../config/environment';

const { RSVP: { Promise }, $: { ajax }, run, get } = Ember;

export default Base.extend({
tokenEndpoint: `${config.host}/knock/auth_token`,

restore(data) {
return new Promise((resolve, reject) => {
if (!Ember.isEmpty(data.token)) {
resolve(data);
} else {
reject();
}
});
},

authenticate(/*args*/) {
authenticate(creds) {
const { identification, password } = creds;

const data = JSON.stringify({
auth: {
email: identification,
password
}
});

const requestOptions = {
url: this.tokenEndpoint,
type: 'POST',
data,
contentType: 'application/json',
dataType: 'json'
};

return new Promise((resolve, reject) => {
ajax(requestOptions).then((response) => {
const { jwt } = response;
// Wrapping aync operation in Ember.run
run(() => {
resolve({
token: jwt
});
});
}, (error) => {
// Wrapping aync operation in Ember.run
run(() => {
reject(error);
});
});
});
},

invalidate(data) {
return Promise.resolve();
}
});
12 changes: 10 additions & 2 deletions app/components/login-form/component.js
Expand Up @@ -4,10 +4,18 @@ export default Ember.Component.extend({
session: Ember.inject.service(),
actions: {
login() {
return true;
// reset login error on each attempt
this.set('loginError', false);
const { identification, password } = this.getProperties( 'identification', 'password');
const s = this.get('session');
s.authenticate('authenticator:knockjwt', { identification, password }).catch((error) => {
// If login fails, just set an error
this.set('loginError', true);
});

},
signout() {
return true;
this.get('session').invalidate();
}
}
});
3 changes: 0 additions & 3 deletions app/mirage/config.js

This file was deleted.

20 changes: 0 additions & 20 deletions app/mirage/factories/contact.js

This file was deleted.

7 changes: 0 additions & 7 deletions app/mirage/scenarios/default.js

This file was deleted.

3 changes: 2 additions & 1 deletion mirage/config.js
@@ -1,6 +1,7 @@
import Mirage from 'ember-cli-mirage';
export default function() {
this.get('/public-posts');
this.post('/knock/auth', (db, request) => {
this.post('/knock/auth_token', (db, request) => {
const req = JSON.parse(request.requestBody);
const pw = Ember.get(req, 'auth.password');

Expand Down
18 changes: 17 additions & 1 deletion tests/acceptance/login-form-test.js
Expand Up @@ -59,9 +59,25 @@ test('user can logout', function(assert) {
'After clicking logout, the user is no longer logged in'
);

const loginFormPresent = find('#loginForm').length > 0 ? true : false;
assert.equal(
loginFormPresent,
true,
'after we click logout, we see the login form'
);
});
});

// test('full path', function(assert) {
// visit('/');
//
// fillIn('.username-field', 'lester@test.com');
// fillIn('.password-field', 'test1234');
// click('.login-btn');
//
//
// });

test('user can login', function(assert) {
invalidateSession(this.application);
visit('/');
Expand Down Expand Up @@ -115,7 +131,7 @@ test('If a user puts in the wrong login credentials, they see a login error', fu
const loginFormPresent = find('#loginForm').length > 0 ? true : false;
assert.equal(
loginFormPresent,
false,
true,
'and we can still see the login form'
);
});
Expand Down

0 comments on commit 680376e

Please sign in to comment.