What is @types/passport-local?
@types/passport-local provides TypeScript type definitions for the passport-local strategy, which is a Passport.js strategy for authenticating with a username and password.
What are @types/passport-local's main functionalities?
LocalStrategy
This feature allows you to define a local authentication strategy using a username and password. The code sample demonstrates how to set up the LocalStrategy with Passport.js.
const LocalStrategy = require('passport-local').Strategy;
const passport = require('passport');
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, { message: 'Incorrect username.' }); }
if (!user.validPassword(password)) { return done(null, false, { message: 'Incorrect password.' }); }
return done(null, user);
});
}
));
Serialization and Deserialization
This feature allows you to serialize and deserialize user information to maintain authentication state across HTTP requests. The code sample shows how to implement these methods with Passport.js.
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
Middleware Integration
This feature allows you to integrate Passport.js middleware into an Express application. The code sample demonstrates how to set up session management and authentication routes.
const express = require('express');
const passport = require('passport');
const app = express();
app.use(require('express-session')({ secret: 'secret', resave: false, saveUninitialized: false }));
app.use(passport.initialize());
app.use(passport.session());
app.post('/login', passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login' }));
Other packages similar to @types/passport-local
@types/passport
@types/passport provides TypeScript type definitions for the core Passport.js library. It is essential for adding type safety to your Passport.js authentication strategies and middleware.
@types/passport-jwt
@types/passport-jwt provides TypeScript type definitions for the passport-jwt strategy, which is used for authenticating with JSON Web Tokens (JWT). It is useful for applications that use token-based authentication instead of session-based.
@types/passport-google-oauth20
@types/passport-google-oauth20 provides TypeScript type definitions for the passport-google-oauth20 strategy, which is used for authenticating with Google OAuth 2.0. It is useful for applications that want to allow users to log in using their Google accounts.
Installation
npm install --save @types/passport-local
Summary
This package contains type definitions for passport-local (https://github.com/jaredhanson/passport-local).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/passport-local.
import { Strategy as PassportStrategy } from "passport-strategy";
import express = require("express");
interface IStrategyOptions {
usernameField?: string | undefined;
passwordField?: string | undefined;
session?: boolean | undefined;
passReqToCallback?: false | undefined;
}
interface IStrategyOptionsWithRequest {
usernameField?: string | undefined;
passwordField?: string | undefined;
session?: boolean | undefined;
passReqToCallback: true;
}
interface IVerifyOptions {
message: string;
}
interface VerifyFunctionWithRequest {
(
req: express.Request,
username: string,
password: string,
done: (error: any, user?: Express.User | false, options?: IVerifyOptions) => void,
): void;
}
interface VerifyFunction {
(
username: string,
password: string,
done: (error: any, user?: Express.User | false, options?: IVerifyOptions) => void,
): void;
}
declare class Strategy extends PassportStrategy {
constructor(options: IStrategyOptionsWithRequest, verify: VerifyFunctionWithRequest);
constructor(options: IStrategyOptions, verify: VerifyFunction);
constructor(verify: VerifyFunction);
name: string;
}
Additional Details
Credits
These definitions were written by Maxime LUCE.