-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69fee3b
commit fdd11d5
Showing
12 changed files
with
175 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use tauri::{Emitter, EventTarget}; | ||
use tauri_plugin_deep_link::DeepLinkExt; | ||
|
||
use crate::windows::{recreate_window, Window}; | ||
|
||
#[derive(Debug, Clone, serde::Serialize)] | ||
pub struct OAuthDeepLinkPayload { | ||
pub state: String, | ||
pub code: String, | ||
} | ||
|
||
pub fn setup_deep_links(app: &tauri::AppHandle) -> tauri::Result<()> { | ||
#[cfg(any(windows, target_os = "linux"))] | ||
{ | ||
use tauri_plugin_deep_link::DeepLinkExt; | ||
app.deep_link() | ||
.register_all() | ||
.map_err(|e| tauri::Error::Anyhow(e.into()))?; | ||
} | ||
let app_handle = app.clone(); | ||
app.deep_link().on_open_url(move |event| { | ||
let urls: Vec<_> = event.urls().into_iter().collect(); | ||
log::debug!("deep link URLs: {:?}", urls); | ||
for url in urls { | ||
log::debug!("handling deep link: {:?}", url); | ||
if let Some(host) = url.host() { | ||
if host.to_string() == "oauth" { | ||
log::debug!("oauth deep link: {:?}", url); | ||
let payload = OAuthDeepLinkPayload { | ||
state: url.query().unwrap_or_default().to_string(), | ||
code: url.query().unwrap_or_default().to_string(), | ||
}; | ||
log::debug!("emitting oauth-deep-link event to {}", Window::Coordinator.as_str()); | ||
let _ = recreate_window(app_handle.clone(), Window::Main, true); | ||
let _ = app_handle.emit_to( | ||
EventTarget::webview_window(Window::Coordinator.as_str()), | ||
"oauth-deep-link", | ||
payload, | ||
); | ||
} | ||
} | ||
} | ||
}); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useSetOAuthToken } from '@shinkai_network/shinkai-node-state/v2/mutations/setOAuthToken/index'; | ||
import { emit, listen } from '@tauri-apps/api/event'; | ||
import { useEffect } from 'react'; | ||
|
||
import { useAuth } from '../store/auth'; | ||
|
||
export const useOAuthDeepLink = () => { | ||
const { mutateAsync: setOAuthToken } = useSetOAuthToken({ | ||
onSuccess: (data) => { | ||
console.log('oauth-success', data); | ||
emit('oauth-success', data); | ||
}, | ||
}); | ||
const auth = useAuth((s) => s.auth); | ||
|
||
useEffect(() => { | ||
if (!auth) return; | ||
const unlisten = listen('oauth-deep-link', (event) => { | ||
console.log('useOAuthDeepLink'); | ||
|
||
const payload = event.payload as { state: string; code: string }; | ||
setOAuthToken({ | ||
state: payload.state, | ||
code: payload.code, | ||
nodeAddress: auth.node_address ?? '', | ||
token: auth.api_v2_key ?? '', | ||
}); | ||
}); | ||
|
||
return () => { | ||
unlisten.then((fn) => fn()); | ||
}; | ||
}, [setOAuthToken, auth]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
libs/shinkai-node-state/src/v2/mutations/setOAuthToken/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { setOAuthToken } from '@shinkai_network/shinkai-message-ts/api/tools/index'; | ||
import { useMutation, UseMutationOptions } from '@tanstack/react-query'; | ||
|
||
import { APIError } from '../../types'; | ||
|
||
export type SetOAuthTokenInput = { | ||
nodeAddress: string; | ||
token: string; | ||
state: string; | ||
code: string; | ||
}; | ||
|
||
export type SetOAuthTokenOutput = { | ||
state: string; | ||
code: string; | ||
}; | ||
|
||
type Options = UseMutationOptions< | ||
SetOAuthTokenOutput, | ||
APIError, | ||
SetOAuthTokenInput | ||
>; | ||
|
||
export const useSetOAuthToken = (options?: Options) => { | ||
return useMutation({ | ||
mutationFn: async (variables) => { | ||
const { nodeAddress, token, state, code } = variables; | ||
await setOAuthToken(nodeAddress, token, { | ||
code, | ||
state, | ||
}); | ||
return { code, state }; | ||
}, | ||
...options, | ||
}); | ||
}; |