DEV: Add app webview event when triggering login (#29075)

Mobile app can capture event and launch a separate login flow. Should
help resolve issues with passkeys (which aren't available in webviews)
and non-local login methods.
This commit is contained in:
Penar Musaraj 2024-10-08 10:04:48 -04:00 committed by GitHub
parent d64d0ddd3d
commit 315f8c5ec6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import logout from "discourse/lib/logout";
import mobile from "discourse/lib/mobile";
import identifySource, { consolePrefix } from "discourse/lib/source-identifier";
import DiscourseURL from "discourse/lib/url";
import { postRNWebviewMessage } from "discourse/lib/utilities";
import Category from "discourse/models/category";
import Composer from "discourse/models/composer";
import { findAll } from "discourse/models/login-method";
@ -26,6 +27,7 @@ function isStrictlyReadonly(site) {
}
export default class ApplicationRoute extends DiscourseRoute {
@service capabilities;
@service clientErrorHandler;
@service composer;
@service currentUser;
@ -290,6 +292,9 @@ export default class ApplicationRoute extends DiscourseRoute {
}
handleShowLogin() {
if (this.capabilities.isAppWebview) {
postRNWebviewMessage("showLogin", true);
}
if (this.siteSettings.enable_discourse_connect) {
const returnPath = cookie("destination_url")
? getURL("/")

View File

@ -111,3 +111,26 @@ acceptance("Modal - Login - With no way to login", function (needs) {
assert.dom(".no-login-methods-configured").exists();
});
});
acceptance("Login button", function () {
test("with custom event on webview", async function (assert) {
const capabilities = this.container.lookup("service:capabilities");
sinon.stub(capabilities, "isAppWebview").value(true);
window.ReactNativeWebView = {
postMessage: () => {},
};
const webviewSpy = sinon.spy(window.ReactNativeWebView, "postMessage");
await visit("/");
await click("header .login-button");
assert.true(
webviewSpy.withArgs('{"showLogin":true}').calledOnce,
"triggers postmessage event"
);
delete window.ReactNativeWebView;
});
});