ConnectWebViewPanel: Connect login flow start moved from ctor to on_page_will_load

This commit is contained in:
Jan Bařtipán 2024-06-20 10:36:08 +02:00 committed by Lukas Matena
parent bd6b570e95
commit e70ee320d9
2 changed files with 28 additions and 8 deletions

View File

@ -145,6 +145,8 @@ void WebViewPanel::load_url(const wxString& url)
if (!m_browser) if (!m_browser)
return; return;
this->on_page_will_load();
this->Show(); this->Show();
this->Raise(); this->Raise();
#ifdef DEBUG_URL_PANEL #ifdef DEBUG_URL_PANEL
@ -258,6 +260,10 @@ void WebViewPanel::on_navigation_request(wxWebViewEvent &evt)
{ {
} }
void WebViewPanel::on_page_will_load()
{
}
/** /**
* Invoked when user selects the "View Source" menu item * Invoked when user selects the "View Source" menu item
*/ */
@ -560,7 +566,21 @@ ConnectWebViewPanel::ConnectWebViewPanel(wxWindow* parent)
{ {
//m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new WebViewHandler("https"))); //m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new WebViewHandler("https")));
wxGetApp().plater()->Bind(EVT_UA_ID_USER_SUCCESS, &ConnectWebViewPanel::on_user_token, this);
}
ConnectWebViewPanel::~ConnectWebViewPanel()
{
m_browser->Unbind(EVT_UA_ID_USER_SUCCESS, &ConnectWebViewPanel::on_user_token, this);
}
void ConnectWebViewPanel::on_page_will_load()
{
Plater* plater = wxGetApp().plater(); Plater* plater = wxGetApp().plater();
const std::string& access_token = plater->get_user_account()->get_access_token();
assert(!access_token.empty());
m_browser->AddUserScript(wxString::Format( m_browser->AddUserScript(wxString::Format(
#if AUTH_VIA_FETCH_OVERRIDE #if AUTH_VIA_FETCH_OVERRIDE
@ -594,7 +614,7 @@ ConnectWebViewPanel::ConnectWebViewPanel(wxWindow* parent)
window.__access_token_version = 0; window.__access_token_version = 0;
)", )",
#else #else
R"( R"(
console.log('Preparing login'); console.log('Preparing login');
window.fetch('/slicer/login', {method: 'POST', headers: {Authorization: 'Bearer %s'}}) window.fetch('/slicer/login', {method: 'POST', headers: {Authorization: 'Bearer %s'}})
.then((resp) => { .then((resp) => {
@ -603,19 +623,14 @@ ConnectWebViewPanel::ConnectWebViewPanel(wxWindow* parent)
}); });
)", )",
#endif #endif
plater->get_user_account()->get_access_token() access_token
)); ));
plater->Bind(EVT_UA_ID_USER_SUCCESS, &ConnectWebViewPanel::on_user_token, this);
}
ConnectWebViewPanel::~ConnectWebViewPanel()
{
m_browser->Unbind(EVT_UA_ID_USER_SUCCESS, &ConnectWebViewPanel::on_user_token, this);
} }
void ConnectWebViewPanel::on_user_token(UserAccountSuccessEvent& e) void ConnectWebViewPanel::on_user_token(UserAccountSuccessEvent& e)
{ {
e.Skip(); e.Skip();
assert(!access_token.empty());
wxString javascript = wxString::Format( wxString javascript = wxString::Format(
#if AUTH_VIA_FETCH_OVERRIDE #if AUTH_VIA_FETCH_OVERRIDE
"window.__access_token = '%s';window.__access_token_version = (window.__access_token_version || 0) + 1;console.log('Updated Auth token', window.__access_token);", "window.__access_token = '%s';window.__access_token_version = (window.__access_token_version || 0) + 1;console.log('Updated Auth token', window.__access_token);",

View File

@ -59,6 +59,10 @@ public:
void set_default_url(const wxString& url) { m_default_url = url; } void set_default_url(const wxString& url) { m_default_url = url; }
virtual void sys_color_changed(); virtual void sys_color_changed();
protected:
virtual void on_page_will_load();
protected: protected:
wxWebView* m_browser { nullptr }; wxWebView* m_browser { nullptr };
@ -201,6 +205,7 @@ protected:
void on_connect_action_print(const std::string& message_data) override; void on_connect_action_print(const std::string& message_data) override;
void on_connect_action_webapp_ready(const std::string& message_data) override {} void on_connect_action_webapp_ready(const std::string& message_data) override {}
void run_script_bridge(const wxString& script) override {run_script(script); } void run_script_bridge(const wxString& script) override {run_script(script); }
void on_page_will_load() override;
private: private:
void on_user_token(UserAccountSuccessEvent& e); void on_user_token(UserAccountSuccessEvent& e);
bool m_reached_default_url {false}; bool m_reached_default_url {false};