Search code examples
environment-variablesjwtcreate-react-app

JWT(jsonwebtoken) sign doesn't work with environment variables


In Create-React-App, I create token with environment variable(using package env-cmd); however, I don't produce valid token.

I try to compose token with environment variable which is named after REACT_APP_API_KEY. If I write value of environment variable in method jwt.sign as a string, then valid token is produced. Although I log REACT_APP_API_KEY to console, valid token is not produced within jwt sign.

import jwt from 'jsonwebtoken';

export function tokenizeValues(token) {
console.log("Env. Val: ", process.env.REACT_APP_API_KEY); //For example, xyz

//Following code doesn't work

return jwt.sign({email:"[email protected]", password:"abc123"}, process.env.REACT_APP_API_KEY); //doesn't work

//But this code is running correctly

return jwt.sign({email:"[email protected]", password:"abc123"}, "xyz"); //works
}

Solution

  • I solved the issue. My API Secret key contains special characters like sign $. Therefore, I got message invalid authentication.