2017-03-26 00:56:19 -07:00
|
|
|
import Ember from 'ember';
|
|
|
|
|
2017-05-22 19:55:07 -07:00
|
|
|
const { get, set, isBlank, $: { getJSON }, inject: { service }, Service } = Ember;
|
2017-03-26 00:56:19 -07:00
|
|
|
const currentUserUrl = '/api/v1/user/me';
|
|
|
|
|
|
|
|
export default Service.extend({
|
|
|
|
session: service(),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to load the currently logged in user.
|
|
|
|
* If the userName is found from a previously retained session,
|
|
|
|
* append to service. Request the full user object, and append
|
|
|
|
* to service.
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
load() {
|
|
|
|
const userName = get(this, 'session.data.authenticated.username');
|
|
|
|
|
|
|
|
if (!isBlank(userName)) {
|
|
|
|
set(this, 'userName', userName);
|
|
|
|
}
|
|
|
|
|
2017-05-22 19:55:07 -07:00
|
|
|
// If we have a valid session, get the currently logged in user, and set the currentUser attribute,
|
|
|
|
// otherwise raise an exception
|
2017-03-26 00:56:19 -07:00
|
|
|
if (get(this, 'session.isAuthenticated')) {
|
2017-05-22 19:55:07 -07:00
|
|
|
return Promise.resolve(getJSON(currentUserUrl)).then(
|
|
|
|
({ status = 'failed', user = {} }) =>
|
|
|
|
(status === 'ok'
|
|
|
|
? Promise.resolve(set(this, 'currentUser', user))
|
|
|
|
: Promise.reject(new Error(`Load current user failed with status: ${status}`)))
|
|
|
|
);
|
2017-03-26 00:56:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve();
|
2017-05-22 19:55:07 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidates the current session if the session is currently valid
|
|
|
|
* useful if, for example, the server is no able to provide the currently logged in user
|
|
|
|
* @return {Boolean|*|Ember.RSVP.Promise}
|
|
|
|
*/
|
|
|
|
invalidateSession() {
|
|
|
|
const sessionService = get(this, 'session');
|
|
|
|
return sessionService.isAuthenticated && sessionService.invalidate();
|
2017-03-26 00:56:19 -07:00
|
|
|
}
|
|
|
|
});
|