Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

jaredhanson/passport

Repository files navigation

passport banner

Passport

Passport isExpress-compatible authenticationmiddleware forNode.js.

Passport's sole purpose is to authenticate requests, which it does through anextensible set of plugins known asstrategies. Passport does not mountroutes or assume any particular database schema, which maximizes flexibility andallows application-level decisions to be made by the developer. The API issimple: you provide Passport a request to authenticate, and Passport provideshooks for controlling what occurs when authentication succeeds or fails.


Sponsors

Simple Authentication
Make login our problem. Not yours.

Auth0 by Okta provides a simple and customizable login page to authenticate your users. You can dynamically add new capabilities to it - including social login, multi-factor authentication, or passkeys - without making changes to your app’s code.

We help protect your app and your users from attacks - defending your application from bot attacks and detecting runtime anomalies based on suspicious IPs, breached credentials, user context, and more.






Status:BuildCoverageDependencies

Install

$ npm install passport

Usage

Strategies

Passport uses the concept of strategies to authenticate requests. Strategiescan range from verifying username and password credentials, delegatedauthentication usingOAuth (for example, viaFacebookorTwitter), or federated authentication usingOpenID.

Before authenticating requests, the strategy (or strategies) used by anapplication must be configured.

passport.use(newLocalStrategy(function(username,password,done){User.findOne({username:username},function(err,user){if(err){returndone(err);}if(!user){returndone(null,false);}if(!user.verifyPassword(password)){returndone(null,false);}returndone(null,user);});}));

There are 480+ strategies. Find the ones you want at:passportjs.org

Sessions

Passport will maintain persistent login sessions. In order for persistentsessions to work, the authenticated user must be serialized to the session, anddeserialized when subsequent requests are made.

Passport does not impose any restrictions on how your user records are stored.Instead, you provide functions to Passport which implements the necessaryserialization and deserialization logic. In a typical application, this will beas simple as serializing the user ID, and finding the user by ID whendeserializing.

passport.serializeUser(function(user,done){done(null,user.id);});passport.deserializeUser(function(id,done){User.findById(id,function(err,user){done(err,user);});});

Middleware

To use Passport in anExpress orConnect-based application, configure itwith the requiredpassport.initialize() middleware. If your application usespersistent login sessions (recommended, but not required),passport.session()middleware must also be used.

varapp=express();app.use(require('serve-static')(__dirname+'/../../public'));app.use(require('cookie-parser')());app.use(require('body-parser').urlencoded({extended:true}));app.use(require('express-session')({secret:'keyboard cat',resave:true,saveUninitialized:true}));app.use(passport.initialize());app.use(passport.session());

Authenticate Requests

Passport provides anauthenticate() function, which is used as routemiddleware to authenticate requests.

app.post('/login',passport.authenticate('local',{failureRedirect:'/login'}),function(req,res){res.redirect('/');});

Strategies

Passport has a comprehensive set ofover 480 authentication strategiescovering social networking, enterprise integration, API services, and more.

Search all strategies

There is aStrategy Search atpassportjs.org

The following table lists commonly used strategies:

StrategyProtocolDeveloper
LocalHTML formJared Hanson
OpenIDOpenIDJared Hanson
BrowserIDBrowserIDJared Hanson
FacebookOAuth 2.0Jared Hanson
GoogleOpenIDJared Hanson
GoogleOAuth / OAuth 2.0Jared Hanson
TwitterOAuthJared Hanson
Azure Active DirectoryOAuth 2.0 / OpenID / SAMLAzure

Examples

Related Modules

Themodules page on thewiki lists other useful modulesthat build upon or integrate with Passport.

License

The MIT License

Copyright (c) 2011-2021 Jared Hanson <https://www.jaredhanson.me/>


[8]ページ先頭

©2009-2025 Movatter.jp