DEV Community

Taylor Beseda
Taylor Beseda

Posted on

4

Express.js Basic Auth: Get it out the door!

Sometimes you need to roll out a prototype before it even has an authentication layer. Here's how to require a username and password for your application.

Important!

If it isn't obvious, this should be TEMPORARY!. A proper authentication layer should be added and sensitive data, like passwords, shouldn't be a part of an application's codebase. With that said...

Express Basic Auth

I'm going to assume some Node.js basics and that you're working with Express.

First, install the express-basic-auth dependency: npm i express-basic-auth.

Require the auth package where you create your app

const app = require('express')();
const basicAuth = require('express-basic-auth');

and tell Express your user(s)' credentials

app.use(basicAuth({
    users: { 'username': 'password' },
    challenge: true,
}));

The challenge option tells the browser to open a prompt so the user doesn't need to enter a URL prefix like username:password@....
Also, you can add several users to the users object.

Simple! Now ship it.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay