Mark_79 0 Newbie Poster

Hello I am needing some assistance with setting up a function to check if a user is logged in or not with Passport.js using the local and slack strategy.

I have a localstrategy which works fine but I also want to implement a sign in with slack as well.

I have added my app to slack and set the redirect URL. Everything is working fine however regardless if a user signs in or not they can still access the main page from the URL.

Example I have my login page 123.456.7.891:3000/login and when a user logs in through the local login or slack it redirects to 123.456.891:3000/index. Well if you know the URL for index you can just navigate to it.

For the local strategy I used this function to prevent that. It checks if the user is logged in or not.

    //Check if user has logged in, if they have, grant access. If not send back to the login screen.
  function isLoggedIn(req, res, next){
      return next();
  req.flash("error", "Must be signed in first to access this page.");

And than I simply add the isLoggedIn function to the route like this. Any route withthe isLoggedIn will not allow access to the route unless the user has signed in.

app.get("/QAApplicationHub", isLoggedIn, (req, res) => {
Application.find({}, (err, allApplications) => {
    }  else {
      res.render("index", { application: allApplications });
      // username:

The issue I am having with when a user logs in with slack is when they are redirected to the redirect URL it just takes them back to the login page stating that the user must be logged in to access the page. The message appears because I have flash set up to show the user the error. It seems that with my current code the isLoggedIn only checks for the local login and not slack.

So how can I implement the isLoggedIn function for both the local and slack strategy? Or what method is it that I need to implement for it to work for both.

This is my code for Passport-slack and local.


    //Local Strategy
    passport.use(new LocalStrategy(User.authenticate()));

  // Configure the Slack Strategy
  passport.use(new SlackStrategy({
    clientID: process.env.SLACK_CLIENT_ID = '123456',
    clientSecret: process.env.SLACK_CLIENT_SECRET ='123abc',
    }, (accessToken, scopes, team, extra, profiles, done) => {
    done(null, profiles.user);

//============================="/login", passport.authenticate("local", {
  successRedirect: "/QAApplicationHub",
  failureRedirect: "/login",
  failureFlash: true

app.get("/logout", (req, res) => {
  req.flash("success", "Successfuly signed out!")

// =============================
// =============================
// path to start the OAuth flow'/auth/slack', passport.authorize('slack', {
  successRedirect: "/QAApplicationHub",
  failureRedirect: "/login",
  failureFlash: true

// OAuth callback url
     (req, res) => {
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.