diff --git a/app/authenticators/knockjwt.js b/app/authenticators/knockjwt.js index 4a54b5a..003ec99 100644 --- a/app/authenticators/knockjwt.js +++ b/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(); } }); diff --git a/app/components/login-form/component.js b/app/components/login-form/component.js index 3e47c3f..06ba9a2 100644 --- a/app/components/login-form/component.js +++ b/app/components/login-form/component.js @@ -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(); } } }); diff --git a/app/mirage/config.js b/app/mirage/config.js deleted file mode 100644 index 70ed185..0000000 --- a/app/mirage/config.js +++ /dev/null @@ -1,3 +0,0 @@ -export default function() { - this.get('/public-posts'); -} diff --git a/app/mirage/factories/contact.js b/app/mirage/factories/contact.js deleted file mode 100644 index 1b3a3ea..0000000 --- a/app/mirage/factories/contact.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - This is an example factory definition. - - Create more files in this directory to define additional factories. -*/ -import Mirage/*, {faker} */ from 'ember-cli-mirage'; - -export default Mirage.Factory.extend({ - // name: 'Pete', // strings - // age: 20, // numbers - // tall: true, // booleans - - // email: function(i) { // and functions - // return 'person' + i + '@test.com'; - // }, - - // firstName: faker.name.firstName, // using faker - // lastName: faker.name.firstName, - // zipCode: faker.address.zipCode -}); diff --git a/app/mirage/scenarios/default.js b/app/mirage/scenarios/default.js deleted file mode 100644 index e07271c..0000000 --- a/app/mirage/scenarios/default.js +++ /dev/null @@ -1,7 +0,0 @@ -export default function(/* server */) { - - // Seed your development database using your factories. This - // data will not be loaded in your tests. - - // server.createList('contact', 10); -} diff --git a/mirage/config.js b/mirage/config.js index 1e24000..83e5c6a 100644 --- a/mirage/config.js +++ b/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'); diff --git a/tests/acceptance/login-form-test.js b/tests/acceptance/login-form-test.js index b3f8353..178a640 100644 --- a/tests/acceptance/login-form-test.js +++ b/tests/acceptance/login-form-test.js @@ -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('/'); @@ -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' ); });