Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a NodeJs application private on Heroku

I'm trying to make a NodeJS application on Heroku private so that only developers can see it. Is there a simply way to do that, like basic auth? (All of the solutions I keep finding are specific to Ruby apps).

like image 347
inspirationCloud Avatar asked Jul 24 '13 20:07

inspirationCloud


People also ask

How do I make my Heroku app private?

To create a Private Space using Dashboard, click the Spaces tab inside your team and click Create new Private Space . When creating the Private Space, you can optionally choose a region (geographic location) for your apps and data services.

Does Heroku set Node_env to production?

By default NODE_ENV is set to production .

Can you host frontend on Heroku?

You can run 2 apps (frontend and backend) on the same Heroku dyno.

How does Heroku work under the hood?

When the Heroku platform receives the application source, it initiates a build of the source application. Under the hood, a git hub webhook is triggered after pushing new Source Code to the master branch. For build and deployment Heroku uses so called buildpacks.


2 Answers

If you want to leverage basic authentication, here are two options: http-auth and Passport. http-auth is a very simple module and Passport is a powerful module with alternatives for authentication. Both modules provide code examples ranging from basic code to Express framework integration.

like image 72
mongermd Avatar answered Oct 13 '22 00:10

mongermd


I have the same problem. I managed to get one solution working that may work for you but wasn't suitable for me as it seems to interfere with the built in user login from angular-fullstack.

I just wanted a quick way to password protect the app so that only developers and stakeholders could see it. https://www.npmjs.org/package/http-auth seems to do the trick.

This involves add http-auth to your project (npm install http-auth --save). Then you'll need to find the file where your createServer is defined and the code there.

If you're using Express you can do something like this

// HTTP Authentication 
var preAuth = require('http-auth');
var basic = preAuth.basic({
        realm: "Restricted Access! Please login to proceed"
    }, function (username, password, callback) { 
         callback( (username === "user" && password === "password"));
    }
);

// Setup server
var app = express();
    app.use(preAuth.connect(basic));
var server = require('http').createServer(app);

If not then you can try one of the options from the http-auth documentation e.g.

// Authentication module.
var auth = require('http-auth');
var basic = auth.basic({
        realm: "Simon Area."
    }, function (username, password, callback) { // Custom authentication method.
        callback(username === "Tina" && password === "Bullock");
    }
);

// Creating new HTTP server.
http.createServer(basic, function(req, res) {
    res.end("Welcome to private area - " + req.user + "!");
}).listen(1337);

Here are also a couple of related threads with somewhat similar approaches.

express.basicAuth throwing error

Basic HTTP authentication in Node.JS?

like image 40
LifeOnLars Avatar answered Oct 13 '22 00:10

LifeOnLars