Skip to content

Conversation

@Jeff-Tian
Copy link

@Jeff-Tian Jeff-Tian commented May 26, 2025

Allow loading license from server config, so that:

  • Don't need to hardcode the license to the source file schema.json
  • Fix the license error in production because of using the hardcoded development key
  • Allow different license keys in different environments

For verifying it, I've published a fork @jeff-tian/strapi-plugin-ckeditor, which works as expected.

image

Server Config

Add a CKEDITOR_LICENSE_KEY to the environment variable to allow the client side to load:

image

@ps-20x
Copy link

ps-20x commented Jun 23, 2025

Sorry to ping you @Mgsy, @Reinmar but can we please get this in soon?
(This is blocking our strapi 5 upgrade actually because having the key in each file is very cumbersome.)

@davidkassa
Copy link

Is there a permissions issue for /ckeditor/config? I'm still on Strapi 4, but receiving a 401 unauthorized error.

@davidkassa
Copy link

Per Claude (but works on my machine):

Fix CKEditor License Key Authentication Issue

Problem

The /ckeditor/config endpoint was returning 401 Unauthorized errors, causing the CKEditor to be stuck on "Loading License Key..." indefinitely.

Root Cause

The CKEditor plugin was using the deprecated request function from @strapi/helper-plugin, which wasn't properly handling authentication headers for admin API
calls.

Solution

Updated the license API to use the modern useFetchClient hook while maintaining clean abstraction:

Key Changes:

  1. Replaced deprecated request function with useFetchClient hook
  2. Maintained API abstraction by using dependency injection pattern
  3. Added proper error handling with console logging for debugging
  4. Updated React dependencies to follow hooks best practices

Files Modified

src/plugins/strapi-plugin-ckeditor/admin/src/api/license.js

- import { request } from "@strapi/helper-plugin";
-
- const licenseRequests = {
-   getLicense: async () => await request(`/ckeditor/config`, { method: "GET" }),
- };
-
- export default licenseRequests;
+ const licenseRequests = {
+   getLicense: async (fetchClient) => {
+     const response = await fetchClient.get("/ckeditor/config");
+     return response;
+   },
+ };
+
+ export default licenseRequests;

src/plugins/strapi-plugin-ckeditor/admin/src/components/CKEditorProvider/index.js
  import { memo, useEffect, useState } from "react";
  import { useCKEditorCloud } from "@ckeditor/ckeditor5-react";
- import licenseRequests from "../../api/license";
+ import { useFetchClient } from "@strapi/helper-plugin";
+ import licenseRequests from "../../api/license";

  const { options } = attribute;
+ const fetchClient = useFetchClient();

  const [licenseKey, setLicenseKey] = useState(options?.licenseKey);

  useEffect(() => {
-   licenseRequests.getLicense().then((response) => {
+   licenseRequests.getLicense(fetchClient).then((response) => {
      const licenseKeyFromServer = response.data?.ckeditor?.licenseKey;
      if (licenseKeyFromServer) {
        setLicenseKey(licenseKeyFromServer);
      }
-   });
- }, []);
+   }).catch((error) => {
+     console.error("Failed to fetch CKEditor license key:", error);
+   });
+ }, [fetchClient]);

Technical Details

The useFetchClient hook automatically:
* Adds JWT authentication headers via request interceptors
* Handles 401 responses with automatic logout
* Uses the supported Strapi v4 API patterns

Benefits

* ✅ Resolves authentication errors for CKEditor license endpoint
* ✅ Maintains clean code architecture with proper abstraction
* ✅ Uses modern, supported Strapi APIs instead of deprecated functions
* ✅ Improves error handling and debugging capabilities
* ✅ Follows React hooks best practices

Testing

* License key should now load successfully for authenticated admin users
* "Loading License Key..." message should disappear after successful API call
* CKEditor should initialize with proper license configuration

@Jeff-Tian
Copy link
Author

Is there a permissions issue for /ckeditor/config? I'm still on Strapi 4, but receiving a 401 unauthorized error.

This PR is for Strapi v5, and tested it worked very well.

@0xGurg
Copy link

0xGurg commented Sep 8, 2025

using v5.23 i experience 401 as well on fetching keys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants