Thanks!
To your question, by user
, I’m referring to both the Ember model and Rails model that is used for authentication and authorization.
When I log in with the credentials for a user (e.g., email and password), I expect the Rails backend to authenticate the credentials and return a JWT token, and then for Ember/ESA/ESAT to use that token for future requests. In addition, I was expecting ESA/ESAT to automatically load the user
with whom’s credentials I used to be loaded into the Ember Data Store.
As you mentioned, loading the currently logged in user into the Ember Data Store is not an automatic thing but rather “roll your own.” Fortunately, ESA documentation provided an example of how to do this here: Managing a Current User.
This example loads and manages the current user using an Ember service. It also provides two different ways for loading the current user’s data:
- via a dedicated API endpoint; and
- using the user’s ID and using
findRecord
.
I opted to use the first approach: a dedicated API endpoint. However, it still doesn’t seem to work.
For reference, here’s what I did.
Created a /users/me
endpoint in my Rails app
# routes.rb
...
resources :users do
get "me", on: :collection
...
# controllers/users_controller.rb
class User < ApplicationRecord
...
def me
render json: current_user #current_user is provided by Knock
end
...
end
Created Ember service as per ESA guide
// app/services/current-user.js
import Ember from 'ember';
const { inject: { service }, RSVP } = Ember;
export default Ember.Service.extend({
session: service('session'),
store: service(),
load() {
if (this.get('session.isAuthenticated')) {
return this.get('store').queryRecord('user', { me: true }).then((user) => {
this.set('user', user);
});
} else {
return RSVP.resolve();
}
}
});
Created user adapter as ESA guide
// app/adapters/user.js
import ApplicationAdapter from './application';
export default ApplicationAdapter.extend({
urlForQueryRecord(query) {
if (query.me) {
delete query.me;
return `${this._super(...arguments)}/me`;
}
return this._super(...arguments);
}
});
Modified routes/appplication.js
to load the the current user as per the ESA guide
// app/routes/application.js
import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
const { service } = Ember.inject;
export default Ember.Route.extend(ApplicationRouteMixin, {
currentUser: service(),
beforeModel() {
return this._loadCurrentUser();
},
sessionAuthenticated() {
this._super(...arguments);
this._loadCurrentUser();
},
_loadCurrentUser() {
return this.get('currentUser').load().catch(() => this.get('session').invalidate());
}
});
However, this doesn’t seem to work. The code within _loadCurrentUser
of routes/application.js
constantly fails and then invalidates the session after each authentication.
If I remove calls to _loadCurrentUser()
with routes/application.js
, then the session remains authenticated. However, as a result, no user is loaded into the Ember Data Store.
The guide makes it seem all pretty straight forward, so I’m not sure what I’m doing wrong.