【问题标题】:Authentification with ember-simple-auth in before model hook在 beforemodel 钩子中使用 ember-simple-auth 进行身份验证
【发布时间】:2018-09-24 18:10:21
【问题描述】:

我创建了一个应用程序,该应用程序需要在除需要使用刷新令牌进行身份验证的一页 (mobile_messages) 之外的所有页面上使用电子邮件/密码实现身份验证。 我从 JWT 身份验证器扩展并覆盖身份验证方法。所以它看起来像:

authenticate (credentials, headers) {
return new Ember.RSVP.Promise((resolve, reject) => {
  this.makeRequest('/auth/mobile_token', credentials, headers)
    .then((response) => {
      Ember.run(() => {
        try {
          const sessionData = this.handleAuthResponse(response)
          resolve(sessionData)
        } catch (error) {
          reject(error)
        }
      })
    }, (xhr) => {
      Ember.run(() => { reject(xhr.responseJSON || xhr.responseText) })
    })
})

}

在 mobile_messages 路由上,我尝试在模型挂钩之前进行身份验证。

beforeModel (transition) {
const authenticator = 'authenticator:api-token'
return this.get('session').authenticate(authenticator, {api_token: transition.queryParams.api_token}).then(() => {
  }, (reason) => {
  transition.abort()
  this.get('notifications').error('Permission denied.', {
    autoClear: true,
    clearDuration: 6200
  })
})

},

如果身份验证被拒绝,我需要留在 mobile_messages 路由上。但是当我使用错误的令牌进入路由时,我得到了下一个回溯:

Preparing to transition from '' to 'mobileMessages'
index.js:169 generated -> controller:mobileMessages {fullName: 
"controller:mobileMessages"}
index.js:169 generated -> controller:aut`enter code here`henticated 
{fullName: "controller:authenticated"}
index.js:169 generated -> controller:loading {fullName: 
"controller:loading"}
router.js:300 Intermediate-transitioned into 'authenticated.loading'
index.js:169 generated -> route:messages-faxes {fullName: 
"route:messages-faxes"}
router.js:190 Transitioned into 'login'
jquery.js:9600 POST http://localhost:3000/auth/mobile_token 500 
(Internal Server Error)

看起来我在收到服务器响应之前被重定向了。我找不到从路线重定向我的人。我尝试检查 ApplicationRouteMixin 但只有当您单击注销按钮时才会调用 sessionInvalidated 方法。并且认证成功后sessionAuthenticated。

如果我推送正确的令牌,那么我首先重定向到登录页面,然后 sessionAuthenticated 触发。之后我重定向到 baseURL。

如何解决重定向到登录页面的问题?

【问题讨论】:

    标签: ember.js ember-simple-auth


    【解决方案1】:

    加载钩子出错。我在命名路线时出错。我创建了带有名称加载的路由以重定向到消息传真路由。在这种情况下,当模型钩子返回 promise ember 之前生成 route:application_loading。在 application_loading 路由中,我运行转换到具有 UnauthenticatedRouteMixin 的 messages-faxes 路由。这个 mixin 看到用户没有通过身份验证并重定向到加载页面。

    【讨论】:

      【解决方案2】:

      在您的路由中包含 UauthenticatedRouteMixin,如果会话未验证,则需要访问该路由。例如:

      // app/routes/login.js
      import UnauthenticatedRouteMixin from 'ember-simple- 
      auth/mixins/unauthenticated-route-mixin';
      
      export default Ember.Route.extend(UnauthenticatedRouteMixin);
      

      【讨论】:

        【解决方案3】:

        Ember Simple Auth 使用 Mixins 来确定在用户通过身份验证/未通过身份验证时应该发生的路由转换行为。

        例如,如果用户未经身份验证,此 mixin 将不允许用户停留在路线上:

        import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
        
        export default Ember.Route.extend(AuthenticatedRouteMixin, {
          // route code
        });
        

        您可能想要使用的是UnauthenticatedRouteMixin

        此 mixin 用于使路由仅在会话可用时才可访问 未经过身份验证(例如,登录和注册路由)。它定义了一个 中止当前转换的 beforeModel 方法,而是 如果会话是,则转换到 routeIfAlreadyAuthenticated 已通过身份验证。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-19
          • 1970-01-01
          • 2015-04-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多