joas8211 / payload-tenancy

Multi-tenancy plugin for Payload CMS
MIT License
122 stars 6 forks source link

Error when creating first user #17

Closed IAmNatch closed 11 months ago

IAmNatch commented 11 months ago

Hi there,

I've added this package to my payload, and configured it as specified, however I'm running into an error when logging in to my account.

If I try to create the account programmatically, I get this error on log-in. If I try to create the account via the payload CMS first-user sign up, I get the same error.

Error:

TypeError: Cannot read properties of undefined (reading 'id')
    at eval (webpack-internal:///(api)/./node_modules/payload-tenancy/dist/utils/defaultAccess.js:170:60)
    at step (webpack-internal:///(api)/./node_modules/payload-tenancy/dist/utils/defaultAccess.js:107:23)
    at Object.eval [as next] (webpack-internal:///(api)/./node_modules/payload-tenancy/dist/utils/defaultAccess.js:48:20)
    at fulfilled (webpack-internal:///(api)/./node_modules/payload-tenancy/dist/utils/defaultAccess.js:11:32)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Payload.config

collections: [Users, Tenants, Pages, Posts, Category, Media],
 admin: {
    user: Users.slug,
 },
...
tenancy({ isolationStrategy: "domain" }),
...

Users Collection:

export const Users: CollectionConfig = {
  slug: "users",
  auth: true,
  admin: {
    useAsTitle: "email",
  },
  fields: [
    {
      name: "firstName",
      type: "text",
    },
    {
      name: "lastName",
      type: "text",
    },
  ],
};

Tenants Collection:

export const Tenants: CollectionConfig = {
  slug: "tenants",
  admin: {
    useAsTitle: "name",
  },
  fields: [
    {
      type: "text",
      name: "name",
      label: "Name",
      required: true,
    },
  ],
};

Seed Script:

 const myTenant = await payload.create({
      collection: "tenants",
      data: {
        slug: "my-company",
        domains: [{ domain: "french-gossip.com" }],
        name: "French Gossip",
      },
    });

 await payload.create({
     collection: "users",
      data: {
          firstName: "Nachi",
          lastName: "Robbins",
          password: "password",
          email: "nachirobbins@gmail.com",
          roles: ["super-admin"],
          tenant: myTenant.id,
       },
   });

Any suggestions would be super appreciated!

IAmNatch commented 11 months ago

I realized for the manual method that I needed to be using user strategy in order to create the account successfully. However, when I try to add a tenant, I'm not seeing the slug field on the UI, despite getting a missing slug validation on save.

To me, this seems like either a compatibility issue with my payload version 1.11.2, or some issue with my config..

IAmNatch commented 11 months ago

As expected, this was my fault. This plugin was used in a client driven app, and was conditionally added on the frontend. This meant that the UI was not treating the plugin as installed.