Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Can't get custom Passport strategy to work

default discord avatar
andersmattsonlast year
2

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.

  • default discord avatar
    lakshay00094last year

    Hey Did you end up finding a solution for this?



    @704795530345054311

    Hey, I am facing the same issue, could you help me understand what the issue was

  • default discord avatar
    aayush214last year
    @704795530345054311

    Hey how did you manage to solve this? I am also getting an error on the admin saying: "LocalStrategy is not a constructor"

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.