client: import type { Workspace } from '@/types/types' import { useAuthQuery } from 'better-auth/client' import type { BetterAuthClientPlugin } from 'better-auth/client' import { atom } from 'nanostores' import type { workspace } from './plugin' export const workspaceClient = () => { // Create atoms for signal tracking const $listWorkspacesSignal = atom(false) const $activeWorkspaceSignal = atom(false) return { id: 'workspace', $InferServerPlugin: {} as ReturnType, getAtoms: ($fetch) => { // Create a query hook for listing workspaces const listWorkspaces = useAuthQuery( $listWorkspacesSignal, '/workspace/list', $fetch, { method: 'GET', }, ) // Create a query hook for getting the active workspace const activeWorkspace = useAuthQuery( $activeWorkspaceSignal, '/workspace/get-active', $fetch, { method: 'GET', }, ) return { $listWorkspacesSignal, $activeWorkspaceSignal, listWorkspaces, activeWorkspace, } }, pathMethods: { '/workspace/list': 'GET', '/workspace/get-active': 'GET', '/workspace/set-active': 'POST', '/workspace/create': 'POST', '/workspace/update': 'POST', '/workspace/delete': 'POST', '/workspace/get-full': 'GET', }, atomListeners: [ { matcher(path) { return ( path === '/workspace/create' || path === '/workspace/delete' || path === '/workspace/update' ) }, signal: '$listWorkspacesSignal', }, { matcher(path) { return path.startsWith('/workspace') }, signal: '$activeWorkspaceSignal', }, { matcher(path) { return path === '/workspace/set-active' }, signal: '$sessionSignal', }, ], } satisfies BetterAuthClientPlugin } plugin: import type { BetterAuthPlugin } from 'better-auth' import { createWorkspace, deleteWorkspace, getFullWorkspace, listWorkspaces, setActiveWorkspace, updateWorkspace, } from './crud' export const workspace = () => { return { id: 'workspace', endpoints: { createWorkspace, updateWorkspace, setActiveWorkspace, getFullWorkspace, deleteWorkspace, listWorkspaces, }, schema: { session: { fields: { activeWorkspaceId: { type: 'string', required: false, }, }, }, workspace: { fields: { name: { type: 'string', required: true, sortable: true, }, slug: { type: 'string', unique: true, sortable: true, }, createdAt: { type: 'date', required: true, }, updatedAt: { type: 'date', required: true, }, deletedAt: { type: 'date', required: true, }, }, }, }, } satisfies BetterAuthPlugin }