2016-03-06 44 views
2

如果不對任何路由應用策略,hapi-auth-cookie正在保護所有路由,包括靜態路由。hapi-auth-cookie保護所有路由,包括靜態的

server.register(require('hapi-auth-cookie'), function (err) { 

    if (err) { 
     logError(err); 
    } 

    server.auth.strategy('session', 'cookie', true, { 
     password: 'things really long', 
     clearInvalid: true, 
     isSecure: false, 
     validateFunc: function (request, session, callback) { 

      cache.get(session.sid, (err, cached) => { 

       if (err) { 
        return callback(err, false); 
       } 

       if (!cached) { 
        return callback(null, false); 
       } 

       return callback(null, true, cached.account); 
      }); 
     } 
    }); 
}); 

這裏是我的路線:

{ 
    method: 'POST', 
    path: '/api/1/doSomething', 
    config: { 
    validate: { 
     payload: someJoyObject 
    }, 
    handler: function(request, reply) { 
    stuff 
    } 
    } 
} 

{ 
    method: 'GET', 
    path: '/{param*}', 
    handler: { 
     directory: { 
      path: './public', 
      listing: false, 
      index: true 
     } 
    } 
} 

我無法載入我的應用程序的任何文件:

{"statusCode":401,"error":"Unauthorized","message":"Missing authentication"} 

回答

4

看一看的documentation for server.auth.strategy()。您將通過true作爲第三個參數,這意味着hapi將默認將此策略應用於所有路由。

要禁用它爲您的靜態路由之一:

  1. 不要把它設置爲所有路由所需的策略
  2. 明確禁用策略的目錄處理路線:

例如:

{ 
    method: 'GET', 
    path: '/{param*}', 
    config: { 
     auth: false 
    }, 
    handler: { 
     directory: { 
      path: './public', 
      listing: false, 
      index: true 
     } 
    } 
} 
+1

謝謝,馬特。不能相信我錯過了這一點。對於其他人:'server.auth.strategy(名稱,方案,[模式],[選項])'和'模式 - 如果爲true,則該方案被自動分配爲任何路由的必需策略,而不需要auth配置。 ' –