Skip to content
tezvyn:

Passport.js: The Local Strategy for Username/Password Auth

Source: passportjs.orgMediumHow cards are made

Passport.js: The Local Strategy for Username/Password Auth

Passport's Local Strategy is the bouncer for traditional username/password logins in Node.js. You provide the logic to verify credentials against your database, and Passport handles the session management.

Why it exists

Passport.js provides a modular way to handle authentication in Node.js. The Local Strategy is the specific plugin for the most common case: authenticating a user with a username and password that you store and manage yourself, as opposed to delegating to a social login provider.

The mental model

Think of Passport as an authentication framework, not a turnkey solution. It uses 'strategies' for different auth methods. The Local Strategy is for credentials stored 'locally' in your app's database. Your job is to write one piece of logic—a 'verify' function—and Passport handles plugging it into the Express request/response cycle, managing sessions, and protecting routes.

How it works

First, you instantiate LocalStrategy and give it a custom verify function. When a user submits a login form, Passport passes the username and password to this function. Your code is then responsible for finding the user in your database. If the user is found, you must hash the submitted password (using the user's stored salt) and compare it to the hashed password stored in your database. This comparison must be done with a timing-safe function to prevent attackers from guessing passwords based on response times. Finally, you call a callback with the user object on success, or with false on failure. This entire flow is triggered by adding passport.authenticate('local') as middleware to your login route.

When to use it

Use the Local Strategy whenever your application is the source of truth for user accounts. It's the standard for any Express app with its own user registration and login forms, giving you full control over the database schema and password security practices like hashing and salting.

When not to use it

Do not use the Local Strategy for third-party authentication. For social logins (Google, Facebook), enterprise SSO (SAML), or other federated identity protocols (OpenID Connect), you must use a different, purpose-built Passport strategy, such as passport-google-oauth20.

One canonical example

The core of the strategy is the verify function you provide. It receives credentials, looks up the user, and securely checks the password. For example: function verify(username, password, cb) { findUserByUsername(username, (err, user) => { if (err || !user) { return cb(null, false, { message: 'Incorrect username.' }); } checkPassword(password, user.hashed_password, user.salt, (err, isMatch) => { if (err || !isMatch) { return cb(null, false, { message: 'Incorrect password.' }); } return cb(null, user); }); }); } This function is then registered with Passport: passport.use(new LocalStrategy(verify));

Interview question

What is the primary responsibility of a developer when integrating Passport.js Local Strategy?

  • a.Providing a 'verify' function to authenticate user credentials against the application's database.Correct
  • b.Configuring the integration with external identity providers like Google or Facebook.
  • c.Managing the entire session lifecycle, including cookie creation and destruction.
  • d.Designing the user interface for login forms and handling client-side input validation.
Why?

The card explicitly states, "Your job is to write one piece of logic—a 'verify' function—and Passport handles plugging it into the Express request/response cycle, managing sessions, and protecting routes." This function is where the developer implements the logic to find the user and compare passwords against their own database. Passport.js itself handles session management, making option C incorrect as the primary responsibility for the developer within the strategy.

Just read this? Test yourself on what you have been reading.

Read the original → passportjs.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on nodejs — each one lists the topics its interview covers.

See open roles