Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: addBeforeAuthCompleteCallback plugin API method #23441

Merged
merged 2 commits into from Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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) {
Expand All @@ -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");

Expand Down
12 changes: 11 additions & 1 deletion app/assets/javascripts/discourse/app/lib/plugin-api.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down
@@ -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(() => {
Expand Down Expand Up @@ -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"
);
});
});
Expand Up @@ -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);
Expand Down Expand Up @@ -228,6 +229,7 @@ export function testCleanup(container, app) {
cleanupTemporaryModuleRegistrations();
cleanupCssGeneratorTags();
clearBulkButtons();
resetBeforeAuthCompleteCallbacks();
}

function cleanupCssGeneratorTags() {
Expand Down
8 changes: 8 additions & 0 deletions docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md
Expand Up @@ -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
Expand Down