2017-05-07 74 views
0

我正在添加一個認證層,我想我已經想通了,除了一個棘手的細節。 我的Meteor應用程序沒有任何路由,但是我已經在連接中間件中添加了一個鉤子,以便在沒有正確的API令牌時「/」路由錯誤。如果令牌沒有問題,我可以撥打next()將路由轉發給Meteor。流星服務器中的每個請求會話?

問題是,根據令牌,我需要爲連接設置服務器端參數,我不知道該怎麼做。例如,假設我有一個映射到權限級別的API密鑰的靜態列表。如果用戶使用「ADMIN_API_KEY」發送請求,那麼我想設置Session.permission_level = "admin"以供Meteor服務器的功能使用。儘管如此,Session僅適用於Meteor的客戶端。

# this code's in coffeescript 

    WebApp.connectHandlers.use '/', (req, res, next) -> 
    validator = new RequestValidator(req, next) 
    validations = [ 
     "valid_namespace", 
     "only_https" 
    ] 
    error = validator.validate(validations) 
    next(error) 
    # <<<<<<<<<<<<<<<<<<<<<<<< 
    # Here I want to set some config option which can be 
    # read by the server in the same way it can read things like 
    # Meteor.user() 

在Rails中,我只會說session[:permission_level] = "admin"。但它似乎不適用於流星這種方式。順便說一句,我沒有在流星中使用路由軟件包,但是如果這樣做會比我更容易。

回答

1

我不知道Session我一直在做這樣的事情

import { DDP } from 'meteor/ddp'; 
import { DDPCommon } from 'meteor/ddp-common'; 

export const authMiddleware = (req, res, next) => { 
    const userId = identifyUser(req); // parse the request to get the token you expect 
    if (!userId) { 
    return next(); 
    } 

    DDP._CurrentInvocation.withValue(new DDPCommon.MethodInvocation({ 
    isSimulation: false, 
    userId, 
    }),() => { 
    next(); 
    // in that context, Meteor.userId corresponds to userId 
    }); 
}; 

我的REST API和行之有效關於用戶ID,並能夠召喚流星功能應該在被調用DDP上下文,如Users.find(...)

+0

問題是我在這個階段沒有登錄用戶,所以我看不到有效的用戶ID –

+0

。我不確定什麼是能夠使用像Session這樣的東西的最佳方式,它看起來是全球性的,但實際上與特定的上下文相關聯。我已經結束了傳遞所有參數,並在這些情況下將Meteor用作簡單節點後端 – Guig

+0

探索可用於流星的方法我看到有uid,基本上是sessoon id –