use*_*152 13 javascript meteor
在我的Meteor.js应用程序中,我想让管理员能够强制注销用户.
用例是我的应用程序正在为最终用户提供服务,并且每当超级用户登录时该服务都是打开的.如果超级用户忘记明确注销,该服务似乎是开放的 - 用户.如果管理员看到这一点,他/她应该能够强制注销登录用户,这样就会为最终用户关闭服务.
这可能与Meteor.js有关吗?如果是这样,怎么样?这个用例有更好的/其他方法吗?
编辑:添加了一些我尝试过的远程注销示例,以便为@Akshat进行澄清.
例1(不能按我的意愿工作):
在注销方法中:
if (user.profile.role === ROLES.ADMIN) {
Meteor
.users
.update({
_id: options.userId
},
{
$set: {
'services.resume.loginTokens' : []
}});
} else {
throw new Meteor.Error(403, "You are not allowed to access this.");
}
Run Code Online (Sandbox Code Playgroud)
在我的application.js中:
var lastUserId;
Deps.autorun(function () {
if(Meteor.user()) {
if (Meteor.user().profile && Meteor.user().profile.firstName) {
console.log("USER LOGGED IN");
console.log("LENGTH LOGINTOKENS",
Meteor
.user()
.services
.resume
.loginTokens.length); // This is always 1
lastUserId = Meteor.user()._id;
if (Meteor.user().services.resume.loginTokens.length === 0) {
// This never fires, and thus the client does not know until
// manually refreshed. Of course I could keep a forceLogOut-variable
// as done in the next example.
window.location.reload();
}
}
} else {
console.log("SOMETHING CHANGED IN METEOR.USER");
if (lastUserId) {
console.log("THE USER IS LOGGED OUT");
Meteor.call('userLoggedOut',
{
userId: lastUserId
});
lastUserId = null;
}
}
});
Run Code Online (Sandbox Code Playgroud)
示例2(当我在客户端使用forceLogOut和Meteor.logout()时,这可以正常工作.):
在注销方法中:
if (user.profile.role === ROLES.ADMIN) {
Meteor
.users
.update({
_id: options.userId
},
{
$set: {
'services.resume.loginTokens' : [],
'profile.forceLogOut': true
}});
} else {
throw new Meteor.Error(403, "You are not allowed to access this.");
}
Run Code Online (Sandbox Code Playgroud)
在我的application.js中:
var lastUserId;
Deps.autorun(function () {
if(Meteor.user()) {
if (Meteor.user().profile && Meteor.user().profile.firstName) {
console.log("USER LOGGED IN");
console.log("LENGTH LOGINTOKENS",
Meteor
.user()
.services
.resume
.loginTokens.length); // This is always 1
lastUserId = Meteor.user()._id;
if (Meteor.user().profile.forceLogOut) {
// Small example 1:
// When logintokens have been set to [], and forceLogOut
// is true, we need to reload the window to show the user
// he is logged out.
window.location.reload();
// END Small example 1.
// Small example 2:
// When already keeping this variable, I might as well just use
// this variable for logging the user out, and no resetting of
// loginTokens are needed, or reloading the browser window.
// This seems to me as the best way.
console.log("FORCING LOGOUT");
Meteor.logout();
// END Small example 2.
// And finally resetting the variable
Meteor.call('resetForceLogOut',
{
userId: Meteor.user()._id
});
}
}
} else {
console.log("SOMETHING CHANGED IN METEOR.USER");
if (lastUserId) {
console.log("THE USER IS LOGGED OUT");
Meteor.call('userLoggedOut',
{
userId: lastUserId
});
lastUserId = null;
}
}
});
Run Code Online (Sandbox Code Playgroud)
Aks*_*hat 26
您必须loginTokens从数据库中删除所有内容.它将通过此查询为所有用户执行此操作.如果要注销较小的用户子集或排除当前用户,则可以自定义选择器.
Meteor.users.update({}, {$set : { "services.resume.loginTokens" : [] }}, {multi:true});
Run Code Online (Sandbox Code Playgroud)
有几件事:
| 归档时间: |
|
| 查看次数: |
6287 次 |
| 最近记录: |