diff options
| -rw-r--r-- | src/bin/ngit/sub_commands/login.rs | 39 | ||||
| -rw-r--r-- | src/lib/login/fresh.rs | 18 | ||||
| -rw-r--r-- | src/lib/login/mod.rs | 2 |
3 files changed, 42 insertions, 17 deletions
diff --git a/src/bin/ngit/sub_commands/login.rs b/src/bin/ngit/sub_commands/login.rs index bdd215f..390f431 100644 --- a/src/bin/ngit/sub_commands/login.rs +++ b/src/bin/ngit/sub_commands/login.rs | |||
| @@ -2,7 +2,7 @@ use anyhow::{Context, Result}; | |||
| 2 | use clap; | 2 | use clap; |
| 3 | 3 | ||
| 4 | use crate::{ | 4 | use crate::{ |
| 5 | cli::Cli, | 5 | cli::{extract_signer_cli_arguments, Cli}, |
| 6 | client::{Client, Connect}, | 6 | client::{Client, Connect}, |
| 7 | git::Repo, | 7 | git::Repo, |
| 8 | login::fresh::fresh_login_or_signup, | 8 | login::fresh::fresh_login_or_signup, |
| @@ -19,24 +19,33 @@ pub struct SubCommandArgs { | |||
| 19 | offline: bool, | 19 | offline: bool, |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | pub async fn launch(_args: &Cli, command_args: &SubCommandArgs) -> Result<()> { | 22 | pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { |
| 23 | // TODO show existing login on record, prompt to logout | 23 | // TODO show existing login on record, prompt to logout |
| 24 | // TODO use cli arguments to login | ||
| 25 | let git_repo_result = Repo::discover().context("cannot find a git repository"); | ||
| 26 | 24 | ||
| 27 | if command_args.offline { | 25 | let client = if command_args.offline { |
| 28 | if let Ok(git_repo) = git_repo_result { | 26 | None |
| 29 | fresh_login_or_signup(&Some(&git_repo), None, command_args.local).await?; | ||
| 30 | } else { | ||
| 31 | fresh_login_or_signup(&None, None, command_args.local).await?; | ||
| 32 | } | ||
| 33 | } else { | 27 | } else { |
| 34 | let client = Client::default(); | 28 | Some(Client::default()) |
| 35 | if let Ok(git_repo) = git_repo_result { | 29 | }; |
| 36 | fresh_login_or_signup(&Some(&git_repo), Some(&client), command_args.local).await?; | 30 | |
| 37 | } else { | 31 | let git_repo_result = Repo::discover().context("cannot find a git repository"); |
| 38 | fresh_login_or_signup(&None, Some(&client), command_args.local).await?; | 32 | let git_repo_option = { |
| 33 | match git_repo_result { | ||
| 34 | Ok(git_repo) => Some(git_repo), | ||
| 35 | Err(_) => None, | ||
| 39 | } | 36 | } |
| 37 | }; | ||
| 38 | |||
| 39 | fresh_login_or_signup( | ||
| 40 | &git_repo_option.as_ref(), | ||
| 41 | client.as_ref(), | ||
| 42 | extract_signer_cli_arguments(args)?, | ||
| 43 | command_args.local, | ||
| 44 | ) | ||
| 45 | .await?; | ||
| 46 | |||
| 47 | // If not offline, disconnect the client | ||
| 48 | if let Some(client) = client { | ||
| 40 | client.disconnect().await?; | 49 | client.disconnect().await?; |
| 41 | } | 50 | } |
| 42 | Ok(()) | 51 | Ok(()) |
diff --git a/src/lib/login/fresh.rs b/src/lib/login/fresh.rs index 524f419..2190844 100644 --- a/src/lib/login/fresh.rs +++ b/src/lib/login/fresh.rs | |||
| @@ -10,6 +10,7 @@ use qrcode::QrCode; | |||
| 10 | use tokio::{signal, sync::Mutex}; | 10 | use tokio::{signal, sync::Mutex}; |
| 11 | 11 | ||
| 12 | use super::{ | 12 | use super::{ |
| 13 | existing::load_existing_login, | ||
| 13 | key_encryption::decrypt_key, | 14 | key_encryption::decrypt_key, |
| 14 | print_logged_in_as, | 15 | print_logged_in_as, |
| 15 | user::{get_user_details, UserRef}, | 16 | user::{get_user_details, UserRef}, |
| @@ -24,7 +25,7 @@ use crate::{ | |||
| 24 | Interactor, InteractorPrompt, Printer, PromptChoiceParms, PromptConfirmParms, | 25 | Interactor, InteractorPrompt, Printer, PromptChoiceParms, PromptConfirmParms, |
| 25 | PromptInputParms, PromptPasswordParms, | 26 | PromptInputParms, PromptPasswordParms, |
| 26 | }, | 27 | }, |
| 27 | client::{send_events, Connect}, | 28 | client::{fetch_public_key, send_events, Connect}, |
| 28 | git::{remove_git_config_item, save_git_config_item, Repo, RepoActions}, | 29 | git::{remove_git_config_item, save_git_config_item, Repo, RepoActions}, |
| 29 | }; | 30 | }; |
| 30 | 31 | ||
| @@ -32,9 +33,24 @@ pub async fn fresh_login_or_signup( | |||
| 32 | git_repo: &Option<&Repo>, | 33 | git_repo: &Option<&Repo>, |
| 33 | #[cfg(test)] client: Option<&MockConnect>, | 34 | #[cfg(test)] client: Option<&MockConnect>, |
| 34 | #[cfg(not(test))] client: Option<&Client>, | 35 | #[cfg(not(test))] client: Option<&Client>, |
| 36 | signer_info: Option<SignerInfo>, | ||
| 35 | save_local: bool, | 37 | save_local: bool, |
| 36 | ) -> Result<(Arc<dyn NostrSigner>, UserRef, SignerInfoSource)> { | 38 | ) -> Result<(Arc<dyn NostrSigner>, UserRef, SignerInfoSource)> { |
| 37 | let (signer, public_key, signer_info, source) = loop { | 39 | let (signer, public_key, signer_info, source) = loop { |
| 40 | if let Some(signer_info) = signer_info { | ||
| 41 | let (signer, _user_ref, source) = load_existing_login( | ||
| 42 | git_repo, | ||
| 43 | &Some(signer_info.clone()), | ||
| 44 | &None, | ||
| 45 | &Some(SignerInfoSource::CommandLineArguments), | ||
| 46 | client, | ||
| 47 | true, | ||
| 48 | true, | ||
| 49 | ) | ||
| 50 | .await?; | ||
| 51 | let public_key = fetch_public_key(&signer).await?; | ||
| 52 | break (signer, public_key, signer_info, source); | ||
| 53 | } | ||
| 38 | match Interactor::default().choice( | 54 | match Interactor::default().choice( |
| 39 | PromptChoiceParms::default() | 55 | PromptChoiceParms::default() |
| 40 | .with_prompt("login to nostr") | 56 | .with_prompt("login to nostr") |
diff --git a/src/lib/login/mod.rs b/src/lib/login/mod.rs index b45bc1d..cf62d85 100644 --- a/src/lib/login/mod.rs +++ b/src/lib/login/mod.rs | |||
| @@ -30,7 +30,7 @@ pub async fn login_or_signup( | |||
| 30 | if res.is_ok() { | 30 | if res.is_ok() { |
| 31 | res | 31 | res |
| 32 | } else { | 32 | } else { |
| 33 | fresh_login_or_signup(git_repo, client, false).await | 33 | fresh_login_or_signup(git_repo, client, None, false).await |
| 34 | } | 34 | } |
| 35 | } | 35 | } |
| 36 | 36 | ||