title: How to authenticate against my end points with passport?
body: <p>I have set up the spotify passport strategy with a mongo db to save the user profile, access and refresh tokens. But apart from the initial auth route and then the callback url, do I need to pass passport.authenticate as a callback to every route? Or am I supposed to have a custom middleware that checks if the cookie-session module still has req.user on the request? I see these as two possibilities. Which is the correct one? I know that on the cookie side, it will eventually expire and the /auth route will need to be called again.   </p>
  
  <p>I have made a middleware function that checks the user property of the request object. But am not sure how to test it. Perhaps reducing the cookie expiration time and check req.user is false.</p>
  
  <pre><code>passport.serializeUser((user, done) =&gt; {
      done(null, user.id);
  });
  
  passport.deserializeUser((id, done) =&gt; {
      User.findById(id)
          .then((user) =&gt; {
              done(null, user);
          });
  });
  
  passport.use(new SpotifyStrategy({
      clientID: keys.spotifyClientID,
      clientSecret: keys.spotifyClientSecret,
      callbackURL: '/auth/spotify/callback',
      proxy: true
  }, async (accessToken, refreshToken, profile, done) =&gt; {
  
      const spotifyId = profile.id
      const name = profile.displayName
      const email = profile.emails[0].value
  
      const existingUser = await User.findOne({ spotifyId: profile.id });
  
      if (existingUser) {
          return done(null, existingUser);
      }
  
      const user = await new User({ spotifyId, name, accessToken, refreshToken}).save();
  
      done(null, user);
  }));
  
  Check the cookie middleware:
  
  </code></pre>
  
  <pre><code>module.exports = isAuthenticated = (req, res, next) =&gt; {
  
  if (req.user) {
      return next();
  }
  
  return res.redirect('/auth/spotify');
  }
  </code></pre>
  
  <pre><code>Example route:
  
  
  </code></pre>
  
  <pre><code>module.exports = (app) =&gt; {
  
  app.get('/api/spotify/playlists', passport.authenticate('spotify'), 
    (req, res) =&gt; {
  
  console.log('got here')
  console.log(req.user);
  
   //return playlists
  });
   }
  </code></pre>
  
  <pre><code>
  </code></pre>