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

Getting error for Tauri Plugin Oauth handler function - Option::unwrap() on a None value #37

Open
davismartens opened this issue Dec 19, 2024 · 5 comments

Comments

@davismartens
Copy link

davismartens commented Dec 19, 2024

I'm struggling to integrate Oauth into Tauri 2.0. I'm following the examples for this plugin but I'm not sure what is going wrong. I'm getting a Option::unwrap() on a None value error that crashes my app whenever the rust function is invoked.

Has anyone come across this before?

My backend is running on localhost:8000 and I have set the plugin ports 8005-8007

My setup

Cargo

[dependencies]
tauri-plugin-oauth = "2"

src-tauri\capabilities\default.json

"oauth:allow-cancel",
"oauth:allow-start"

tauri.config.json

  "plugins": {
    "oauth": {
      "ports": [8005, 8006, 8007],
      "response": "OAuth process completed. You can close this window."
    }

lib.rs

use tauri::{command, Emitter, Window};
use tauri_plugin_oauth::start;

#[command]
async fn start_server(window: Window) -> Result<u16, String> {
    start(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.
        let _ = window.emit("redirect_uri", url);
    })
        .map_err(|err| err.to_string())
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()

        .plugin(tauri_plugin_shell::init())
        .plugin(tauri_plugin_opener::init())
        .plugin(tauri_plugin_oauth::init())
        .invoke_handler(tauri::generate_handler![start_server])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

login.jsx

import { Button } from "@/components/ui/button";
import GoogleLogo from "@/assets/Google-Logo.svg";
import { useNavigate } from "@solidjs/router";
import { start, cancel, onUrl } from '@fabianlars/tauri-plugin-oauth';

let port;

function Login() {
  const navigate = useNavigate();

  const GoogleOAuthSignIn = async () => {
    try {
      port = await start();
      console.log(`OAuth server started on port ${port}`);

      await onUrl((url) => {
        console.log('Received OAuth URL:', url);
        
        // Handle the OAuth redirect logic here

        stopOAuthServer();
      });

    } catch (error) {
      console.error('Error starting OAuth server:', error);
    }
  };

  const stopOAuthServer = async () => {
    try {
      if (port) {
        await cancel(port);
        console.log('OAuth server stopped');
      }
    } catch (error) {
      console.error('Error stopping OAuth server:', error);
    }
  };

  return (
    <div class="flex items-center justify-center h-screen bg-background text-foreground">
      <div class="w-full max-w-md p-6 space-y-6">
        <h2 class="text-2xl font-bold text-center">Login</h2>
        <p class="text-sm text-muted-foreground text-center">
          Enter your email below to create your account
        </p>

        <div class="space-y-4">
          <Button
            variant="outline"
            class="w-full flex items-center justify-center gap-2"
            onClick={GoogleOAuthSignIn}
          >
            <img src={GoogleLogo} alt="Google Logo" class="w-5 h-5 filter invert" />
            Sign In with Google
          </Button>
        </div>

        <p class="text-xs text-center text-muted-foreground">
          By clicking continue, you agree to our{" "}
          <a href="#" class="underline">
            Terms of Service
          </a>{" "}
          and{" "}
          <a href="#" class="underline">
            Privacy Policy
          </a>
          .
        </p>
      </div>
    </div>
  );
}

export default Login;

Error

thread 'main' panicked at C:\Users\username\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tauri-plugin-oauth-2.0.0\src/lib.rs:211:37:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at core\src\panicking.rs:221:5:
panic in a function that cannot unwind
stack backtrace:
   0:     0x7ff729108e91 - std::backtrace_rs::backtrace::dbghelp64::trace
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\..\..\backtrace\src\backtrace\dbghelp64.rs:91
   1:     0x7ff729108e91 - std::backtrace_rs::backtrace::trace_unsynchronized
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\..\..\backtrace\src\backtrace\mod.rs:66
   2:     0x7ff729108e91 - std::sys::backtrace::_print_fmt
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\sys\backtrace.rs:66
   3:     0x7ff729108e91 - std::sys::backtrace::impl$0::print::impl$0::fmt
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\sys\backtrace.rs:39
   4:     0x7ff72912a70a - core::fmt::rt::Argument::fmt
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/core\src\fmt\rt.rs:177
   5:     0x7ff72912a70a - core::fmt::write
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/core\src\fmt\mod.rs:1186
   6:     0x7ff7291043d7 - std::io::Write::write_fmt<std::sys::pal::windows::stdio::Stderr>
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\io\mod.rs:1839
   7:     0x7ff729108cd5 - std::sys::backtrace::BacktraceLock::print
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\sys\backtrace.rs:42
   8:     0x7ff72910ae27 - std::panicking::default_hook::closure$1
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panicking.rs:268
   9:     0x7ff72910ac07 - std::panicking::default_hook
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panicking.rs:295
  10:     0x7ff72910b4b3 - std::panicking::rust_panic_with_hook
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panicking.rs:801
  11:     0x7ff72910b302 - std::panicking::begin_panic_handler::closure$0
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panicking.rs:667
  12:     0x7ff72910994f - std::sys::backtrace::__rust_end_short_backtrace<std::panicking::begin_panic_handler::closure_env$0,never$>
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\sys\backtrace.rs:170
  13:     0x7ff72910af3e - std::panicking::begin_panic_handler
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panicking.rs:665
  14:     0x7ff729134795 - core::panicking::panic_nounwind_fmt::runtime
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/core\src\panicking.rs:112
  15:     0x7ff729134795 - core::panicking::panic_nounwind_fmt
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/core\src\panicking.rs:122
  16:     0x7ff729134843 - core::panicking::panic_nounwind
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/core\src\panicking.rs:221
  17:     0x7ff7291349c7 - core::panicking::panic_cannot_unwind
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/core\src\panicking.rs:310
  18:     0x7ff728bafc76 - webview2_com_sys::Microsoft::Web::WebView2::Win32::impl$991::new::Invoke<webview2_com::callback::WebResourceRequestedEventHandler_Impl,-1>
                               at C:\Users\username\.cargo\registry\src\index.crates.io-6f17d22bba15001f\webview2-com-sys-0.33.0\src\Microsoft.rs:39957
  19:     0x7ff97660f730 - _CxxFrameHandler3
  20:     0x7ff9766033d8 - is_exception_typeof
  21:     0x7ff999b23846 - RtlCaptureContext2
  22:     0x7ff728bafc25 - webview2_com_sys::Microsoft::Web::WebView2::Win32::impl$991::new::Invoke<webview2_com::callback::WebResourceRequestedEventHandler_Impl,-1>
                               at C:\Users\username\.cargo\registry\src\index.crates.io-6f17d22bba15001f\webview2-com-sys-0.33.0\src\Microsoft.rs:39970
  23:     0x7ff94f346252 - CreateWebViewEnvironmentWithOptionsInternal
  24:     0x7ff94f34609f - CreateWebViewEnvironmentWithOptionsInternal
  25:     0x7ff94f35ce83 - CreateWebViewEnvironmentWithOptionsInternal
  26:     0x7ff94f3aef20 - DllCanUnloadNow
  27:     0x7ff94f5d76c8 - GetHandleVerifier
  28:     0x7ff94f39ce4c - DllCanUnloadNow
  29:     0x7ff94f39f151 - DllCanUnloadNow
  30:     0x7ff94f39f0b1 - DllCanUnloadNow
  31:     0x7ff94f39efb2 - DllCanUnloadNow
  32:     0x7ff94f552ba1 - GetHandleVerifier
  33:     0x7ff94f5e9a0c - GetHandleVerifier
  34:     0x7ff94f3a0e9c - DllCanUnloadNow
  35:     0x7ff94f5c9945 - GetHandleVerifier
  36:     0x7ff94f555396 - GetHandleVerifier
  37:     0x7ff94f55458a - GetHandleVerifier
  38:     0x7ff94f554e71 - GetHandleVerifier
  39:     0x7ff94f554b37 - GetHandleVerifier
  40:     0x7ff94f55473b - GetHandleVerifier
  41:     0x7ff94f55458a - GetHandleVerifier
  42:     0x7ff94f553ba5 - GetHandleVerifier
  43:     0x7ff94f5532b8 - GetHandleVerifier
  44:     0x7ff94f455694 - DllCanUnloadNow
  45:     0x7ff94f56b781 - GetHandleVerifier
  46:     0x7ff94f56b781 - GetHandleVerifier
  47:     0x7ff94f5530ff - GetHandleVerifier
  48:     0x7ff94f53a5a4 - GetHandleVerifier
  49:     0x7ff94f38c5f8 - DllCanUnloadNow
  50:     0x7ff94f38c456 - DllCanUnloadNow
  51:     0x7ff94f38bc8d - DllCanUnloadNow
  52:     0x7ff94f567f5c - GetHandleVerifier
  53:     0x7ff94f567e40 - GetHandleVerifier
  54:     0x7ff94f4c1eef - DllCanUnloadNow
  55:     0x7ff9996f5801 - CallWindowProcW
  56:     0x7ff9996f334d - IsWindowUnicode
  57:     0x7ff7287348ff - windows::Win32::UI::WindowsAndMessaging::DispatchMessageW
                               at C:\Users\username\.cargo\registry\src\index.crates.io-6f17d22bba15001f\windows-0.58.0\src\Windows\Win32\UI\WindowsAndMessaging\mod.rs:772
  58:     0x7ff7286b4422 - tao::platform_impl::platform::event_loop::EventLoop<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >::run_return<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > >,tauri_runtime_wry::impl$45::run::closure_env$0<enu
                               at C:\Users\username\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.30.8\src\platform_impl\windows\event_loop.rs:259
  59:     0x7ff7286b4b5b - tao::platform_impl::platform::event_loop::EventLoop<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >::run<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > >,tauri_runtime_wry::impl$45::run::closure_env$0<enum2$<tau
                               at C:\Users\username\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.30.8\src\platform_impl\windows\event_loop.rs:221
  60:     0x7ff728710431 - tao::event_loop::EventLoop<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >::run<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > >,tauri_runtime_wry::impl$45::run::closure_env$0<enum2$<tauri::EventLoopMessage>,tau
                               at C:\Users\username\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.30.8\src\event_loop.rs:215
  61:     0x7ff72840fcd9 - tauri_runtime_wry::impl$45::run<enum2$<tauri::EventLoopMessage>,tauri::app::impl$16::run::closure_env$0<tauri_runtime_wry::Wry<enum2$<tauri::EventLoopMessage> >,tauri::app::impl$19::run::closure_env$0<tauri_runtime_wry::Wry<enum2$<tauri::EventLoopMessage>
                               at C:\Users\username\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tauri-runtime-wry-2.2.0\src\lib.rs:2812
  62:     0x7ff728217880 - tauri::app::App<tauri_runtime_wry::Wry<enum2$<tauri::EventLoopMessage> > >::run<tauri_runtime_wry::Wry<enum2$<tauri::EventLoopMessage> >,tauri::app::impl$19::run::closure_env$0<tauri_runtime_wry::Wry<enum2$<tauri::EventLoopMessage> > > >
                               at C:\Users\username\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tauri-2.1.1\src\app.rs:1146
  63:     0x7ff7282180ac - tauri::app::Builder<tauri_runtime_wry::Wry<enum2$<tauri::EventLoopMessage> > >::run<tauri_runtime_wry::Wry<enum2$<tauri::EventLoopMessage> > >
                               at C:\Users\username\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tauri-2.1.1\src\app.rs:2069
  64:     0x7ff72821150f - typeupapp_lib::run
                               at C:\Users\username\dev-projects\typeUp\v_2\typeUpTauri\src-tauri\src\lib.rs:33
  65:     0x7ff728211029 - typeupapp::main
                               at C:\Users\username\dev-projects\typeUp\v_2\typeUpTauri\src-tauri\src\main.rs:5
  66:     0x7ff72821115b - core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library\core\src\ops\function.rs:250
  67:     0x7ff72821100e - core::hint::black_box
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library\core\src\hint.rs:389
  68:     0x7ff72821100e - std::sys::backtrace::__rust_begin_short_backtrace<void (*)(),tuple$<> >
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library\std\src\sys\backtrace.rs:154
  69:     0x7ff7282110c1 - std::rt::lang_start::closure$0<tuple$<> >
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library\std\src\rt.rs:195
  70:     0x7ff7290ff31c - std::rt::lang_start_internal::closure$1
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\rt.rs:174
  71:     0x7ff7290ff31c - std::panicking::try::do_call
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panicking.rs:557
  72:     0x7ff7290ff31c - std::panicking::try
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panicking.rs:520
  73:     0x7ff7290ff31c - std::panic::catch_unwind
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panic.rs:358
  74:     0x7ff7290ff31c - std::rt::lang_start_internal
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\rt.rs:174
  75:     0x7ff72821109a - std::rt::lang_start<tuple$<> >
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library\std\src\rt.rs:194
  76:     0x7ff728211049 - main
  77:     0x7ff729131dc0 - invoke_main
                               at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  78:     0x7ff729131dc0 - __scrt_common_main_seh
                               at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
  79:     0x7ff99804e8d7 - BaseThreadInitThunk
  80:     0x7ff999adfbcc - RtlUserThreadStart
thread caused non-unwinding panic. aborting.
@davismartens davismartens changed the title Getting error: Option::unwrap() on a None value Getting error for Tauri Plugin Oauth handler function - Option::unwrap() on a None value Dec 19, 2024
@FabianLars
Copy link
Owner

Has anyone come across this before?

I don't think anyone has reported this to me yet.

Does this also happen if you completely remove the plugin from tauri.conf.json?

I'll be unavailable until Saturday (or maybe Sunday, we'll see) so can't take a closer look until then.

