Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
@types/passport
Advanced tools
The @types/passport npm package provides TypeScript type definitions for Passport.js, a popular authentication middleware for Node.js. These type definitions allow TypeScript developers to use Passport.js in their applications with the benefits of type safety and IntelliSense in their code editors.
Authentication Middleware Initialization
This code initializes Passport as middleware in an Express application, enabling authentication functionalities.
import * as passport from 'passport';
app.use(passport.initialize());
Serialize User
This function defines how to store the user in the session storage, typically saving the user ID.
passport.serializeUser((user, done) => {
done(null, user.id);
});
Deserialize User
This function defines how to retrieve the user from the session storage, using the user ID to fetch the user record from the database.
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user);
});
});
Use of Strategy
This code sample demonstrates how to implement a local authentication strategy using Passport.js. It involves checking the username and password against stored records.
import { Strategy as LocalStrategy } from 'passport-local';
passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.verifyPassword(password)) { return done(null, false); }
return done(null, user);
});
}
));
Provides TypeScript definitions for Express session middleware, similar to how @types/passport provides definitions for Passport.js. While @types/express-session focuses on session handling, @types/passport focuses on user authentication.
Offers TypeScript definitions for the jsonwebtoken package, which is used for generating JWTs for authentication purposes. It complements @types/passport by providing types for handling JWTs, whereas @types/passport is more broadly focused on authentication strategies.
npm install --save @types/passport
This package contains type definitions for Passport (http://passportjs.org).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/passport.
These definitions were written by Horiuchi_H, Eric Naeseth, Igor Belagorudsky, Tomek Łaziuk, Daniel Perez Alvarez, Kevin Stiehl, and Oleg Vaskevich.
FAQs
TypeScript definitions for passport
The npm package @types/passport receives a total of 1,073,007 weekly downloads. As such, @types/passport popularity was classified as popular.
We found that @types/passport demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.