2016-07-13 120 views
2

如何直接設置登錄用戶?像流星 - 設置登錄用戶

Picker.route('sayhi', (params, req, res) => { 
    if (req.method === 'POST') { 
    const customUserId = getCustomUserId(req.headers); 
    Meteor.setUserId(customUserId); 
    console.log("hey I'm", Meteor.user()); 
    res.end('ok'); 
    } 
}); 

回答

2

對於它的價值,這是它的內部流星所做的那樣:

DDP._CurrentInvocation.withValue(new DDPCommon.MethodInvocation({ 
    isSimulation: false, userId: getCustomUserId(req.headers), 
}), function() { 
    // now things like Meteor.userId() work as expected 
    // also every method called here will get the right userId 
}); 

這種解決方案的唯一缺點是,它的感覺就像使用某種私有API的(或至少不記錄方法),這些方法可能會隨着時間而變化如果你不害怕它,那麼我相信這是一條路。

編輯

由於@Guig在評論中提到,一會還需要meteor add ddp-common

+0

謝謝,到目前爲止效果很好。您還需要在您的Meteor依賴項中包含DDPCommon軟件包 – Guig

+0

沒錯。我應該也可以把它寫在答案中。 –

+0

我覺得這個命令是'流星加ddp-common' :)感謝編輯 – Guig

0

東西你想允許管理員冒充用戶?如果是這樣,那麼gwendall:impersonate軟件包可能對您有所幫助。

Impersonate.do(userId, callback) 

還是你想讓用戶以不同的方式進行身份驗證?有許多現有的包以多種方式進行身份驗證。

+0

通過查看「impersonate」包,它看起來像這個解決方案只適用於客戶端。 –