Programmer
Programmer

Reputation: 65

Develop Node + Express applications with user authentication without having to log into an account each server change?

I am developing a website heavily embedded with user authentication (using passport). I also have nodemon reloading the server after each server change. However it is quite tedious to have to log in as a user each time to test a new change. Is there a way to avoid having to constantly log in as a user to test every single new server change? I can not find any npm modules that fake a persistent user or solve the problem. Thanks!


The Answer - If you are in a similar situation use this code.

Came back with a solution that works, so for anyone who sees this post in the future. This is how you get simple persistent user authentication. With passport. This assumes you have strategies in place already, and as just tring to make the users stay after server restarts.

var bodyParser = require('body-parser')
var mongoose = require('mongoose');
var session = require('express-session');
var cookieParser = require('cookie-parser');
const MongoStore = require('connect-mongo')(session);


app.use(session({
resave: true,
saveUninitialized: true,
secret: 'add-secret',
store: new MongoStore({
  url: process.env.MONGODB_URI || "mongodb://localhost/add-url",
  autoReconnect: true,
  clear_interval: 3600
  })
}));


app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

Upvotes: 1

Views: 37

Answers (1)

paulsm4
paulsm4

Reputation: 121599

Yup - it sounds like that's EXACTLY what you want: a "mock authentication" module (for testing purposes only).

Here are two possibilities; you might also consider writing your own:

Upvotes: 1

Related Questions