Express.js如何在用户登录时显示/隐藏div [英] Expressjs how to show / hide a div in case user its logged
问题描述
我遵循了此示例-> https://github.com/visionmedia/express/tree/master/examples/auth
I followed this example -> https://github.com/visionmedia/express/tree/master/examples/auth
效果很好,但我不喜欢它们在您成功登录或注销时显示内容的方式.
Works fine, but I dont like the way they display the content one time you successfully logged in or logged out.
是否还有另一种方法可以让用户一次性通过X或Y身份验证?例子
Its there other way to do X or Y one time user its authenticated ? Example
我有一个线性登录形式,很高兴将div一次通过身份验证的用户隐藏起来,但是我不能这样做
I have a linear form for login, will be nice to hide that div one time user it's authenticated, but I can't do this
$(document).ready(function(){
if(req.session.user) { $(.class of teh frm to hide).hide(); }
}
我也尝试过session.user
I tried too session.user
每时每刻都在告诉我它的含义.我把它放在布局上.
All the time tells me its undefined. I placed this on layout.
那么,如果用户通过身份验证,或者如果用户是管理员,我如何在视图上显示内容?
So how I show things on a view if user its authenticated or, if user it's admin ??
导致atm是我看到它授权某些路线并通过
Cause atm the only way I see it's authorizate some routes and display specific content via
req.session.success = 'Authenticated as ' + req.session.user.name
+ ' click to <a href="/logout">logout</a>. '
+ ' You may now access <a href="/restricted">/restricted</a>.';
}
认为这不是要走的路.
推荐答案
最好使用模板引擎来显示不同的内容,而不要使用客户端JavaScript.如果您使用的是玉石,请执行以下操作:
It is better to use your template engine to show different contents instead of client-side javascript. If you are using jade, do the following:
在app.js中,创建一个变量,如果用户登录,该变量将传递到每个视图:
In your app.js create a variable that is passed to every view if the user is logged in:
app.use(function(req, res, next){
// all the stuff from the example
if (req.session.user) {
res.locals.user = req.session.user
}
next();
});
在模板引擎(例如jade)中使用此变量:
Use this variable in your template engine (for example jade):
if user
// show stuff here
else
// for everyone else
这篇关于Express.js如何在用户登录时显示/隐藏div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!