Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6907049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:23:14+00:00 2026-05-27T08:23:14+00:00

I have some questions regarding login and sessions. I have this code: The db

  • 0

I have some questions regarding login and sessions. I have this code:

The db query:

login: function(req,callback) {
    var query = 'SELECT id FROM users WHERE email = "' + req.body.email_login + '" AND password = "' + hashlib.sha1(req.body.password_login) + '" LIMIT 1';
    client.query(query, callback);
}

The route:

app.post('/login', function(req, res, next) {

    users.login(req,function(err, results) {
        if (err) {
            res.render('index');
        } else if (results[0]) {
            req.session.userdata = results[0];
                req.session.is_logged_in = true;
                res.render('site/news');
        }

    }
}

Auth middleware:

var auth = function (req, res, next) {
    if (req.session.userdata && req.session.is_logged_in === true) {
        next();
    } else {
        res.redirect('/');
    }
}

I use db store for the session.

Now my questions are:

1) Is this a safe way to do it? Or should I consider doing it some other way?

2) Say I have this URL /domain/users/1, where the last segment is the user id which is used to fetch user data.
And on that view I have a form for changing user data. Is it safe to check if the user id matches the session user id and then show the form?

In the view:

// e.g. get the session.id from dynamichelper
if (data.userid === session.userdata.id) {
    // The form where user can change his data contained within here
}

The server is going to use SSL.

Thanks in advance

George

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:23:15+00:00Added an answer on May 27, 2026 at 8:23 am

    In the db query code, check for req.body.email_login and req.body.password_login to make sure they’re not null and that they’re strings. Someone could sent an empty response and that will generate an Internal Error on your side.

    Also in the route, you might want to log the error and redirect the user to the /500.html page (internal error):

    if (err) {
      console.log(error);
      res.redirect('500');
    } else ...
    

    You shouldn’t do this in the view:

    if(data.userid === session.userdata.id) { //The form where user can change his data contained within here }
    

    Try instead to achieve this in the model (preferably), make a function for it and pass only one parameter to the view like so:

    res.render('view', { loggedIn: true });
    

    The function from the model:

    function checkUser(id, session) {
      return (userid === session.userdata.id);
    }
    ...
    module.exports.checkUser = checkUser;
    

    You can call it from the route like so (for ex):

    res.render('view', { loggedIn: model.checkUser(req.body.id, req.session); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There have been some similar questions asked regarding Grid views, but none have been
I have couple questions regarding some C++ rules. Why am I able to call
I have a question regarding the some data which is being transfered from one
I came across this class while reading a C# book and have some questions.
I have some questions about Perl's map function. Specifically: How does %hash = map
I have read some topic regarding with my question but there code doesn't work
If I have a function called from a few places, and it requires some
I have recently had some input from a colleague regarding committing in a stored
I have some questions regarding MVC that I would like to clarify. At our
I have an other active question HERE regarding some hopeless memory issues that possibly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.