Can't get custom Passport strategy to work

default discord avatar
andersmattson
2 weeks ago

Ok, I might have totally misunderstood how the authentication in Payload and Passport works but I'm trying to implement a custom strategy using

https://www.passportjs.org/packages/passport-custom/

.



My somewhat simplified code looks like this:



export const usersCollection: CollectionConfig = {
  slug: "users",
  auth: {
    disableLocalStrategy: true,
    strategies: [{
      name: 'users-custom'
      strategy: new CustomStrategy(( req, callback ) => {
        let user;
        // Do stuff here to find out which user is requesting and populate user
        callback(null, user);
      });
    }],
  },
  admin: {
    useAsTitle: "email",
  },
  access: {
    read: ({ req }) => {
      if (req.user && req.user.groups && req.user.groups.includes("Admin")) {
        return true;
      }
      if (req.user) {
        return {
          email: req.user.email,
        }
      }
      return false;
    },
    admin: ({ req }) => {
      return req.user && req.user.groups && req.user.groups.includes("Admin");
    },
  },
  ...
};


This works fine when accessing api endpoints but for some reason it does not work when trying to access the Payload admin UI.



My understanding was that the custom strategy is used for all authentication in Payload, even the Admin UI? No matter what I do, after logging in the user is redirected back to the login screen even though the Payload JWT cookie is set.

    Open the post
    Continue the discussion in Discord
    Like what we're doing?
    Star us on GitHub!

    Star

    Connect with the Payload Community on Discord

    Discord

    online

    Can't find what you're looking for?

    Get help straight from the Payload team with an Enterprise License.