簡體   English   中英

模型掛鈎之前使用ember-simple-auth進行身份驗證

[英]Authentification with ember-simple-auth in before model hook

我創建了一個應用程序,需要在除一頁(mobile_messages)之外的所有頁面上使用電子郵件/密碼進行身份驗證,在該頁面上需要使用刷新令牌進行身份驗證。 我從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_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 Simple Auth使用Mixins來確定如果對用戶進行身份驗證/未身份驗證,則應該發生的路由轉換行為。

例如,如果用戶未經身份驗證,此混合將不允許用戶停留在路線上:

import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';

export default Ember.Route.extend(AuthenticatedRouteMixin, {
  // route code
});

您可能要使用的是UnauthenticatedRouteMixin

僅在會話未通過身份驗證的情況下,此混合器才用於使路由可訪問(例如,登錄和注冊路由)。 它定義了一個beforeModel方法,該方法將中止當前轉換,如果會話已通過身份驗證,則轉換為routeIfAlreadyAuthenticated。

在您的路由中包括UnauthenticatedRouteMixin,如果會話未通過驗證,則需要訪問該路由。 例如:

// app/routes/login.js
import UnauthenticatedRouteMixin from 'ember-simple- 
auth/mixins/unauthenticated-route-mixin';

export default Ember.Route.extend(UnauthenticatedRouteMixin);

加載掛鈎是錯誤的。 我在命名路線時出錯。 我創建了帶有名稱加載的路由,以重定向到郵件傳真路由。 在這種情況下,當模型掛鈎返回之前,promer ember會生成route:application_loading。 在application_loading路由中,我運行到具有UnauthenticatedRouteMixin的消息-傳真路由的過渡。 此混入看到該用戶未通過身份驗證,並重定向到加載頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM