Verify JWT Token error 'secretOrPrivateKey must have a value' - jwt

return failure(new Error('secretOrPrivateKey must have a value'));enter image description here

I think your application is not able to read the environnement variables.
I advice you to use the package dotenv : https://github.com/motdotla/dotenv
After installing it you have to import it at the top of your application like this:
require("dotenv").config();
Specify a file .env at the root of your application and add your variables
JWT_SECRET=12bob12ou2b1ob
NODE_ENV=development
After, you can use the variables like you did in your linked image:
const jwtSecret = process.env.JWT_SECRET

Related

Loopback 4: Specify environment var in the #model

I am trying to get the mongodb collection name from an environment variable defined in .env. It looks like the model is initialised before the .env is read.
#model({
name: 'MyType',
settings: {
mongodb: process.env.COLLECTION_NAME,
},
})
The process.env.COLLECTION_NAME is, at this point, undefined
Any way to externalised the collection name?
Many thanks for the help.
By default, Node.js does not read from .env.
To read from .env, we need to utilize the dotenv package.
Since models are resolved through the Booter during startup, hence we need to call dotenv.config() before app.boot().
Update index.ts as follows:
// At the top of the file
import dotenv from 'dotenv';
...
// Inside the main function
// dotenv.config() must appear before app.boot()
dotenv.config()
...
app.boot();
app.start();
This would cause dotenv to overwrite process.env with the values in .env where necessary.
Further reading:
https://www.npmjs.com/package/dotenv
How do I setup the dotenv file in Node.js?
in your
src/index.ts
import {ApplicationConfig, MyApplication} from './application';
require('dotenv').config();
export * from './application';
and your .env file in the "root" of your project

Scala Config: Include another file.conf

Currently, I have a resources/application.conf file which has the following keys:
development {
server = www.myapp.com
aws_key = my_aws_key
aws_secret = my_aws_secret
}
I would like to remove my aws_key and aws_secret from the file.
I was thinking of creating another file called resources/credentials.conf and storing my personal authentications there.
credentials {
aws_key = my_aws_key
aws_secret = my_aws_secret
}
and then include it some way in my application.conf or merge that config to the Config object in addition to application.conf.
credentials.conf would be git ignored. A sample file would be checked in credentials-sample.conf which each developer would change according to his own credentials and rename the sample file to credentials.conf
I tried different variation of include like
include "credentials"
include "credentials.conf"
include "./credentials.conf"
include file("./credentials.conf")
and so on.
I know I can pass it via system variables but I would like to try it like mentioned above. If you know of a better way, please let me know.
Typesafe Config provide the way to fallback from one configuration to another. You can try the ConfigFactory.Load() to load different config and use withFallback to line them up in your designated order. Such as:
ConfigFactory.Load("credentials.conf") withFallback ConfigFactory.Load("application.conf")
Inside your conf file, add
include "another_file.conf"
ie: https://github.com/cicco94/scala-akka-slick-demo/blob/master/src/main/resources/application.conf

sails.js Is it possible to set process.env variables from application?

I am trying to use npm module fb to use facebook api. The config file is located in the module and here is the snapshot of the same
var config = { };
// should end in /
config.rootUrl = process.env.ROOT_URL || 'http://localhost:3000/';
config.facebook = {
appId: process.env.FACEBOOK_APPID || '130243393813697',
appSecret: process.env.FACEBOOK_APPSECRET || 'c82696768ae4ad8b63db874cb64eb558',
appNamespace: process.env.FACEBOOK_APPNAMESPACE || 'nodescrumptious',
redirectUri: process.env.FACEBOOK_REDIRECTURI || config.rootUrl + 'login/callback'
};
module.exports = config;
I don't wish to change the config file of the module since node_modules folder is kept in the gitignore list. For configuring the module to use my app's appId and appSecret, I need to set the process.env variables FACEBOOK_APPID and FACEBOOK_APPSECRET
I understand that it can be done while calling the sails lift but is it by any means possible to set these values inside the app so that I only have to call
sails lift
without any of those variables and those should be set automatically ? Or what is the best way to achieve what I am trying to do here ?
You should set the environment-variables outside of your App.
Instead of sails lift you could also use node app.js. With that you can define environment-variables for your node-application with:
$> FOO='bar' node app.js
In your case:
$> FACEBOOK_APPID='232322a22' FACEBOOK_APPSECRET='mysecrete' node app.js
If you want to set this vars in your app (I wouldn't suggest that!) you could set them before including your npm-module. For example in your config/bootstrap.js:
module.exports.bootstrap = function(cb) {
process.env.FACEBOOK_APPID = "myvar";
process.env.FACEBOOK_APPSECRET = "mysecrete";
sails.facebook = require("yourmodule");
cb();
};
Here is a good link for setting environment variables when "Production", "Staging" and "Development" in Sails feels fragmented.
Here is a quick video tutorial which simply explains how to create environment variables in node.js
Step1:
Install the package dotenv as dependency
$ npm install dotenv --save
Step2:
Then require dotenv in app.js and load the .env file
var dotenv = require('dotenv');
dotenv.load();
Step3:
Create a .env file in the root folder (ie. at the same document level as app.js file)
Step4:
Add your environment variables
S3_KEY=enfiownqefniqofewqofnieqwvewlk
S3_SECRET=123456789
Now you can access those environment variables anywhere in the app (probably in some config/ files):
return {
key: process.env.S3_KEY,
secret: process.env.S3_SECRET,
region: 's3-eu-west-1',
bucket: 'myBucket',
s3params: {
ACL: 'public-read'
}
}
Credit goes to Noah Bass for providing this tutorial.

Simple auth addon seems to not be reading env config

I am following the example here, and I have this in my config/environment.js file:
ENV['simple-auth'] = {
authorizer: 'simple-auth-authorizer:devise',
routeAfterAuthentication: 'landing-pages'
};
However, after my app authenticates it tries to go to the index route. I confirmed the configuration variable used had index as the routeAfterAuthentication property by adding a breakpoint in the sessionAuthenticationSucceeded method of the library.
I tried importing the configuration in the environment.js file ES6-style, but that doesn't seem possible.
Ember Simple Auth actually still relies on the window.ENV configuration variable, so you'll need to add it to your configuration. Do it like this:
window.MyAppENV = {{ENV}};
+ window.ENV = window.MyAppENV;
window.EmberENV = window.MyAppENV.EmberENV;
When used with the Ember CLI Simple Auth addon, Ember Simple Auth uses the ENV['simple-auth'] configuration set in config/environment.js like below:
...
var ENV = {
...
};
ENV['simple-auth'] = {
routeAfterAuthentication: 'some.route.name.you.choose'
};
...

Why do i use custom Variable Environment in a hosting website?

I am new to hosting world (cloudcontrol), an i got some problem with application credentials, like database administration (mongohq), or google authentification.
So, will i put those variable with some kind of syntaxte (something like $variable) in the code, and then make a commandline with key-value as variable-value ?
If you are using Tornado, it makes it even simpler. Use tornado.options and pass environment variables while running the code.
Use following in your Tornado code:
define("mysql_host", default="127.0.0.1:3306", help="Main user DB")
define("google_oauth_key", help="Client key for Google Oauth")
Then you can access the these values in your rest of your code as:
options.mysql_host
options.google_oauth_key
When you are running your Tornado script, pass the environment variables:
python main.py --mysql_host=$MYSQL_HOST --google_oauth_key=$OAUTH_KEY
assuming both $MYSQL_HOST and $OAUTH_KEY are environment variables. Let me know if you need a full working example or any further help.
example:
First set a environment variable:
$export mongo_uri_env=mongodb://alien:12345#kahana.mongohq.com:10067/essog
and make changes in your Tornado code:
define("mongo_uri", default="127.0.0.1:28017", help="MongoDB URI")
...
...
uri = options.mongo_uri
and you would run your code as
python main.py --mongo_uri=$mongo_uri_env
If you don't want to pass it while running, then you have to read that environment variable directly in your script. For that
import os
...
...
uri = os.environ['mongo_uri_env']