feat: login/register no longer blocking (#216)
This commit is contained in:
parent
9e6746a4d1
commit
4bdf4c40c2
4 changed files with 26 additions and 25 deletions
|
@ -26,7 +26,7 @@ pub struct Client<'a> {
|
||||||
client: reqwest::Client,
|
client: reqwest::Client,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn register(
|
pub async fn register(
|
||||||
address: &str,
|
address: &str,
|
||||||
username: &str,
|
username: &str,
|
||||||
email: &str,
|
email: &str,
|
||||||
|
@ -45,36 +45,38 @@ pub fn register(
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = format!("{}/register", address);
|
let url = format!("{}/register", address);
|
||||||
let client = reqwest::blocking::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let resp = client
|
let resp = client
|
||||||
.post(url)
|
.post(url)
|
||||||
.header(USER_AGENT, APP_USER_AGENT)
|
.header(USER_AGENT, APP_USER_AGENT)
|
||||||
.json(&map)
|
.json(&map)
|
||||||
.send()?;
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
if !resp.status().is_success() {
|
if !resp.status().is_success() {
|
||||||
return Err(eyre!("failed to register user"));
|
return Err(eyre!("failed to register user"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let session = resp.json::<RegisterResponse>()?;
|
let session = resp.json::<RegisterResponse>().await?;
|
||||||
Ok(session)
|
Ok(session)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn login(address: &str, req: LoginRequest) -> Result<LoginResponse<'static>> {
|
pub async fn login(address: &str, req: LoginRequest<'_>) -> Result<LoginResponse<'static>> {
|
||||||
let url = format!("{}/login", address);
|
let url = format!("{}/login", address);
|
||||||
let client = reqwest::blocking::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
let resp = client
|
let resp = client
|
||||||
.post(url)
|
.post(url)
|
||||||
.header(USER_AGENT, APP_USER_AGENT)
|
.header(USER_AGENT, APP_USER_AGENT)
|
||||||
.json(&req)
|
.json(&req)
|
||||||
.send()?;
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
if resp.status() != reqwest::StatusCode::OK {
|
if resp.status() != reqwest::StatusCode::OK {
|
||||||
return Err(eyre!("invalid login details"));
|
return Err(eyre!("invalid login details"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let session = resp.json::<LoginResponse>()?;
|
let session = resp.json::<LoginResponse>().await?;
|
||||||
Ok(session)
|
Ok(session)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::prelude::*;
|
|
||||||
use std::{borrow::Cow, fs::File};
|
|
||||||
|
|
||||||
use atuin_common::api::LoginRequest;
|
use atuin_common::api::LoginRequest;
|
||||||
use eyre::Result;
|
use eyre::Result;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
use tokio::{fs::File, io::AsyncWriteExt};
|
||||||
|
|
||||||
use atuin_client::api_client;
|
use atuin_client::api_client;
|
||||||
use atuin_client::settings::Settings;
|
use atuin_client::settings::Settings;
|
||||||
|
@ -29,7 +29,7 @@ fn get_input() -> Result<String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cmd {
|
impl Cmd {
|
||||||
pub fn run(&self, settings: &Settings) -> Result<()> {
|
pub async fn run(&self, settings: &Settings) -> Result<()> {
|
||||||
let session_path = atuin_common::utils::data_dir().join("session");
|
let session_path = atuin_common::utils::data_dir().join("session");
|
||||||
|
|
||||||
if session_path.exists() {
|
if session_path.exists() {
|
||||||
|
@ -47,15 +47,16 @@ impl Cmd {
|
||||||
let session = api_client::login(
|
let session = api_client::login(
|
||||||
settings.sync_address.as_str(),
|
settings.sync_address.as_str(),
|
||||||
LoginRequest { username, password },
|
LoginRequest { username, password },
|
||||||
)?;
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
let session_path = settings.session_path.as_str();
|
let session_path = settings.session_path.as_str();
|
||||||
let mut file = File::create(session_path)?;
|
let mut file = File::create(session_path).await?;
|
||||||
file.write_all(session.session.as_bytes())?;
|
file.write_all(session.session.as_bytes()).await?;
|
||||||
|
|
||||||
let key_path = settings.key_path.as_str();
|
let key_path = settings.key_path.as_str();
|
||||||
let mut file = File::create(key_path)?;
|
let mut file = File::create(key_path).await?;
|
||||||
file.write_all(key.as_bytes())?;
|
file.write_all(key.as_bytes()).await?;
|
||||||
|
|
||||||
println!("Logged in!");
|
println!("Logged in!");
|
||||||
|
|
||||||
|
|
|
@ -142,13 +142,13 @@ impl AtuinCmd {
|
||||||
}
|
}
|
||||||
|
|
||||||
Self::Sync { force } => sync::run(&client_settings, force, &mut db).await,
|
Self::Sync { force } => sync::run(&client_settings, force, &mut db).await,
|
||||||
Self::Login(l) => l.run(&client_settings),
|
Self::Login(l) => l.run(&client_settings).await,
|
||||||
Self::Logout => {
|
Self::Logout => {
|
||||||
logout::run();
|
logout::run();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
Self::Register(r) => {
|
Self::Register(r) => {
|
||||||
register::run(&client_settings, &r.username, &r.email, &r.password)
|
register::run(&client_settings, &r.username, &r.email, &r.password).await
|
||||||
}
|
}
|
||||||
Self::Key => {
|
Self::Key => {
|
||||||
use atuin_client::encryption::{encode_key, load_key};
|
use atuin_client::encryption::{encode_key, load_key};
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
use std::fs::File;
|
|
||||||
use std::io::prelude::*;
|
|
||||||
|
|
||||||
use eyre::Result;
|
use eyre::Result;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
use tokio::{fs::File, io::AsyncWriteExt};
|
||||||
|
|
||||||
use atuin_client::api_client;
|
use atuin_client::api_client;
|
||||||
use atuin_client::settings::Settings;
|
use atuin_client::settings::Settings;
|
||||||
|
@ -20,7 +18,7 @@ pub struct Cmd {
|
||||||
pub password: Option<String>,
|
pub password: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(
|
pub async fn run(
|
||||||
settings: &Settings,
|
settings: &Settings,
|
||||||
username: &Option<String>,
|
username: &Option<String>,
|
||||||
email: &Option<String>,
|
email: &Option<String>,
|
||||||
|
@ -32,11 +30,11 @@ pub fn run(
|
||||||
let password = or_user_input(password, "password");
|
let password = or_user_input(password, "password");
|
||||||
|
|
||||||
let session =
|
let session =
|
||||||
api_client::register(settings.sync_address.as_str(), &username, &email, &password)?;
|
api_client::register(settings.sync_address.as_str(), &username, &email, &password).await?;
|
||||||
|
|
||||||
let path = settings.session_path.as_str();
|
let path = settings.session_path.as_str();
|
||||||
let mut file = File::create(path)?;
|
let mut file = File::create(path).await?;
|
||||||
file.write_all(session.session.as_bytes())?;
|
file.write_all(session.session.as_bytes()).await?;
|
||||||
|
|
||||||
// Create a new key, and save it to disk
|
// Create a new key, and save it to disk
|
||||||
let _key = atuin_client::encryption::new_key(settings)?;
|
let _key = atuin_client::encryption::new_key(settings)?;
|
||||||
|
|
Loading…
Reference in a new issue