当前位置: 动力学知识库 > 问答 > 编程问答 >

ember.js - forbid certain (most) routes if not logged in

问题描述:

I'm using Ember for my whole site, even the static pages.

I have a devise api setup, with a user.new, user.login and user.edit in order to create, login or edit an existing account.

Any user can just go to

http://hostname/user/edit

to see the edit screen. I would realy like to make this only possible if a user is logged in. If not that route should for example redirect to the login page.

Is there a way to do that?

网友答案:

You can put a redirect inside your route. In there you can conditionally check to see if the user is logged in. If they aren't, then you can transition somewhere else.

See the guides for more about redirection.

Update

Here is an example from an AuthenticatedRoute that I use. For routes that I was to be authenticated, I just subclass from this one.

var AuthenticatedRoute = Ember.Route.extend({
  session: Ember.inject.service(),
  ...

  beforeModel: function(transition) {
    if (Ember.isEmpty(this.get('session').get('token'))) {
      this.get('flashNotification').notify({
        title: "You need to sign in first."
      });
      return this.transitionTo('sign-in');
    }
  },

Hopefully that gives some good direction.

If you need more resources, check out:

  • Embercasts
  • Ember Screencasts - I haven't seen this one (since it is behind the paywall, but their videos seem to be pretty useful.
分享给朋友:
您可能感兴趣的文章:
随机阅读: