1

I'm building a Tauri app and would like to set up OAuth integration with Google. To do so, I will need a URI for the oauth callback, but Tauri is unclear how to configure the schema possibly using this method or with the WindowUrl?

How can I add a URI to my Tauri app so I could like to it like the following example: myapp://callback

I think it could look something like the following:

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![greet])
        .register_uri_scheme_protocol("myapp", move |app, request| {
            # protocol logic here
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

2 Answers 2

3

Tauri currently doesn't directly support deep linking. A good alternative that I found was this rust project. After installation you can do something like the following:

#[tauri::command]
async fn start_oauth_server(window: Window) -> Result<u16, String> {
println!("Starting server");

start(None, move |url| {
    // Because of the unprotected localhost port, you must verify the URL here.
    // Preferebly send back only the token, or nothing at all if you can handle everything else in Rust.

    // convert the string to a url
    let url = url::Url::parse(&url).unwrap();

    // get the code query parameter
    let code = url
        .query_pairs()
        .find(|(k, _)| k == "code")
        .unwrap_or_default()
        .1;

    // get the state query parameter
    let state = url
        .query_pairs()
        .find(|(k, _)| k == "state")
        .unwrap_or_default()
        .1;

    // create map of query parameters
    let mut query_params = HashMap::new();

    query_params.insert("code".to_string(), code.to_string());
    query_params.insert("state".to_string(), state.to_string());
    query_params.insert(String::from("redirect_uri"), url.to_string());

    if window.emit("redirect_uri", query_params).is_ok() {
        println!("Sent redirect_uri event");
    } else {
        println!("Failed to send redirect_uri event");
    }
})
.map_err(|err| err.to_string())
}

Update March 2023 Thanks Zoltán Balogh for the flag

Checkout https://github.com/FabianLars/tauri-plugin-deep-link

Fabian put this up for the short term, but longterm keep an eye out on PR #323

2

Please take a look at this plugin for Tauri

https://github.com/FabianLars/tauri-plugin-deep-link

This gives you deep linking on Windows as well as on Mac Os

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.