2017-05-07 75 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 –