title: Passport.js on Cloud Functions for Firebase gives error "Cannot read property 'apply' of undefined"
body: <p>I have been trying to integrate Passport.js Gitlab oAuth on Firebase cloud functions for the passed couple of days. I'm getting an error that is difficult to debug. Can you please help me understand what is actually failing and how can I fix it ?</p>
  
  <pre><code>Error : 
  TypeError: Cannot read property 'apply' of undefined
  at Immediate.&lt;anonymous&gt; (/user_code/node_modules/express/lib/router/index.js:635:14)
  at runCallback (timers.js:639:20)
  at tryOnImmediate (timers.js:610:5)
  at processImmediate [as _immediateCallback] (timers.js:582:5)
  </code></pre>
  
  <p>This is the entire function. The passport library being used is <a href="https://github.com/gtanner/passport-gitlab" rel="nofollow noreferrer">https://github.com/gtanner/passport-gitlab</a></p>
  
  <pre><code>var functions = require('firebase-functions');
  // Firebase Setup
  const admin = require('firebase-admin');
  const passport = require('passport');
  
  const session = require('express-session');
  
  const express = require('express');
  const cors = require('cors')({
      origin: true
  });
  
  
  const router = new express.Router();
  router.use(cors);
  
  
  
  const FirebaseStore = require('connect-session-firebase')(session);
  
  const GitLabStrategy = require('passport-gitlab').Strategy;
  
  
  const serviceAccount = require('./service-account.json');
  admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: `https://${process.env.GCLOUD_PROJECT}.firebaseio.com`
  });
  
  
  
  
  router.use(session({
      store: new FirebaseStore({
          database: admin.database(),
      }),
      secret: 'myapp',
      resave: false,
      saveUninitialized: true
  }));
  router.use(passport.initialize());
  router.use(passport.session());
  
  
  
  passport.serializeUser(function(user, done) {
      console.log('serialize', user);
      done(null, user);
  });
  passport.deserializeUser(function(obj, done) {
      console.log('deserialize', obj);
      done(null, obj);
  });
  
  
  
  
  passport.use(new GitLabStrategy({
          clientID: '',
          clientSecret: '',
          callbackURL: "https://us-central1-auth-fa8a7.asd.net/api/gitlabCallback"
      },
      function(accessToken, refreshToken, profile, cb) {
          console.log('accessToken ', accessToken)
          console.log('refreshToken ', refreshToken)
          console.log('refreshToken ', profile)
  
          cb(null, profile);
      }
  ));
  
  
  
  
  router.get('/gitlab', passport.authenticate('gitlab'));
  router.get('/gitlabCallback', passport.authenticate('gitlab', {
      successRedirect: '/',
      failureRedirect: '/login'
  }));
  
  
  
  exports.api = functions.https.onRequest(router);
  </code></pre>