首页 文章

Ember Simple Auth如何在会话失效后重定向登录?

提问于
浏览
1

我正在使用ember simple auth,当我退出并且会话无效时,我被重定向回到站点的根目录 . 如何在无效会话中将其重定向到登录路由?

2 回答

  • 0

    在路由中使用经过身份验证的路由mixin,在身份验证后应该可以访问:

    import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
    
    AuthenticatedRoute = Ember.Route.extend(AuthenticatedRouteMixin, {
      // route you would like to redirect to after logout
      authenticationRoute: "routes.login",
    
      // ... your stuff ...
    })
    

    在根应用程序路由中使用也相应的mixin:

    import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
    
    ApplicationRoute = Ember.Route.extend(ApplicationRouteMixin, {
      // route you would like to redirect to after login
      routeAfterAuthentication: "routes.authenticated-route",
    
      // ... your stuff
    })
    

    这取决于您的需求,您将如何使用它 . 您可以将它放在每个经过身份验证的路由中,或者例如您可以将其放在一个路由中(例如,称为“/ auth”)并将另一个经过身份验证的路由放入此路由中 . 这些路由中的每一个都将进行身份验证,您的路由结构可能如下所示:

    /
      login
      auth
        alsoAuthenticatedRoute
          someAuthenticatedDetailRoute
        anotherAuthenticatedRoute
          anotherAuthenticatedDetailRoute
    
  • 1

    您可以将其用于读取会话

    this.get('session.data.currentUser')
    

    处理你想要的任何东西 .

    应用程序/ router.js

    Router.map(function() {
      this.route('posts');
      this.route('post', { path: '/post/:post_id' });
    

    });

    应用程序/路由/ index.js

    import Ember from 'ember';
    export default Ember.Route.extend({
      afterModel(model, transition) {
        if (model.get('length') === 1) {
           this.transitionTo('post', model.get('firstObject'));
        }
     } 
    });
    

    用这个

相关问题