#Using Clerk for auth without a Users collection

1 messages · Page 1 of 1 (latest)

sand violet
#

Hey there. 👋 I'm integrating Payload into a new React Typescript app that already uses clerk.js for auth.

I'd like to completely remove the Users collection and just use Clerk for my auth. It provides server-side functions for the currentUser and role and so on.

I can configure a new CollectionConfig with the right access permissions:

import type { Access, CollectionConfig } from "payload";
import { auth as fetchAuth } from "@clerk/nextjs/server";

export const isContentEditor: Access = async ({ req }) => {
  // Check if the logged-in user has content editing permissions
  const _auth = await fetchAuth();
  return _auth.has({ permission: "org:content:edit" });
};

export const Courses: CollectionConfig = {
  slug: "courses",
  access: {
    read: async ({ req }) => await isContentEditor({ req }),
    create: async ({ req }) => await isContentEditor({ req }),
etc.

The problem is the admin dashboard - payload.config.ts still seems to require the slug for a users collection:

// payload.config.ts
export default buildConfig({
  admin: {
    user: Users.slug,
  },

According to the docs, this is required.

Is there really no way to complete remove the Users collection and just use my own third party auth?

tall tuskBOT
astral oyster
#

@sand violet im looking to achieve the same thing, did you find a solution to this?

round isle
#

I’d be interested to find out more about this too.

vestal coral
#

Not sure if this answers it, but did come across it in the past

https://www.youtube.com/watch?v=egKaeOuddFA

Payload CMS and Clerk - advanced integration with Clerk webhooks and custom components tested with Playwright.

Chapters:

00:00:00 Overview
00:03:35 Setup
00:14:56 Website review
00:25:53 Integration review
00:59:25 Webhooks
01:21:26 Testing
01:34:00 New role
01:49:26 New user

Source Code - github.com/DanailMinchev/payload-clerk-example

#payl...

â–¶ Play video
astral oyster
#

@vestal coral thanks for the response! i've just checked the code in the repo linked to the video and this guy is still using the Users collection.. it would be nice to not use the Users collection by default and instead use a custom authentication solution such as better-auth (im storing users in another DB, not the payload one)

vestal coral
#

I did see this somewhere also, once again not sure if its of any use

astral glacier
#

what you certainly could do (even if it isn't fully what you intend doing), would be to have a very stripped down user collection, which basically just holds your payload userId and your clerk userId. From there, everything could be done via your clerk user.

vestal coral
#

yeah I don't think you should entirely remove the users collection, surely payload still needs to associate data/collections with it?

surreal moss
#

Hi folks, thank you for referencing the example @vestal coral , just to share my research as well:

I am using the user's collection only as a referencing point for the other collections (to reference the owners of the "posts" for example) and to store clerk's id, but I am still using Clerk as a source of truth.

  • as mentioned by @sand violet I was unable to remove the users's collection as well, but I was able to hide it using:
  admin: {
    hidden: true,
  },

in the first version of the example https://github.com/DanailMinchev/payload-clerk-example/blob/feat/part-1/src/collections/Users.ts#L11

I was thinking to implement this in a similar way with Better Auth as well, but the solution will be similar.

wild cedar
#

I am trying to do the same thing with better auth - separate schema for better auth and it just syncs user id to user tables via db triggers.

But what I can’t figure out is how to initiate a session that is recognized by the admin panel. Been going in circles for three days on it.

vestal coral
#

@wild cedar just use the payload/better-auth plugin?

wild cedar
#

Is it working? I thought it was pre production - I had trouble getting it working and just figured it wasn’t ready. If it’s working I’ll give it another try though. Anyone else using it?

vestal coral
#