dagda1
dagda1

Reputation: 28820

Ember.js authentication, pushstate and routing

I wonder if anybody has any thoughts on Ember.js in conjunction with routing and pushstate.

The typical Ember example for authentication is:

aStateManager = Em.StateManager.create({
    initialState: 'unauthenticated',
    authenticated: Em.State.create({}),
    unauthenticated: Em.State.create({
      authenticate: function(stateManager, context){
        stateManager.goToState('authenticated')
      }
    })
  })

  aStateManager.send('authenticate')

That will work if a user is always sent to the root url. But what if the user types the the url into the address bar such as '/api/resource/1', is there a way to tell if the user is authenticated or not in each state?

I guess what I am looking for is a rails like before_filter.

Has anyone come up with a solution for this common scenario?

Upvotes: 3

Views: 1371

Answers (1)

Gaurav Shetty
Gaurav Shetty

Reputation: 461

The issue we are going to face with any sort of authentication is that the user has complete access to all the models and can change the data in them and give himself permissions he did not previously have. And contacting the server every time would be a pain.

One solution I can think of off the top of my mind is to use the freezable mixin and the isDirty flag of objects. We can have the permissions for a user as a DS model(if we are using ember data) and then define it as frozen. Now we create a permission check mixin that checks to see if the model is dirty (ie it has been changed). And also respect the frozen status in all other parts of code.

Of course I have not thought of various other use cases but this should be a valuable starting point.

Upvotes: 1

Related Questions