feat: auto login on startup

This commit is contained in:
2024-11-25 08:55:50 +07:00
parent db7e28a78a
commit e619fa365b
6 changed files with 41 additions and 6 deletions

View File

@@ -12,8 +12,8 @@ components = { package = "ui", git = "https://github.com/longbridgeapp/gpui-comp
reqwest_client = { git = "https://github.com/huacnlee/zed.git", branch = "export-platform-window" } reqwest_client = { git = "https://github.com/huacnlee/zed.git", branch = "export-platform-window" }
# Nostr # Nostr
nostr-relay-builder = { git = "https://github.com/rust-nostr/nostr", branch = "nip17" } nostr-relay-builder = { git = "https://github.com/rust-nostr/nostr" }
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", branch = "nip17", features = [ nostr-sdk = { git = "https://github.com/rust-nostr/nostr", features = [
"lmdb", "lmdb",
"all-nips", "all-nips",
] } ] }

View File

@@ -23,7 +23,7 @@ pub async fn get_client() -> &'static Client {
// Add some bootstrap relays // Add some bootstrap relays
let _ = client.add_relay("wss://relay.damus.io").await; let _ = client.add_relay("wss://relay.damus.io").await;
let _ = client.add_relay("wss://relay.primal.net").await; let _ = client.add_relay("wss://relay.primal.net").await;
let _ = client.add_relay("wss://nostr.fmt.wiz.biz").await; let _ = client.add_relay("wss://nos.lol").await;
let _ = client.add_relay("wss://directory.yabu.me").await; let _ = client.add_relay("wss://directory.yabu.me").await;
let _ = client.add_discovery_relay("wss://user.kindpag.es/").await; let _ = client.add_discovery_relay("wss://user.kindpag.es/").await;

View File

@@ -0,0 +1,2 @@
pub const KEYRING_SERVICE: &str = "Coop Safe Storage";
pub const APP_NAME: &str = "coop";

View File

@@ -1,12 +1,17 @@
use asset::Assets; use asset::Assets;
use client::NostrClient; use client::NostrClient;
use components::theme::{Theme, ThemeColor, ThemeMode}; use components::theme::{Theme, ThemeColor, ThemeMode};
use constants::{APP_NAME, KEYRING_SERVICE};
use gpui::*; use gpui::*;
use keyring::Entry;
use nostr_sdk::prelude::*;
use state::AppState; use state::AppState;
use std::sync::Arc; use std::sync::Arc;
use utils::get_all_accounts_from_keyring;
use views::app::AppView; use views::app::AppView;
pub mod asset; pub mod asset;
pub mod constants;
pub mod state; pub mod state;
pub mod utils; pub mod utils;
pub mod views; pub mod views;
@@ -45,6 +50,33 @@ async fn main() {
// Refresh // Refresh
cx.refresh(); cx.refresh();
// Login
let async_cx = cx.to_async();
cx.foreground_executor()
.spawn(async move {
let accounts = get_all_accounts_from_keyring();
if let Some(account) = accounts.first() {
let client = async_cx
.read_global(|nostr: &NostrClient, _cx| nostr.client)
.unwrap();
let entry =
Entry::new(KEYRING_SERVICE, account.to_bech32().unwrap().as_ref())
.unwrap();
let password = entry.get_password().unwrap();
let keys = Keys::parse(password).unwrap();
client.set_signer(keys).await;
async_cx
.update_global(|app_state: &mut AppState, _cx| {
app_state.signer = Some(*account);
})
.unwrap();
}
})
.detach();
// Set window size // Set window size
let bounds = Bounds::centered(None, size(px(860.0), px(650.0)), cx); let bounds = Bounds::centered(None, size(px(860.0), px(650.0)), cx);
@@ -53,7 +85,7 @@ async fn main() {
window_bounds: Some(WindowBounds::Windowed(bounds)), window_bounds: Some(WindowBounds::Windowed(bounds)),
window_decorations: Some(WindowDecorations::Client), window_decorations: Some(WindowDecorations::Client),
titlebar: Some(TitlebarOptions { titlebar: Some(TitlebarOptions {
title: Some(SharedString::new_static("coop")), title: Some(SharedString::new_static(APP_NAME)),
appears_transparent: true, appears_transparent: true,
..Default::default() ..Default::default()
}), }),

View File

@@ -3,6 +3,7 @@ use nostr_sdk::prelude::*;
pub struct AppState { pub struct AppState {
pub signer: Option<PublicKey>, pub signer: Option<PublicKey>,
// TODO: add more app state
} }
impl Global for AppState {} impl Global for AppState {}

View File

@@ -7,7 +7,7 @@ use gpui::*;
use keyring::Entry; use keyring::Entry;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use crate::state::AppState; use crate::{constants::KEYRING_SERVICE, state::AppState};
pub struct Onboarding { pub struct Onboarding {
input: View<TextInput>, input: View<TextInput>,
@@ -36,7 +36,7 @@ impl Onboarding {
let secret = keys.secret_key().to_secret_hex(); let secret = keys.secret_key().to_secret_hex();
let entry = let entry =
Entry::new("Coop Safe Storage", &public_key.to_bech32().unwrap()) Entry::new(KEYRING_SERVICE, &public_key.to_bech32().unwrap())
.unwrap(); .unwrap();
// Store private key to OS Keyring // Store private key to OS Keyring