keystone.js当前登录用户

时间:2017-04-12 14:57:09

标签: node.js handlebars.js keystonejs

我是Keystone的新手,但是我一整天都在努力寻找当前登录的用户名,但我还不清楚如何做到这一点。

如果我从keystone获取索引视图,例如

{{!< 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>masterofimps@yahoo.co.uk</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中定义其他内容?

如果有人可以在正确的方向上帮助示例或指针,我真的非常感激!

1 个答案:

答案 0 :(得分:3)

如果您使用了Yeoman生成器,则其中一个中间件函数(initLocals)会将当前用户设置为本地变量。

res.locals.user = req.user;

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

您不需要在Handlebars中的变量名称前包含locals。因此,删除它以获取当前用户信息。

{{user}}
{{user.name}}