ember.js - ember-simple-auth: Persisting session in localstorage using custom authenticator -
setup:
- ember : 2.0.2
- ember data : 2.0.1
- jquery : 1.11.3
- ember simple auth : 1.0.0 (jjabrams branch)
- ember cli : 1.13.8
i'm using pretender mock server.
usecase:
using custom authenticator interface server.
have 2 routes: login, protected (and default index,application)
when login right credentials, authenticate method of authenticator gets called , logs response object passed resolve().
observations:
after logging in , being directed protected page, refreshing protected route (which has
authenticatedroutemixin
) leads login page.localstorage has no values bound after successful login. before login:
ember_simple_auth:session -> {"authenticated":{}}
restore()
method of authenticator never called.going route protected route after auth , coming goest login page again.
//authenticators/custom.js import ember 'ember'; import base 'ember-simple-auth/authenticators/base'; export default base.extend({ restore: function (data) { return new ember.rsvp.promise(function (resolve, reject) { console.log("resolve",data); if (!ember.isempty(data.token)) { //todo remove log resolve(data); } else { console.log("rejecting",data); reject(); } }); }, authenticate(credentials) { return new ember.rsvp.promise((resolve, reject) => ember.$.ajax({ url: '/token', type: 'post', data: json.stringify({ email: credentials.identification, password: credentials.password }), contenttype: 'application/json;charset=utf-8', datatype: 'json' }).then(function (response) { ember.run(function () { //this logs expected information console.log("response", response, response.token, response.user); resolve(response); }); }, function (xhr, status, error) { console.log("error", error, xhr.responsetext); var response = xhr.responsetext; ember.run(function () { reject(response); }); })); }, invalidate(token) { return api.logout(token); } });
//environment.js env['ember-simple-auth'] = { store: 'session-store:local-storage', routeafterauthentication: '/protected' };
tldr; how make session persist?
i got working finally. ember 2.0 , esa 1.0
here steps took:
- create new ember cli project
- update ember , ember data values ^2.0.0 in bower.json source
- add esa jjabrams dep package.json source
- run npm install && bower install
gotchas: (this original problem caused problems described in question)
- if you're upgrading older versions of esa, references 'simple-auth/..' should updated refer 'ember-simple-auth/..' instead.. .. include imports authenticators, authorizers, stores, mixins , config key in config/environment.js file.
all shouldn't issue once esa 1.0 , ember cli ember 2.0 comes out :)
Comments
Post a Comment