Home > Back-end >  Passport JS logout() function not working, not being called at all
Passport JS logout() function not working, not being called at all

Time:08-10

I am trying to implement a logout endpoint for my app using passport js. I have already setup register, login and session functionality to the app. All works fine but when I create an endpoint and call req.logout() the endpoint runs and just completely skips the passport logout() function like its not even there, doesn't even throw an error from the callback it just appears to be completely disregarding the entire block of code. I can however confirm the endpoint is definitely being hit. I've tested this with postman and the same problem persists as well.

I have button placed at the top of the page which links to a logout function that calls the login endpoint on my node backend server. Here is the frontend function

   async function logOut() {
        await axios({method: "POST", url: "http://localhost:3000/restaurants/logout", withCredentials: true}).then((response) => {
            console.log("response status", response)

            if (response.status === 200) {
                // setCount("login")
            }
        })

    }

Here is the endpoint on my backend server (when calling this endpoint the call comes through from the frontend but just completely ignores the logout method and callback written into the endpoint)

router.post("/logout", function(req, res, next) {
    req.logout(function(err) {
      if (err) {
        return next(err);
      }
      res.json("logged out");
    });

    console.log("logout called")
  });

I've tried changing req.logout() to req. logOut() I've tried tried req.session.destroy(), tried to change it from a post request to a get request and the same issue persists. Passport docs arent much use as I've implemented the solution exactly as it is described in the docs to no avail.

Hoping someone can help here :)

CodePudding user response:

use res.redirect('/');

Also it's not a good approach to use async await with .then() .catch()

router.post("/logout", function(req, res, next) {
    req.logout(function(err) {
      if (err) {
        return next(err);
      }
      res.redirect('/');
    });

    console.log("logout called")
  })

change the request like the below

async function logOut() {
    try {
        const response = await axios({ method: "POST", url: "http://localhost:3000/restaurants/logout", withCredentials: true })
        console.log(response)
    } catch (error) {
        console.log(error)
    }
}

CodePudding user response:

Ok so I've figured it out, see working code with comments

// I had to make the route asynchronous on the server side.

router.post("/logout", async function (req, res, next) {

    console.log("logout user", req.user)

    try {
//I had to include the user before the callback when calling the logout function
        req.logOut(req.user, function (err) {
            console.log("logout callback called")
            if (err) {
                console.log("error", err)
                return next(err);

            }
        

//removed the json response in the callback as for some reason the callback wont execute the code placed inside it, I can however confirm the session has been scrapped and the user logged out. I also removed the console log for "logout callback called" as it was not firing either.
        });
    } catch (e) {
        console.log(e)
    }
//added a response to the frontend with the result of an authentication check
res.json(req.isAuthenticated())
    console.log("logout called")
});

I also removed the async/await from the frontends request and just left the standard Javascript promise in.

 function logOut() {
         axios({method: "POST", url: "http://localhost:3000/restaurants/logout", withCredentials: true}).then((response) => {
            console.log("response status", response)

            if (response.status === 200) {
                // setCount("login")
            }
        })

    }
  • Related