Note that there are some explanatory texts on larger screens.

plurals
  1. POnode.js - logging in won't update to show username
    text
    copied!<p>I have a Node.js app that I am using to build a login system. I have the login system working, but I have to manually refresh the page for it to show 'username logged in'. It successfully hides the sign in/sign up fields that you need to fill out to log in.</p> <p>Here is my app.js code:</p> <pre><code>/** * Application Configuration */ app.configure(function(){ app.set('port', process.env.PORT || 3000); app.set('views', __dirname + '/views'); app.enable('jsonp callback'); app.set('view engine', 'jade'); app.set('view options', {layout : false}); app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.session({ secret : 'sessionsecret' })); app.use(express.methodOverride()); app.use(express.static(__dirname + '/public')); app.use(function(req, res, next){ res.locals.session = req.session; next(); }); app.use(app.router); }); </code></pre> <p>And here is the part of <code>login.js</code> where I am hiding the login form.</p> <pre><code> if(username &amp;&amp; password){ $.post('/login', $(this).serialize(), function(response){ if(response.retStatus === 'success'){ $('#loginForm').hide(); $('#signup-btn').hide(); //$(location).attr('href', '/admin'); }else if(response.retStatus === 'failure'){ $('#signup-error-modal').modal('show'); } }); } }); </code></pre> <p>And finally this is the JADE/HTML code for the view where it should be showing the username automatically</p> <pre><code>if(session.user) div#logoutsection.pull-right a#logout-btn.btn.btn-info.pull-right.top-bar-form-button(href='/logout') Logout p#loginprompt.pull-right.login-prompt #{session.user.username} small logged In else a#signup-btn.btn.pull-right.top-bar-form-button(href='#signup-modal', data-toggle="modal") Sign Up form.pull-right.form-horizontal.top-bar-form(method="post", id="loginForm") input.input-small.top-bar-form-textbox(id="username", type="text", name="User", placeholder="Username") input.input-small.top-bar-form-textbox(id="password", type="password", name="Password", placeholder="Password") button.btn.btn-primary.top-bar-form-button(type="submit", value="Sign In") Sign In </code></pre> <p>Let me know if you want to see anymore code, I think I have it all here. But I really don't get why I have to manually refresh for it to show the username. Any ideas welcome?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload