Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: addBeforeAuthCompleteCallback plugin API method (#23441)

Mark VanLandingham576c76e4cb67
diff --git a/app/assets/javascripts/discourse/app/instance-initializers/auth-complete.js b/app/assets/javascripts/discourse/app/instance-initializers/auth-complete.js
index f5a5d8e0cb0..9b2ab1e6410 100644
--- a/app/assets/javascripts/discourse/app/instance-initializers/auth-complete.js
+++ b/app/assets/javascripts/discourse/app/instance-initializers/auth-complete.js
@@ -15,6 +15,16 @@ const AuthErrors = [
"not_allowed_from_ip_address",
];
+const beforeAuthCompleteCallbacks = [];
+
+export function addBeforeAuthCompleteCallback(fn) {
+ beforeAuthCompleteCallbacks.push(fn);
+}
+
+export function resetBeforeAuthCompleteCallbacks() {
+ beforeAuthCompleteCallbacks.length = 0;
+}
+
export default {
after: "inject-objects",
initialize(owner) {
@@ -30,12 +40,17 @@ export default {
const router = owner.lookup("router:main");
router.one("didTransition", () => {
next(() => {
+ const options = JSON.parse(lastAuthResult);
+
+ if (!beforeAuthCompleteCallbacks.every((fn) => fn(options))) {
+ return;
+ }
+
if (router.currentPath === "invites.show") {
owner
.lookup("controller:invites-show")
- .authenticationComplete(JSON.parse(lastAuthResult));
+ .authenticationComplete(options);
} else {
- const options = JSON.parse(lastAuthResult);
const modal = owner.lookup("service:modal");
const siteSettings = owner.lookup("service:site-settings");
diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js
index 4503549ef8d..43e81431cf4 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-api.js
+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js
@@ -128,12 +128,13 @@ import { registerCustomUserNavMessagesDropdownRow } from "discourse/controllers/
import { registerFullPageSearchType } from "discourse/controllers/full-page-search";
import { registerHashtagType } from "discourse/lib/hashtag-autocomplete";
import { _addBulkButton } from "discourse/components/modal/topic-bulk-actions";
+import { addBeforeAuthCompleteCallback } from "discourse/instance-initializers/auth-complete";
// If you add any methods to the API ensure you bump up the version number
// based on Semantic Versioning 2.0.0. Please update the changelog at
// docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md whenever you change the version
// using the format described at https://keepachangelog.com/en/1.0.0/.
-export const PLUGIN_API_VERSION = "1.10.0";
+export const PLUGIN_API_VERSION = "1.11.0";
// This helper prevents us from applying the same `modifyClass` over and over in test mode.
function canModify(klass, type, resolverName, changes) {
@@ -2400,6 +2401,15 @@ class PluginApi {
registerFullPageSearchType(translationKey, searchTypeId, searchFunc);
}
+ /**
+ * @param {function} fn - Function that will be called before the auth complete logic is run
+ * in instance-initializers/auth-complete.js. If any single callback returns false, the
+ * auth-complete logic will be aborted.
+ */
+ addBeforeAuthCompleteCallback(fn) {
+ addBeforeAuthCompleteCallback(fn);
+ }
+
/**
* Registers a hashtag type and its corresponding class.
* This is used when generating CSS classes in the hashtag-css-generator.
diff --git a/app/assets/javascripts/discourse/tests/acceptance/auth-complete-test.js b/app/assets/javascripts/discourse/tests/acceptance/auth-complete-test.js
index 0545939c499..bd7188bf5a7 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/auth-complete-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/auth-complete-test.js
@@ -1,6 +1,7 @@
import { acceptance, exists } from "discourse/tests/helpers/qunit-helpers";
import { currentRouteName, visit } from "@ember/test-helpers";
import { test } from "qunit";
+import { withPluginApi } from "discourse/lib/plugin-api";
acceptance("Auth Complete", function (needs) {
needs.hooks.beforeEach(() => {
@@ -49,4 +50,28 @@ acceptance("Auth Complete", function (needs) {
"it shows the registration modal"
);
});
+
+ test("Callback added using addBeforeAuthCompleteCallback", async function (assert) {
+ withPluginApi("1.11.0", (api) => {
+ api.addBeforeAuthCompleteCallback(() => {
+ api.container
+ .lookup("router:main")
+ .transitionTo("discovery.categories");
+ return false;
+ });
+ });
+
+ await visit("/");
+
+ assert.strictEqual(
+ currentRouteName(),
+ "discovery.categories",
+ "The function added via API was run and it transitioned to 'discovery.categories' route"
+ );
+
+ assert.notOk(
+ exists("#discourse-modal div.create-account-body"),
+ "registration modal is not shown"
+ );
+ });
});
diff --git a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js
index 614f8a9324a..b0580dfe048 100644
--- a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js
+++ b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js
@@ -91,6 +91,7 @@ import { resetMentions } from "discourse/lib/link-mentions";
import { resetModelTransformers } from "discourse/lib/model-transformers";
import { cleanupTemporaryModuleRegistrations } from "./temporary-module-helper";
import { clearBulkButtons } from "discourse/components/modal/topic-bulk-actions";
+import { resetBeforeAuthCompleteCallbacks } from "discourse/instance-initializers/auth-complete";
export function currentUser() {
return User.create(sessionFixtures["/session/current.json"].current_user);
@@ -228,6 +229,7 @@ export function testCleanup(container, app) {
cleanupTemporaryModuleRegistrations();
cleanupCssGeneratorTags();
clearBulkButtons();
+ resetBeforeAuthCompleteCallbacks();
}
function cleanupCssGeneratorTags() {
diff --git a/docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md b/docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md
index 176ea043469..6473d754758 100644
--- a/docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md
+++ b/docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md
@@ -7,6 +7,14 @@ in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
+## [1.11.0] - 2023-08-30
+
+### Added
+
+- Adds `addBeforeAuthCompleteCallback` which allows plugins and themes to add functions to be
+ evaluated before the auth-complete logic is run. If any of these callbacks return false, the
+ auth-complete logic will be aborted.
+
## [1.10.0] - 2023-08-25
### Added