@davismartens
Copy link
Author

Does this also happen if you completely remove the plugin from tauri.conf.json?

It only happens after I modify lib.rs to handle the oauth plugin, otherwise I have not come across it.

No worries if you are unavailable. I will try to trouble shoot / work around, and will share if I find a solution.

@FabianLars
Copy link
Owner

It only happens after I modify lib.rs to handle the oauth plugin, otherwise I have not come across it.

Just to be clear, with and without the plugin config in tauri.conf.json?
Without modifying lib.rs the plugin won't even be compiled into the app so that part makes sense.

@davismartens
Copy link
Author

davismartens commented Dec 21, 2024

Just to be clear, with and without the plugin config in tauri.conf.json?

Thanks for clarifying, I reinstalled and removed the plugin from tauri.conf.json which has resolved the error.

What isn't working yet though is emitting the url from the window. If I open a Login window and open on onUrl listener, I don't receive anything. Am I doing something wrong?

I followed the example:

backend running on localhost:8000
oauth entry localhost:8000
oauth redirect localhost:1420?token={...}

I don't really understand why the url isn't being emitted.

lib.rs

use tauri::{command, Emitter, Window};
use tauri_plugin_oauth::start;

#[command]
async fn start_server(window: Window) -> Result<u16, String> {
    start(move |url| {
        // Log the captured URL to the terminal
        println!("Captured URL: {}", url);

        // Emit the URL to the frontend
        let emit_result = window.emit("redirect_uri", url);

        // Log if the emission was successful or failed
        if let Err(e) = emit_result {
            eprintln!("Failed to emit redirect_uri event: {:?}", e);
        }
    })
    .map_err(|err| err.to_string())
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_shell::init())
        .plugin(tauri_plugin_opener::init())
        .plugin(tauri_plugin_oauth::init())
        .invoke_handler(tauri::generate_handler![start_server])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

