strapi-plugin-ckeditor icon indicating copy to clipboard operation
strapi-plugin-ckeditor copied to clipboard

feat(#146): allow loading license from server config

Open Jeff-Tian opened this issue 7 months ago • 5 comments

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

Jeff-Tian avatar May 26 '25 11:05 Jeff-Tian

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.)

ps-20x avatar Jun 23 '25 07:06 ps-20x

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

davidkassa avatar Jul 04 '25 18:07 davidkassa

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

davidkassa avatar Jul 04 '25 18:07 davidkassa

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.

Jeff-Tian avatar Jul 05 '25 03:07 Jeff-Tian

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

0xGurg avatar Sep 08 '25 11:09 0xGurg