Skip to content

How to put a password on peerflix server using basic auth

DarkVenusNet edited this page Jan 8, 2018 · 11 revisions

How to put a password on peerflix server using basic-auth

This is a guide for putting a password on peerflix-server without using squit or Nginx.

Tested on Ubuntu 16.04

requirements

You need to install basic-auth and nano

npm install basic-auth

apt-get -y install nano

Close peerflix-server if it is running

  • if you have started peerflix-server with the command peerflix-server then click Ctrl + C

or

  • if you are running peerflix-server with forever forever stop /usr/bin/peerflix-server

Adding Password

Open the file index.js with nano

nano /usr/lib/node_modules/peerflix-server/server/index.js

and replace

  1. api.use(function (req, res, next) {
  2. res.header('Access-Control-Allow-Origin', '*');
  3. res.header('Access-Control-Allow-Methods', 'OPTIONS, POST, GET, PUT, DELETE');
  4. res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');

to

  1. var basicAuth = require('basic-auth');
  2. var dvnuser = 'your_user';
  3. var dvnpass = 'your_pass';
  4. api.use(function (req, res, next) {
  5. function unauthorized(res) {
  6. res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
  7. eturn res.send(401);
  8. };
  9. var user = basicAuth(req);
  10. if (!user || !user.name || !user.pass) {
  11. return unauthorized(res);
  12. };
  13. if (user.name === dvnuser && user.pass === dvnpass) {
  14. res.header('Access-Control-Allow-Origin', '*');
  15. res.header('Access-Control-Allow-Methods', 'OPTIONS, POST, GET, PUT, DELETE');
  16. res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  17. return next();
  18. } else {
  19. return unauthorized(res);
  20. };

P.S.: replace your_ user and your_pass with your desidered username and password

Click Ctrl + X then click two times enter to exit

Restarting peerflix-server

  • You can start peerflix-server normally with peerflix-server command

or

  • You can start peerflix-server using forever forever start $(which peerflix-server)
Clone this wiki locally