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

440
Views
login authorization problem, does not pass the token

I am trying to make a login system with authorization, unfortunately the token is not transferred.

const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const urlencodedParser = bodyParser.urlencoded({ extended: false });
const mysql = require('mysql');
const validator = require('validator');
const jwt = require('jsonwebtoken');
require('dotenv').config().ACCESS_TOKEN;
const ACCESS_TOKEN = process.env.ACCESS_TOKEN;


const app = express();
app.use(express.json());
const publicDirectoryPath = path.join(__dirname, '../public');
console.log(publicDirectoryPath);
app.use(express.static(publicDirectoryPath));


function generateAccessToken(username) {
return jwt.sign(username, ACCESS_TOKEN, { expiresIn: '1800s' });
}

app.post('/login', urlencodedParser, (req, res) => {

res.get(req.body.username + req.body.password);

const token = generateAccessToken({ username: req.body.username });
res.json(token);

});


function authenticateToken(req, res, next) {
   const authHeader = req.headers['authorization']
   const token = authHeader && authHeader.split(' ')[1]
   console.log(token)

if (token == null) return res.sendStatus(401)

  jwt.verify(token, ACCESS_TOKEN, (err, user) => {
  console.log(err)

  if (err) return res.sendStatus(403)

  req.user = user

  next()
})
}

app.get('/admin', authenticateToken, (req, res) => {
     res.send("admin panel");

})

const port = 3000;
app.listen(port, () => {
  console.log(`Server run: http://localhost:${port}`);
 })

wants him to be redirected to the admin panel after clicking the login button. However, I am stuck at this stage and do not know what to do next: enter image description here

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

0

You should pass the token to the next route

const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const urlencodedParser = bodyParser.urlencoded({ extended: false });
const mysql = require('mysql');
const validator = require('validator');
const jwt = require('jsonwebtoken');
require('dotenv').config().ACCESS_TOKEN;
const ACCESS_TOKEN = process.env.ACCESS_TOKEN;

const app = express();
app.use(express.json());
const publicDirectoryPath = path.join(__dirname, '../public');
console.log(publicDirectoryPath);
app.use(express.static(publicDirectoryPath));

function generateAccessToken(username) {
  return jwt.sign(username, 'ACCESS_TOKEN', { expiresIn: '1800s' });
}

app.post('/login', urlencodedParser, (req, res) => {
  res.get(req.body.username + req.body.password);

  const token = generateAccessToken({ username: req.body.username });
  res.redirect(`/admin?token=${token}`);
});

function authenticateToken(req, res, next) {
  token = req.query.token;
  if (token == null) return res.sendStatus(401);

  jwt.verify(token, 'ACCESS_TOKEN', (err, user) => {
    console.log(err);

    if (err) return res.sendStatus(403);

    req.user = user;

    next();
  });
}

app.get('/admin', authenticateToken, (req, res) => {
  res.send('admin panel');
});

const port = 3000;
app.listen(port, () => {
  console.log(`Server run: http://localhost:${port}`);
});

screenshot

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!