Changing a property of a session


#1

Hello Everyone,

I do have a login-page component like this, in which I try to set the username property of my session service to username provided by the login form when authenticating:

import Ember from ‘ember’;

export default Ember.Component.extend({

authManager: Ember.inject.service(‘session’),

actions: { authenticate() { const {login, password} = this.getProperties(‘login’, ‘password’); this.get(‘authManager’).authenticate(‘authenticator:oauth2’, login, password).then(() => { this.get(‘authManager’).set(‘username’, login); console.log(this.get(‘authManager’).get(‘username’)); alert(‘Success! Click the top link!’); }, (err) => { alert('Error obtaining token: ’ + err.responseText); }); } } });

And here is the session service:

import Ember from ‘ember’; import DS from ‘ember-data’; import ESASession from “ember-simple-auth/services/session”;

export default ESASession.extend({

store: Ember.inject.service(),

username: “Anonymous”,

currentUser: Ember.computed(‘isAuthenticated’, ‘username’, function() { console.log(“This block runs”); if (this.get(‘isAuthenticated’)) { const promise = this.get(‘store’).queryRecord(‘user’, {username: this.get(‘username’)}); return DS.PromiseObject.create({ promise: promise }); } }),

});

Even though, the authenticate() action from the login-page component executes, it doesn’t change the username property of my session service. So the service queries the back-end with the username value of “Anonymous”. But the console log after the set method (in the authenticate action) prints out the correct username provided by the login form. This has been baffling me for quite sometime today, therefore I would be glad if you guys could help with this situation.

Best Regards


#2

Hello again,

I was able to understand why above problem occurs. It’s because property state gets back to initial values after a page reload which I was doing with Ember Inspector.

Best Regards,