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){
  if(req.isAuthenticated()){
      return next();
  }
  req.flash("error", "Must be signed in first to access this page.");
  res.redirect("/login");
}

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) => {
  if(err){
      console.log(err);
    }  else {
      res.render("index", { application: allApplications });
      // username: req.user.name
    }
   });
});     

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.

    app.use(passport.initialize());
    app.use(passport.session());

    //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);
  }));

 //=============================
//LOCAL LOGIN MIDDLEWARE
//=============================
app.post("/login", passport.authenticate("local", {
  successRedirect: "/QAApplicationHub",
  failureRedirect: "/login",
  failureFlash: true
}));

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

// =============================
// PASSPORT-SLACK MIDDLEWARE
// =============================
// path to start the OAuth flow
app.post('/auth/slack', passport.authorize('slack', {
  successRedirect: "/QAApplicationHub",
  failureRedirect: "/login",
  failureFlash: true
}));

// OAuth callback url
 app.get('/auth/slack/callback',
   passport.authorize('slack',
     (req, res) => {
     res.redirect('/QAApplicationHub')
   }));
Be a part of the DaniWeb community

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