Access to user model in global signInSuccess handler
Closed this issue · 3 comments
Previously I was able to use the following code alongside my App.Auth
object to set up the user object in my exception notification system:
App.Auth.on 'signInComplete', ->
if App.Auth.get('signedIn')
App.Auth.get('user').on 'didLoad', ->
Raven.setUser
id: App.Auth.get('user.id')
username: App.Auth.get('user.username')
email: App.Auth.get('user.email')
App.Auth.on 'signOutSuccess', ->
Raven.setUser()
I saw that you are now supposed to use @auth.addHandler
which is fine, I have set up a beforeModel hook in my application controller that adds the handlers and it gets triggered when a user is signed in through the rememberable function.
However, the @auth.get('user').on 'didLoad'
doesn't do anything, and if I call @auth.get('user')
in my success handler it just returns null
.
Am I doing something wrong? What's the correct way to do this now?
PS. It may be useful to mention where to set global handlers in the documentation, it took me a while to workout where to put the @auth.addHandler
calls.
Can you post code of your success handler? Handlers get fired in an Ember.RSVP.all
promises array; after that, the signIn()
method itself would resolve (or reject). Same for signOut
, send
, createSession
and destroySession
. So, if you just want to run some code on the current user, just use the promise interface
@auth.signIn().then -> doSomething()
The current user would be guaranteed to be present when doSomething()
executes. If it doesn't, then it's a bug.
This is similar to how ember-data moved away from the cumbersome didLoad
, id == undefined
etc checks, to the promise interface.
I'm not aware of any ember idiom or best practice of where to put global handlers. It could be in an initializer, it could be by deferring application bootstrapping itself, it could be in application view's didInsertElement
, etc. That's why I'm leaving that to individual apps.
What I'm trying to do is something like this:
@auth.addHandler 'signInSuccess', =>
@auth.get('user').on 'didLoad', =>
console.log 'setting raven user'
Raven.setUser
id: @auth.get('user.id')
username: @auth.get('user.username')
email: @auth.get('user.email')
I'm not able to use @auth.signIn().then -> doSomething()
(as far as I'm aware) because that doesn't cover users that are logged in through the rememberable or url authentication methods.