gpt4 book ai didi

node.js - keystone.js 当前登录的用户

转载 作者:太空宇宙 更新时间:2023-11-04 03:26:56 24 4
gpt4 key购买 nike

我是 Keystone 的新手,但一整天都在尝试查找当前登录的用户名,但我不清楚如何执行此操作。

例如,如果我从 keystone 获取索引 View

{{!< 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>

我想将用户名添加到 View 代码中,例如

<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 中定义其他内容吗?

如果有人可以帮助提供一个示例或指向正确方向的指针,我将非常感激!

最佳答案

如果您使用 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}}

关于node.js - keystone.js 当前登录的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43373100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com