aboutsummaryrefslogtreecommitdiffstats
path: root/crates/turtle/src/command/client/account/register.rs
blob: 548c27391acfb8aecdd790958c6d11c64389a498 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use clap::Parser;
use eyre::{Result, bail};

use super::login::or_user_input;
use crate::atuin_client::settings::{Settings, SyncAuth};

#[derive(Parser, Debug)]
pub struct Cmd {
    #[clap(long, short)]
    pub username: Option<String>,

    #[clap(long, short)]
    pub password: Option<String>,

    #[clap(long, short)]
    pub email: Option<String>,
}

impl Cmd {
    pub async fn run(&self, settings: &Settings) -> Result<()> {
        match settings.resolve_sync_auth().await {
            SyncAuth::Legacy { .. } => {
                println!("You are already logged in.");
                println!("Run 'atuin logout' to log out.");
                return Ok(());
            }

            SyncAuth::NotLoggedIn { .. } => {}
        }

        // Legacy registration flow
        println!("Registering for an Atuin Sync account");

        let username = or_user_input(self.username.clone(), "username");
        let email = or_user_input(self.email.clone(), "email");
        let password = self
            .password
            .clone()
            .unwrap_or_else(super::login::read_user_password);

        if password.is_empty() {
            bail!("please provide a password");
        }

        let session = crate::atuin_client::api_client::register(
            settings.sync_address.as_str(),
            &username,
            &email,
            &password,
        )
        .await?;

        let meta = Settings::meta_store().await?;
        meta.save_session(&session.session).await?;

        let _key = crate::atuin_client::encryption::load_key(settings)?;

        println!(
            "Registration successful! Please make a note of your key (run 'atuin key') and keep it safe."
        );
        println!(
            "You will need it to log in on other devices, and we cannot help recover it if you lose it."
        );

        Ok(())
    }
}