#Access Global State in deep_link callback

3 messages ยท Page 1 of 1 (latest)

normal umbra
#

After creating a deeplink I am trying to access my auth_state from the open_url callback in the setup method.

It seems like it is impossible to access the state and don't understand how I could do this... Why it seems impossible to do something that simple ๐Ÿ™


#[derive(Default)]
struct AuthData {
    state: Option<String>,
    code_verifier: Option<String>,
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    let mut builder = tauri::Builder::default();

    #[cfg(desktop)]
    {
        builder = builder.plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| {
            let _ = app
                .get_webview_window("main")
                .expect("no main window")
                .set_focus();
        }));
    }

 builder
        .plugin(tauri_plugin_opener::init())
        .setup(|app| {
            #[cfg(any(windows, target_os = "linux"))]
            {
                use tauri_plugin_deep_link::DeepLinkExt;
                app.deep_link().register_all()?;
            }
            app.manage(Mutex::new(AuthData::default()));

            // Note that get_current's return value will also get updated every time on_open_url gets triggered.

            let app_handle = app.app_handle();
            let state = app_handle.state::<Mutex<AuthData>>();

            // Lock the mutex to mutably access the state.
            let mut auth_data = state.lock().unwrap();
            app.deep_link().on_open_url(|event| {
                if let Some(deep_link_url) = event.urls().get(0) {
                  // <_----------------- NEED TO ACCESS THE AUTH STATE VALUES HERE
                }
            });
            Ok(())
        })
}
clear idol
#

it's easier if you move the AppHandle inside the event handler. Example: ```rs
let app_handle = app.app_handle().clone();

        app.deep_link().on_open_url(move |event| {//<- added move keyword
            if let Some(deep_link_url) = event.urls().get(0) {
              let state = app_handle.state::<Mutex<AuthData>>();
              let mut auth_data = state.lock().unwrap();
            }
        });
normal umbra