mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
Meteor.user() on the server (only in methods for now).
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
(function () {
|
||||
|
||||
Meteor.userId = function () {
|
||||
return Meteor.default_connection.userId();
|
||||
};
|
||||
|
||||
Meteor.user = function () {
|
||||
var userId = Meteor.default_connection.userId();
|
||||
var userId = Meteor.userId();
|
||||
if (userId) {
|
||||
var result = Meteor.users.findOne(userId);
|
||||
if (result) {
|
||||
|
||||
@@ -73,6 +73,32 @@
|
||||
});
|
||||
|
||||
|
||||
///
|
||||
/// CURRENT USER
|
||||
///
|
||||
Meteor.userId = function () {
|
||||
// This function only works if called inside a method. In theory, it
|
||||
// could also be called from publish statements, since they also
|
||||
// have a userId associated with them. However, given that publish
|
||||
// functions aren't reactive, using any of the infomation from
|
||||
// Meteor.user() in a publish function will always use the value
|
||||
// from when the function first runs. This is likely not what the
|
||||
// user expects. The way to make this work in a publish is to do
|
||||
// Meteor.find(this.userId()).observe and recompute when the user
|
||||
// record changes.
|
||||
var currentInvocation = Meteor._CurrentInvocation.get();
|
||||
if (!currentInvocation || !currentInvocation.userId)
|
||||
throw new Error("Meteor.userId can only be invoked in method calls.");
|
||||
return currentInvocation.userId();
|
||||
};
|
||||
|
||||
Meteor.user = function () {
|
||||
var userId = Meteor.userId();
|
||||
if (!userId)
|
||||
return null;
|
||||
return Meteor.users.findOne(userId);
|
||||
};
|
||||
|
||||
///
|
||||
/// CREATE USER HOOKS
|
||||
///
|
||||
|
||||
@@ -165,7 +165,28 @@ if (Meteor.isClient) (function () {
|
||||
test.equal(Meteor.user().profile.touchedByOnCreateUser, true);
|
||||
}));
|
||||
},
|
||||
logoutStep
|
||||
|
||||
// test Meteor.user(). This test properly belongs in
|
||||
// accounts-base/accounts_tests.js, but this is where the tests that
|
||||
// actually log in are.
|
||||
function(test, expect) {
|
||||
var clientUser = Meteor.user();
|
||||
Meteor.call('testMeteorUser', expect(function (err, result) {
|
||||
test.equal(result._id, clientUser._id);
|
||||
test.equal(result.profile.touchedByOnCreateUser, true);
|
||||
test.equal(err, undefined);
|
||||
}));
|
||||
},
|
||||
logoutStep,
|
||||
function(test, expect) {
|
||||
var clientUser = Meteor.user();
|
||||
test.equal(clientUser, null);
|
||||
Meteor.call('testMeteorUser', expect(function (err, result) {
|
||||
test.equal(err, undefined);
|
||||
test.equal(result, null);
|
||||
}));
|
||||
}
|
||||
|
||||
]);
|
||||
|
||||
}) ();
|
||||
@@ -234,6 +255,14 @@ if (Meteor.isServer) (function () {
|
||||
});
|
||||
|
||||
|
||||
// This test properly belongs in accounts-base/accounts_tests.js, but
|
||||
// this is where the tests that actually log in are.
|
||||
Tinytest.add('accounts - user() out of context', function (test) {
|
||||
// basic server context, no method.
|
||||
test.throws(function () {
|
||||
Meteor.user();
|
||||
});
|
||||
});
|
||||
|
||||
// XXX would be nice to test Meteor.accounts.config({forbidSignups: true})
|
||||
}) ();
|
||||
|
||||
@@ -32,3 +32,10 @@ Meteor.accounts.config({
|
||||
requireEmail: false,
|
||||
requireUsername: false
|
||||
});
|
||||
|
||||
|
||||
// This test properly belongs in accounts-base/accounts_tests.js, but
|
||||
// this is where the tests that actually log in are.
|
||||
Meteor.methods({
|
||||
testMeteorUser: function () { return Meteor.user(); }
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user