Reputation: 1027
so I'm making a simple log in form in my site using Jade and ExpressJS. It's all working, the only problem I'm having is when the user enters incorrect log in details. When they enter wrong details I redirect them to the log in page with a parameter in the URL (/admin?falseLogIn
). My question is: how can I check if this GET variable exists in my jade view?
This is my code:
log-in.jade
form(action="/log-in" method="post")
label Username
input(type="text" class="form-control" name="username" placeholder="Username")
label Password
input(type="text" class="form-control" name="password" placeholder="Password")
input(type="submit" class="btn btn-info pull-right" style="margin-top: 15px;" value="Log In")
app.js
app.get("/admin",function(req, res){
if(req.session.userName){
res.render("admin", {session_name: req.session.userName});
}else{
res.render("log-in");
}
});
app.post("/log-in", function(req, res){
var admin = Admin.findOne({"username": req.body.username, "password": req.body.password});
admin.count(function(error, count){
//console.log(count);
if(count > 0){
req.session.userName = req.body.username;
res.redirect("/admin");
}else{
res.redirect("/admin?falseLogIn");
}
});
});
I tried the following inside my jade view but get an error Cannot read property 'location' of undefined
:
- var page = window.location.pathname
if page === "/admin?falseLogIn"
.alert.alert-danger Error! Wrong details, please try again!
I come from a PHP background, so in PHP i would be able to check directly in the html using:
<?php if(isset($_GET['falseLogIn'])){// display error to user } ?>
Any help is greatly appreciated. Thanks!
Upvotes: 1
Views: 897
Reputation: 516
First, I would recommend using full key-values for your query string, /admin?login=false
Then you use req.query
to get the specific variable and pass into your jade view or using res.locals
to make the variable accessible in your views.
app.js
app.get("/admin",function(req, res){
res.locals.login = req.query.login; // this would be accessible in your view as 'login'
if (req.session.userName) {
res.render("admin", {session_name: req.session.userName});
} else {
res.render("log-in", {
login: req.query.login, // or pass variable to your view directly, also accessible in your view as 'login'
});
}
});
Reasoning. I don't think it's good practice to parse your url query directly in your view. While that is fine for raw php like your example using $_GET, if you use a MVC framework, like Laravel for example, you quickly move away from that. Let the controller do its job of handling logic based on queries and handing off the relevant information to views. Then in your view, its should be simple presentation logic.
Upvotes: 3
Reputation: 2986
You should write you script under script(type='text/javascript').
script(type='text/javascript').
var page = window.location.pathname
But if you want to compare the url, you should get the search
part instead of pathname
:
script(type='text/javascript').
var page = window.location.search
if(page === '?falseLogIn'){/* some code here, display the error */}
Upvotes: 0