FEATURE: addBeforeAuthCompleteCallback plugin API method (#23441)

This commit is contained in:
Mark VanLandingham 2023-09-06 08:48:51 -07:00 committed by GitHub
parent 60bc4efda4
commit 576c76e4cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 3 deletions

View File

@ -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");

View File

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

View File

@ -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"
);
});
});

View File

@ -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() {

View File

@ -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