Rudi
Rudi

Reputation: 1587

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?

Upvotes: 2

Views: 1209

Answers (1)

RyanJM
RyanJM

Reputation: 7068

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:

Upvotes: 5

Related Questions