main
rs 70 lines 2.03 KB
Raw
1 use {
2 smbcloud_model::oauth::OauthRedirect,
3 tauri::{command, AppHandle, Emitter, Url},
4 tauri_plugin_log::log::{debug, error},
5 tauri_plugin_oauth::{start_with_config, OauthConfig},
6 };
7
8 #[command]
9 pub async fn start_server(app: AppHandle) -> Result<u16, String> {
10 let config = OauthConfig {
11 ports: Some(vec![8000, 8001, 8002, 8003]),
12 response: Some(
13 "
14 <html>
15 <head>
16 <title>Done!</title>
17 <style>
18 body {
19 font-family: sans-serif;
20 text-align: center;
21 padding: 50px;
22 }
23 h1 {
24 color: #333;
25 }
26 p {
27 font-size: 20px;
28 color: #666;
29 }
30 a {
31 color: #007BFF;
32 text-decoration: none;
33 }
34 a:hover {
35 text-decoration: underline;
36 }
37 </style>
38 </head>
39 <body>
40 <h1>Done!</h1>
41 <p>You can close this window. You can <a href='rumi://'>open the app</a>.</p>
42 <p>If you are not redirected, <a href=\"javascript:window.close()\">click here</a>.</p>
43 </body>
44 </html>
45 "
46 .into(),
47 ),
48 };
49 start_with_config(config, move |url| {
50 debug!("Received URL: {}", url);
51 let url = Url::try_from(url.as_str()).unwrap();
52
53 for (key, val) in url.query_pairs() {
54 debug!("{} = {}", key, val);
55 }
56
57 if let Some(query) = url.query() {
58 let oauth_redirect: OauthRedirect = serde_qs::from_str(query).unwrap();
59 match app.emit("oauth_redirect_uri", oauth_redirect) {
60 Ok(_) => {
61 debug!("Emitted event.");
62 }
63 Err(e) => {
64 error!("Failed to emit event: {}", e);
65 }
66 }
67 }
68 })
69 .map_err(|err| err.to_string())
70 }