Errors after a successful login from Azure Active Directory in passport. Error: passport.initialize() middleware not in use


I am using passport for Azure Active Directory login, the flow from my application to Azure Active Directory is working fine and the login at Azure Active Directory also successful. But when Azure Active Directory sending the redirect to my server backend it's failing with following error

Error: passport.initialize() middleware not in use
    at IncomingMessage.req.login.req.logIn (/Users/experimental/githome/ad-example/node_modules/passport/lib/http/request.js:46:34)
    at Strategy.strategy.success (/Users/experimental/githome/ad-example/node_modules/passport/lib/middleware/authenticate.js:235:13)
    at verified (/Users/experimental/githome/ad-example/node_modules/passport-oauth2/lib/strategy.js:186:20)
    at module.exports (/Users/experimental/githome/ad-example/passport/passport-adfs.js:21:10)
    at Strategy.passport.use.AzureOAuth2Strategy [as _verify] (/Users/experimental/githome/ad-example/passport/passport.js:27:9)
    at /Users/experimental/githome/ad-example/node_modules/passport-oauth2/lib/strategy.js:200:24
    at Strategy.userProfile (/Users/experimental/githome/ad-example/node_modules/passport-azure-ad-oauth2/lib/passport-azure-ad-oauth2/strategy.js:100:3)
    at loadIt (/Users/experimental/githome/ad-example/node_modules/passport-oauth2/lib/strategy.js:374:17)
    at Strategy.OAuth2Strategy._loadUserProfile (/Users/experimental/githome/ad-example/node_modules/passport-oauth2/lib/strategy.js:389:25)
    at /Users/experimental/githome/ad-example/node_modules/passport-oauth2/lib/strategy.js:177:16
    at /Users/experimental/githome/ad-example/node_modules/oauth/lib/oauth2.js:209:7
    at passBackControl (/Users/experimental/githome/ad-example/node_modules/oauth/lib/oauth2.js:134:9)
    at IncomingMessage.<anonymous> (/Users/experimental/githome/ad-example/node_modules/oauth/lib/oauth2.js:157:7)
    at IncomingMessage.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1094:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)

1 Answer

5 years ago by

Looks like you forgot to add passport's initialize() middleware. Add it using the following code and retry

var app = express();


app.use(passport.initialize())
app.use(passport.session())
5 years ago by Karthik Divi