Equires WIX_EVENTS.READ_DRAFT_EVENTS permission” even when logged in as Site Owner (Wix Events API / Velo Backend)

I’m having trouble with
A permission error in my Wix Velo site when trying to query draft events from the backend.

Even though I’m logged in using the site owner’s email (the same account I use to edit the site in the Wix Dashboard), the backend call fails with this error:

{
  "success": false,
  "error": "message: Requires WIX_EVENTS.READ_DRAFT_EVENTS permission; MemberIdentity id='f5a852dc-390d-4e28-a11d-e5fc2f16d9b7'
  details:
    applicationError:
      description: Requires WIX_EVENTS.READ_DRAFT_EVENTS permission; MemberIdentity id='f5a852dc-390d-4e28-a11d-e5fc2f16d9b7'
      code: PERMISSION_DENIED
      data:
        context:
          instanceId: 3e5ce0e4-4c02-4704-95b3-978f343cc8e7
          siteMemberId: f5a852dc-390d-4e28-a11d-e5fc2f16d9b7
        identities:
          - person:
              type: SITEMEMBER
              hasUserRole: true

Working in
Wix Editor

Site link
Link: https://www.ezdestinations.com/blank-5?rc=test-site

What I’ve tried so far
I have tried using the elevated permission within the backend code both the .jsw and .js file. And tried with different account with the admin access to the wix project. Also those accounts are the site members with login credentials created within the site.

Code:

//adminEvents.js
import { wixEventsV2 } from 'wix-events.v2';
import { elevate } from 'wix-auth';


export async function getAllEventsElevated() {
      return await elevate(async () => {
    let allEvents = [];
    let offset = 0;
    const batchSize = 100;

    while (true) {
      const query = await wixEventsV2.queryEvents({ includeDrafts: true })
        .ascending('dateAndTimeSettings.startDate')
        .limit(batchSize)
        .skip(offset)
        .find();

      allEvents.push(...query.items);
      if (query.items.length < batchSize) break;
      offset += batchSize;
    }

    return allEvents;
  })();
}
// adminEvents.jsw
import { webMethod, Permissions } from 'wix-web-module';
import { getAllEventsElevated } from 'backend/adminevents.js';

export function getAllEventsData() {
  return webMethod(Permissions.Admin, async () => {
    try {
      const events = await getAllEventsElevated();
      return { success: true, events };
    } catch (error) {
      return { success: false, error: error.message };
    }
  })();
}

//This is how I called it in frontend

const result = await getAllEventsData();

It’s difficult to say without seeing the code you’re tried. Can you share the code?

Hey! @noahlovell, Thanks for your comment, I have updated the post with the Code.

@noahlovell Any updates on this?

I think it’s because you’re using elevate incorrectly. It also seems you’re using webMethods inside .jsw which won’t work. It’ll need to be within a .web.js file, and written with the correct syntax.

Just run through a test site, and this code works.

Page code:

import { getAllEventsData } from "backend/adminEvents.web.js"

$w.onReady(async function () {
    const events = await getAllEventsData()
    console.log(events)
});

Backend:

// backend/adminEvents.web.js
import { Permissions, webMethod } from "wix-web-module";
import { getAllEventsElevated } from 'backend/adminEvents.js';

export const getAllEventsData = webMethod(Permissions.Anyone, async () => {
    try {
        const events = await getAllEventsElevated();
        return { success: true, events };
    } catch (error) {
        return { success: false, error: error.message };
    }
});
// backend/adminEvents.js
import { wixEventsV2 } from 'wix-events.v2';
import { elevate } from 'wix-auth';

export async function getAllEventsElevated() {
    const elevatedEventsQuery = elevate(wixEventsV2.queryEvents)

    let allEvents = [];
    let offset = 0;
    const batchSize = 100;

    while (true) {
        const query = await elevatedEventsQuery({ includeDrafts: true })
            .ascending('dateAndTimeSettings.startDate')
            .limit(batchSize)
            .skip(offset)
            .find();

        allEvents.push(...query.items);
        if (query.items.length < batchSize) break;
        offset += batchSize;
    }

    return allEvents;
};
2 Likes

Thanks @noahlovell it worked as my expectation. Also I hope the WIX documentation mention about it somewhere.

1 Like