Last active
September 3, 2018 10:51
-
-
Save mhluska/8d7b4b394a844d72a5bbe0074fb935b0 to your computer and use it in GitHub Desktop.
Ember-simple-auth Current User Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Service, { inject as service } from '@ember/service'; | |
import { computed } from '@ember/object'; | |
import { alias } from '@ember/object/computed'; | |
export default Service.extend({ | |
store: service(), | |
session: service(), | |
auth: alias('session.data.authenticated'), | |
userId: computed('auth.user_id', function() { | |
const userId = this.get('auth.user_id'); | |
return userId ? userId.toString() : null; | |
}), | |
profileId: computed('auth.profile_id', function() { | |
const profileId = this.get('auth.profile_id'); | |
return profileId ? profileId.toString() : null; | |
}), | |
user: computed('userId', function() { | |
const userId = this.get('userId'); | |
return userId ? this.get('store').findRecord('user', userId) : null; | |
}), | |
profile: computed('user.profile', function() { | |
return this.get('user.profile'); | |
}), | |
isSettled: computed('user.isSettled', function() { | |
// Check for `false` since `null` is considered settled (logged out). | |
return this.get('user.isSettled') !== false; | |
}), | |
reload() { | |
this.notifyPropertyChange('userId'); | |
this.notifyPropertyChange('user'); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment