Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
node js backend security header

im trying to make a login endpoint/api, and i am sending the user login data through the authentication headers, is that okay for the security? the authorization headers is actually contains an object, it is encoded to a Base64, it contains user data like hashed password (not hashed yet in this code), username, and some sort of a serverkey (to authorize if it is the right client that sending an api request), just wanna make sure if it is secure or not..

const aFunction = (req, res) => {
        require('crypto').randomBytes(48, function(err, buffer) {
            const token = buffer.toString('hex');
            const auth = JSON.parse(Buffer.from(req.headers.authorization, 'base64').toString('ascii')) 
        
            if(auth.serverkey == version["serverkey"]){
                loginUser(auth.username,token).then(data =>{
                
                    if (data.rows.length < 1 || data.rows[0].password != auth.password) {
                        res.send({
                            status: "failed", 
                            message: "Username/Password is wrong", 
                            status_code: 400
                        })
                    }else{
                        res.send({
                            status: "success", 
                            message: "successfully logged in", 
                            status_code: 200, token: token
                        })

                    }

                })
            }else{
                res.send({
                    status: "failed", 
                    message: "Unauthorized Client", 
                    status_code: 401
                })
            }
        });
        
            

        
    }

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I would add a check for if (req.secure) to your code, and reject any non-https request coming your way. (Don't do this if your nodejs program is behind a reverse proxy.)

Users with browsers can see everything you send back and forth to your server, whether via https or http. (devtools) So, if by reading your headers they can figure out how to send you malicious headers, then somebody will. And base64 encoding has exactly the same security as no encoding at all. So salt and hash your passwords. Use the bcrypt package or jwts.

I would handle errors this way, by calling next() with an error parameter rather than by using .send() to deliver a failure message. Use this sort of code.

const createError = require('http-errors')
 ...
const myfunction (req, res, next) {
  ...
  if (!req.secure) return next(createError(403, 'https required'))
  ...
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!