2013-12-11 4 views
10

Meteor.jsアプリケーションでは、管理者が強制的にユーザーをログアウトできるようにしたいと考えています。Meteor.jsのサーバからユーザをログアウトするにはどうしたらいいですか?

私のアプリケーションはエンドユーザにサービスを提供しており、スーパーユーザがログインするたびにサービスが開かれています。スーパーユーザが明示的にログアウトするのを忘れた場合、サービスは開いているようですエンドユーザーのために。管理者がこれを見ると、ログインしたユーザーを強制的にログアウトすることができ、エンドユーザーのためにサービスが閉じられるはずです。

これはMeteor.jsで可能ですか?もしそうなら、どうですか?このユースケースに対して、より良い/他のアプローチがありますか?

編集:@Akshatを明確にするために、私が試したリモートログアウトのいくつかの例を追加しました。

例1(私が望むように動作しません):ログアウト方法で

:私application.jsで

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."); 
     } 

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; 
     } 
    } 
}); 

例2(本作品私が望むように、クライアント側でMeteor.logout()と一緒にforceLogOutだけを使用する場合):

ログアウト方法:私application.jsで

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."); 
     } 

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; 
     } 
    } 
}); 

答えて

22

あなたは、データベースからすべてのloginTokensを削除する必要があります。これは、このクエリを持つすべてのユーザーに対して実行されます。より小さなサブセットのユーザーをログアウトする場合や、現在のユーザーを除外する場合は、セレクターをカスタマイズできます。

Meteor.users.update({}, {$set : { "services.resume.loginTokens" : [] }}, {multi:true}); 

物事のカップル:これは、現在ログインしているユーザーを含め、すべてのユーザーをログアウトします

  • それだけ
+0

!速やかなご返信ありがとうございます。これはどこに文書化されていますか、btwですか? – user2602152

+2

これは文書化されているとは思えませんが、Meteorはログインしているユーザーを識別するために 'loginTokens'の中の値を使用しています。したがって、それらがなければすべてが反応するのでログオフされます – Akshat

+0

時々それは魅力のように機能し、ユーザーはログインページにリダイレクトされます。しかし、他の時間では、ユーザは依然として「制限区域」内を移動することができる。ユーザーがログアウトしていることを確認するために、リフレッシュを行う必要があります。これをどうすれば解決できますか? – user2602152

2

ログアウトみんなになく、サーバ側で動作しますない、現在のユーザー(あなた):私は一度これをしようとします

Meteor.users.update({ 
    _id: { 
     $ne: Meteor.user()._id 
    } 
}, { 
    $set: { 
     "services.resume.loginTokens": [] 
    } 
}, { 
    multi: true 
}); 
関連する問題