mirror of
https://github.com/infinilabs/coco-app.git
synced 2025-12-29 00:24:46 +01:00
refactor: refactoring auth flow (#226)
This commit is contained in:
@@ -5,5 +5,5 @@ use serde::{Deserialize, Serialize};
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RequestAccessTokenResponse {
|
||||
pub access_token: String,
|
||||
pub expire_at: u32,
|
||||
pub expire_in: u32,
|
||||
}
|
||||
|
||||
@@ -6,10 +6,11 @@ use crate::server::profile::get_user_profiles;
|
||||
use crate::server::search::CocoSearchSource;
|
||||
use crate::server::servers::{get_server_by_id, persist_servers, persist_servers_token, save_access_token, save_server};
|
||||
use reqwest::{Client, StatusCode};
|
||||
use std::collections::HashMap;
|
||||
use tauri::{AppHandle, Manager, Runtime};
|
||||
fn request_access_token_url(request_id: &str, code: &str) -> String {
|
||||
fn request_access_token_url(request_id: &str) -> String {
|
||||
// Remove the endpoint part and keep just the path for the request
|
||||
format!("/auth/request_access_token?request_id={}&token={}", request_id, code)
|
||||
format!("/auth/request_access_token?request_id={}", request_id)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -25,10 +26,12 @@ pub async fn handle_sso_callback<R: Runtime>(
|
||||
if let Some(mut server) = server {
|
||||
// Prepare the URL for requesting the access token (endpoint is base URL, path is relative)
|
||||
// save_access_token(server_id.clone(), ServerAccessToken::new(server_id.clone(), code.clone(), 60 * 15));
|
||||
let path = request_access_token_url(&request_id, &code);
|
||||
let path = request_access_token_url(&request_id);
|
||||
|
||||
// Send the request for the access token using the util::http::HttpClient::get method
|
||||
let response = HttpClient::get(&server_id, &path, None)
|
||||
let mut header = HashMap::new();
|
||||
header.insert("Authorization".to_string(), format!("Bearer {}", code).to_string());
|
||||
let response = HttpClient::advanced_post(&server_id, &path, Some(header), None, None)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to send request to the server: {}", e))?;
|
||||
|
||||
@@ -45,7 +48,7 @@ pub async fn handle_sso_callback<R: Runtime>(
|
||||
let access_token = ServerAccessToken::new(
|
||||
server_id.clone(),
|
||||
token.access_token.clone(),
|
||||
token.expire_at,
|
||||
token.expire_in,
|
||||
);
|
||||
// dbg!(&server_id, &request_id, &code, &token);
|
||||
save_access_token(server_id.clone(), access_token);
|
||||
|
||||
Reference in New Issue
Block a user