2017-04-12 128 views
0

我是新来的Keystone,但一直试图找到当前登录的用户名,但我不清楚如何做到这一点。keystone.js当前登录用户

如果我走索引视图从梯形例如

{{!< default}} 
<div class="container"> 
    <div class="jumbotron"><img src="/images/logo.svg" width="160"> 
     <h1>Welcome</h1> 
     <p>This is your new <a href='http://keystonejs.com' target='_blank'>KeystoneJS</a> website.</p> 
     <p> 
      It includes the latest versions of 
      <a href='http://getbootstrap.com/' target='_blank'>Bootstrap</a> 
      and <a href='http://www.jquery.com/' target='_blank'>jQuery</a>. 
     </p> 
     <p>Visit the <a href='http://keystonejs.com/guide' target='_blank'>Getting Started</a> guide to learn how to customise it.</p> 
     <hr> 
     <p>We have created a default Admin user for you with the email <strong>[email protected]</strong> and the password <strong>admin</strong>.</p> 
     <p><a href="/keystone/signin" style="margin-right: 10px" class="btn btn-lg btn-primary">Sign in</a> to use the Admin UI.</p> 
     <hr> 
     <p> 
      Remember to <a href='https://github.com/keystonejs/keystone' target='_blank'>Star KeystoneJS on GitHub</a> and 
      <a href='https://twitter.com/keystonejs' target='_blank'>follow @keystonejs</a> on twitter for updates. 
     </p> 
    </div> 
</div> 

,我想用户名添加到视图代码财产以后像

<h1>Welcome Tim</h1> 

我已经试过

<h1>Welcome {{locals.user}}</h1> 
<h1>Welcome {{locals.user.name}}</h1> 
<h1>Welcome {{req.user}}</h1> (using an Express request) 

但无济于事。如何从用户模型中找到用户名?我必须首先在.. \ routes \ views中定义其他内容吗?

如果有人可以帮助一个例子或指针在正确的方向,我真的很感激!

回答

3

如果使用了Yeoman生成器,则包含的一个中间件函数(initLocals)会将当前用户设置为局部变量。

res.locals.user = req.user; 

https://github.com/keystonejs/generator-keystone/blob/master/app/templates/routes/_middleware.js#L27

你并不需要包括locals在把手变量名的前面。所以删除它以获取当前用户信息。

{{user}} 
{{user.name}} 
+0

你先生,太棒了。在仔细阅读中间件代码后,我想我现在明白了。 – masterofimps