javascript - Expressjs how to showhide a div in case user its logged - Stack Overflow

I followed this example -> Works fine, but I dont like the way they display the content one time you

I followed this example ->

Works fine, but I dont like the way they display the content one time you successfully logged in or logged out.

Its there other way to do X or Y one time user its authenticated ? Example

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(); }
}

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 ??

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>.';
} 

Think its not the way to go.

I followed this example -> https://github./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.

Its there other way to do X or Y one time user its authenticated ? Example

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(); }
}

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 ??

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>.';
} 

Think its not the way to go.

Share Improve this question asked Sep 17, 2012 at 10:41 NonyckNonyck 6821 gold badge13 silver badges29 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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:

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();
});

Use this variable in your template engine (for example jade):

if user
  // show stuff here
else
  // for everyone else

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745345248a4623513.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信