login.jsx

// Import the required modules
import { Button } from "@/components/ui/button";
import GoogleLogo from "@/assets/Google-Logo.svg";
import { start, onUrl } from "@fabianlars/tauri-plugin-oauth";

function Login() {
  const handleGoogleSignIn = async () => {
    try {
      // Start the OAuth server
      const port = await start();
      console.log(`OAuth server started on port ${port}`);

      // Set up a listener for the OAuth redirect and log the raw URL
      await onUrl((url) => {
        console.log("Received OAuth URL:", url);
      });

      // Open the OAuth flow in a new window
      const oauthUrl = `http://localhost:8000/login`;
      window.open(oauthUrl, "_blank");
    } catch (error) {
      console.error("Error starting OAuth server:", error);
    }
  };

  return (
    <div class="flex items-center justify-center h-screen bg-background text-foreground">
      <div class="w-full max-w-md p-6 space-y-6">
        <h2 class="text-2xl font-bold text-center">Login</h2>
        <p class="text-sm text-muted-foreground text-center">
          Enter your email below to create your account
        </p>

        <div class="space-y-4">
          <Button
            variant="outline"
            class="w-full flex items-center justify-center gap-2"
            onClick={handleGoogleSignIn}
          >
            Sign In with Google
          </Button>
        </div>

        <p class="text-xs text-center text-muted-foreground">
          By clicking continue, you agree to our{" "}
          <a href="#" class="underline">
            Terms of Service
          </a>{" "}
          and{" "}
          <a href="#" class="underline">
            Privacy Policy
          </a>
          .
        </p>
      </div>
    </div>
  );
}

export default Login;

@FabianLars
Copy link
Owner

FabianLars commented Dec 22, 2024

Thanks for clarifying, I reinstalled and removed the plugin from tauri.conf.json which has resolved the error.

ok that's good but still something i have to look into since the config you had was technically correct.

oauth redirect localhost:1420?token={...}

This looks wrong. 1420 is likely the frontend port, not the plugin's port. You have to redirect to the plugin's port for the event to trigger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants