Comments (8)
Another simple solution inspired by @thermokarst's session-account
service, is to have an observer in the in the application's controller:
// app/pods/application/controller.js
import Ember from 'ember';
export default Ember.Controller.extend({
session: Ember.inject.service('session'),
setSessionClaims: Ember.observer('session.data.authenticated.token', function() {
if(this.get('session.data.authenticated.token')) {
try {
let claims = JSON.parse(atob(this.get('session.data.authenticated.token').split('.')[1]));
} catch (error) {
return alert('Failed to JSON.parse(claims)');
}
this.set('session.data.authenticated.claims', claims);
}
}).on('init'),
});
or instead of the long session.data.authenticated.claims
you could just set session.currentUser
(thanks @thermokarst)
from ember-simple-auth-token.
I have to deal with the same issue today. This is how I resolved it.
First, I modify my backend server (django) to return the user id with the token. So, with a succesfull authentication it returns something like this:
{
"token": "eyJhbGciOiJIUzI1NiIsIskdj........",
"empleado_id": 1234
}
Then, I created a custom session. When it receives the token it makes a secord request for the user data. This is the code:
/* app/initializers/crystal-session.js */
import Em from "ember";
import DS from "ember-data";
import Session from "simple-auth/session";
var CrystalSession = Session.extend({
user: function() {
var accountId = this.get('empleado_id');
if (!Em.isEmpty(accountId)) {
return DS.PromiseObject.create({
promise: this.container.lookup('store:main').find('empleado', accountId)
});
}
return null;
}.property('empleado_id')
});
export default {
name: 'crystal-session',
before: "simple-auth",
initialize: function (container, application) {
application.register('session:crystal', CrystalSession);
}
};
Please notice that my user is called empleado
Finally, let simple-auth know about this session. Edit your config/environment.js
file with:
ENV['simple-auth'] = {
session: 'session:crystal',
/* other stuff here */
}
from ember-simple-auth-token.
Hello Injaon, Can you please show your controller code, particular the authenticate action that's triggered when submitting the username/password. I followed your example, but the user function isn't being triggered, so I'm wondering if you're using a regular ajax call or this.get('session').authenticate(authenticator, credentials) method in this package.
from ember-simple-auth-token.
@aibarra11: I had implemented something based on @injaon's response, however I just realized that for what I am trying to do, this was probably overkill. I just reimplemented to something along these lines (see below). I completely removed the custom session initializer, and did the following in my login controller:
// app/pods/login/controller.js
import Ember from 'ember';
import parseBase64 from '../../utils/parse-base64';
export default Ember.Controller.extend({
actions: {
authenticate: function() {
let credentials = this.getProperties('identification', 'password');
let session = this.get('session');
let authenticator = 'simple-auth-authenticator:token';
session.authenticate(authenticator, credentials).then(() => {
// parseBase64 just converts the base64 encoded string to a javascript object
let t = parseBase64(session.get('secure.token'));
// 'sub' is the user's id, as per the JWT draft specification
this.store.find('user', t['sub']).then((user) => {
// we can store the current user in the ember-simple-auth session
session.set('currentUser', user);
});
}, (error)=> {
// handle errors appropriately
});
}
}
});
In my template I have access to the following:
And my session in local storage looks like this:
{
"secure": {
"authenticator": "simple-auth-authenticator:token",
"token": "TOKEN_DATA"
},
"currentUser": {
"email": "testA",
"name": "Test Admin User",
"role": "A",
"createdAt": "1901-01-01T00:00:00.000Z",
"updatedAt": "1901-01-01T00:00:00.000Z"
}
}
Not sure if that adheres to the usual best-practices or not, but it seems to work just fine for me. Any comments or suggestions are certainly welcome.
EDITED 6:40p AKDT: fixed double declaration of variable user
from ember-simple-auth-token.
@thermokarst that works for me! hahaha, yeah.. I'm pretty sure i'm not following a bunch of best practices too, but at the rate the ember core changes and the rate i'm able to learn this, i'm happy no matter how i get it working =)
from ember-simple-auth-token.
@thermokarst I'm having trouble importing parseBase64 (cannot find import), nor can I find any additional info about it. Is it default Ember or an additional library?
from ember-simple-auth-token.
@Baukereg: parseBase64
is a user-defined function (the import statement at the top: import parseBase64 from '../../utils/parse-base64';
, utils is for user-defined functions). Sorry, I guess I didn't make it clear in my comments in the code snippet above, but parseBase64
is a function that I dropped into utils. I pulled that from ember-cli-simple-auth-token, you can find it here: https://github.com/thermokarst/hymenobacterdotinfo/blob/master/app/utils/parse-base64.js.
It's worth noting, when ember-simple-auth 1.0 came out I dropped ember-cli-simple-auth-token, and moved to a session-account service setup (https://github.com/thermokarst/hymenobacterdotinfo/blob/master/app/services/session-account.js).
from ember-simple-auth-token.
@aalimovs: thanks for posting this, I really like the idea of using an observer on the application's controller. Seems like for simple use cases this is simpler than setting up a service. Thanks!
from ember-simple-auth-token.
Related Issues (20)
- Demo app not working due to CORS issue HOT 2
- Some browser cannot write local Storage
- Possible to get a beta release that is is sync with ember-simple-auth 3.1.0-beta.0? HOT 71
- Travis CI HOT 1
- CI Integration HOT 1
- Release a version compatible with ESA v3.1.0 HOT 5
- Request to update dependency on ember-get-config to 0.3.0 HOT 3
- Refresh token after computer sleep HOT 10
- Fastboot best practice HOT 1
- Addon incompatible with latest version of ember-simple-auth HOT 3
- Transferring Ownership HOT 8
- addon/authenticators/jwt restore not called when page is refreshed HOT 1
- Dependency on old version of ember-cli-babel HOT 1
- Support for ESA v5 and v6 HOT 1
- refreshAccessToken not called on built project on Safari
- Thoughts on dropping support for very old ember and simple auth? HOT 1
- Updating the addon HOT 7
- Ember 5 support HOT 1
- server crash when using JWT with fastboot HOT 1
- Help Understanding RefreshLeeway
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ember-simple-auth-token.