I have node.js package which uses rest api in ubuntu. When I login, this server (node package) gives me a token in json body. So when I want to do something (any request) in server I am using this token. But there is an error when I try to compare the token server gave with login and the token that client send to server.
The code to get token from the server:
router.post('/login',(req,res)=>{
const name = req.body.name;
const password = req.body.password;
console.log("Login request by "+name+" with password: "+password);
if(name == "asdasd" && password == "asdadsad"){
console.log("Auth succesful");
const token = jwt.sign(
{
user: name,
password: password
},
process.env.JWT_KEY,
{
expiresIn: "1h"
}
)
return res.status(200).json({
message:"Auth succesful",
token: token
});
}else {
return res.status(401).json({
message:"Auth failed :("
})
}
})
The code to make request with the given token:
router.post('/add',checkAuth,(req,res,next)=>{
console.log('haha');
const {
fname, mname, lname, birthdate, created, updated, sex, place, title_id
} = req.body
pool.query(`INSERT INTO users (
fname, mname, lname, birthdate, created, updated, sex, place, title_id
) VALUES ($1, $2,$3, $4,$5, $6,$7, $8,$9)`, [
fname, mname, lname, birthdate, created, updated, sex, place, title_id
], (error, result) => {
if (error) {throw error}
res.status(201).send(`User added with ID: ${result.insertId}`)
})
})
The code mentioned as checkAuth above:
const jwt = require('jsonwebtoken')
module.exports = (req,res,next) =>{
try{
const decoded = jwt.verify(req.headers.authorization, process.env.JWT_KEY);
req.userData = decoded;
next();
}catch (error) {
return res.status(401).json({
message: "Auth failed"
});
}
}
nodemon.json file:
{
"env": {
"JWT_KEY": "secret"
}
}
This is the json response from the server I got when I connect to server with postman or with android studio:
{
"message": "Auth failed"
}
This is the result of consol.log(process.env);
Object {ALLUSERSPROFILE: "C:\ProgramData", AMD_ENTRYPOINT: "vs/workbench/services/extensions/node/extensionHos…", APPDATA: "C:\Users\aydogan\AppData\Roaming", APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL: "true", CommonProgramFiles: "C:\Program Files\Common Files", …}
check-auth.js:6
[[StableObjectId]]:2
ALLUSERSPROFILE:"C:\ProgramData"
AMD_ENTRYPOINT:"vs/workbench/services/extensions/node/extensionHostProcess"
APPDATA:"C:\Users\aydogan\AppData\Roaming"
APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL:"true"
CommonProgramFiles:"C:\Program Files\Common Files"
CommonProgramFiles(x86):"C:\Program Files (x86)\Common Files"
CommonProgramW6432:"C:\Program Files\Common Files"
COMPUTERNAME:"AYDOGAN"
ComSpec:"C:\WINDOWS\system32\cmd.exe"
DriverData:"C:\Windows\System32\Drivers\DriverData"
FPS_BROWSER_APP_PROFILE_STRING:"Internet Explorer"
FPS_BROWSER_USER_PROFILE_STRING:"Default"
HOMEDRIVE:"C:"
HOMEPATH:"\Users\aydogan"
IntelliJ IDEA:"C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.5\bin;"
LOCALAPPDATA:"C:\Users\aydogan\AppData\Local"
LOGONSERVER:"\\AYDOGAN"
NUMBER_OF_PROCESSORS:"8"
OneDrive:"C:\Users\aydogan\OneDrive"
OneDriveConsumer:"C:\Users\aydogan\OneDrive"
JWT_KEY seems undefined, I can see it with debug.
I appreciate any help
nodemon.json