2013-01-16 30 views
2

我有一个用户配置文件视图,用户可以在其中编辑其配置文件信息。下面的一切都很好,更新成功。但是,当我注销帐户并使用其他用户帐户登录时,更新失败并返回Access denied错误。直到我刷新页面,我才能使用第二个帐户再次编辑配置文件信息。Meteor.users.update()在注销时不起作用+登录

我知道这种情况非常罕见,用户通常不会注销一个帐户,登录另一个帐户并尝试更新其个人资料,但我想更好地了解为什么会发生这种情况。当用户注销时是否刷新客户端令牌或是否存在需要完全重新加载页面的其他内容?

在客户机JS:

Template.user_profile_form.events({ 
    'click #submit_profile_btn': function(evt) { 
     evt.preventDefault(); 
     var first_name = $('#profile_first_name').val() 
      ,last_name = $('#profile_last_name').val() 
      ,email = $('#profile_email').val() 
      ,email_lower_case = email.toLowerCase() 
      ,gravatar_hash = CryptoJS.MD5(email_lower_case) 
     ; 

     gravatar_hash = gravatar_hash.toString(CryptoJS.enc.Hex); 

     // TODO need to create user sessions so that when you log out and log back in, you have a fresh session 
     Meteor.users.update({_id: this.userId }, { 
      $set: { 
       profile: { 
        first_name: first_name, 
        last_name: last_name, 
        gravatar_hash: gravatar_hash 
       } 
      } 
     }, function(error) { 
      if (!error) { 
       Session.set('profile_edit', 'success'); 
       Meteor.setTimeout(function() { 
        Session.set('profile_edit', null); 
       }, 3000); 
      } else { 
       Session.set('profile_edit', 'error'); 
       Template.user_profile_form.error_message = function() { 
        return error.reason; 
       }; 
      } 
     }); 

     Meteor.call('changeEmail', email); 
    } 
}); 

服务器JS:

Meteor.publish('list-all-users', function() { 
    return Meteor.users.find({_id: this.userId }, { 
     fields: { 
      profile: 1, 
      emails: 1 
     } 
    }); 
}); 

Meteor.methods({ 
    sendEmail: function(to, from, subject, text) { 
     this.unblock(); 

     Email.send({ 
      to: to, 
      from: from, 
      subject: subject, 
      text: text 
     }); 
    }, 
    changeEmail: function(newEmail) { 
     // TODO Need to validate that new e-mail does not already exist 
     Meteor.users.update(Meteor.userId(), { 
      $set: { 
       emails: [{ 
        address: newEmail, 
        verified: false 
       }] 
      } 
     }); 
    } 
}); 

模板:

<template name="user_profile_form"> 
    <h2>Update Profile</h2> 
    <div id="profile-form"> 
     {{#if success}} 
      <div class="alert alert-success"> 
       <strong>Profile updated!</strong> Your profile has been successfully updated. 
      </div> 
     {{/if}} 
     {{#if error}} 
      <div class="alert alert-error"> 
       <strong>Uh oh!</strong> Something went wrong and your profile was not updated. {{error_message}}. 
      </div> 
     {{/if}} 
     <p> 
      {{#each profile}} 
      <input type="text" id="profile_first_name" placeholder="First Name" value="{{first_name}}"> 
      <input type="text" id="profile_last_name" placeholder="Last Name" value="{{last_name}}"> 
      {{/each}} 
      <input type="email" id="profile_email" placeholder="Email" value="{{email_address}}"> 
     </p> 
    </div> 
    <div id="submit-btn"> 
     <input type="submit" id="submit_profile_btn" class="btn btn-primary"> 
    </div> 
</template> 

回答

2

流星注销功能几乎没做什么。它当然不会推倒会话状态或其他应用程序的上下文。您的代码必须在您的应用注销事件期间重置这些变量。手动刷新页面会导致客户端JavaScript重新加载擦除现有会话数据。

+0

注销事件是所有由accounts-ui处理 - 当你注销时是否有回调函数?也许像[{{loggingin}}](http://docs.meteor.com/#template_loggingin)但注销? –

+0

此外 - 如果注销确实没有关闭与用户相关的会话状态,这不是账户中的错误 - 用户界面? –

+0

这取决于。在我们的应用程序中,我们提供了一个明确的注销链接。相关的事件处理程序显式调用[Meteor.logout](http://docs.meteor.com/#meteor_logout)。 –

0

如果你不想惹你可以使用下面的模式(代码的CoffeeScript)从Session清除各个变量的账户的UI模板内部:

Deps.autorun (c) -> 
    user = Meteor.user() 
    if user 
    # Setup code on login (if required) 
    else if not user 
    # Clear session on logout 
    Session.set "profile_edit", undefined