代码之家  ›  专栏  ›  技术社区  ›  Nikolay Shcherba

在模型挂钩之前使用ember simple auth-in进行身份验证

  •  0
  • Nikolay Shcherba  · 技术社区  · 6 年前

    我创建了一个应用程序,需要在所有页面上使用电子邮件/密码进行身份验证,只有一个页面(mobile\u消息)需要使用刷新令牌进行身份验证。 我从JWT验证器扩展并重写了authenticate方法。所以看起来像:

    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\u消息路由上,我尝试在模型挂钩之前进行身份验证。

    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\u消息路由上。但当我使用wront令牌进入路线时,我得到了下一个回程:

    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激发。之后,我重定向到baseURL。

    要解决重定向到登录页面的问题,是否很热门?

    3 回复  |  直到 6 年前
        1
  •  0
  •   handlebears    6 年前

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

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

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

    您可能想要使用的是 UnauthenticatedRouteMixin

    此mixin用于仅当会话为 未通过身份验证(例如,登录和注册路由)。它定义了 中止当前转换的beforeModel方法 如果会话 已验证。

        2
  •  0
  •   sandeep chauhan    6 年前

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

    // app/routes/login.js
    import UnauthenticatedRouteMixin from 'ember-simple- 
    auth/mixins/unauthenticated-route-mixin';
    
    export default Ember.Route.extend(UnauthenticatedRouteMixin);
    
        3
  •  0
  •   Nikolay Shcherba    6 年前

    这是装料钩的一个错误。我在命名路由时出错。我创建了带有名称加载的路由,以重定向到消息传真路由。在这种情况下,在模型挂钩返回promise ember之前生成路由:application\u loading。在application\u loading route中,我运行到消息传真路由的转换,该路由具有未经身份验证的路由。这个mixin可以看到用户没有经过身份验证,并重定向到加载页面。