Skip to content

Commit 34e87f1

Browse files
committed
Add signup command
1 parent 08f823c commit 34e87f1

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

src/command/admin.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn login(args: &LoginArgs) -> Result<()> {
1515
json!({"username": args.username, "password": args.password, "label": args.label}),
1616
)?;
1717
let res = res.result.unwrap();
18-
let api_key = res["api_key"].as_str().unwrap();
18+
let api_key = res["token"].as_str().unwrap();
1919
settings::put_str("password", api_key)?;
2020
println!("You are now logged in as {}", args.username);
2121
Ok(())
@@ -37,16 +37,40 @@ pub fn create_api_key(args: &CreateApiKeyArgs) -> Result<()> {
3737
}
3838

3939
#[derive(Args)]
40-
pub struct AddAdminArgs {
41-
pub new_admin_name: String,
42-
pub new_admin_password: String,
40+
pub struct AddUserArgs {
41+
pub name: String,
42+
pub password: String,
4343
}
4444

45-
pub fn add_admin(args: &AddAdminArgs) -> Result<()> {
45+
pub fn add_user(args: &AddUserArgs) -> Result<()> {
4646
rpc::call(
47-
"add_admin",
48-
json!({"new_admin_name": args.new_admin_name, "new_admin_password": args.new_admin_password}),
49-
)?.print()
47+
"add_user",
48+
json!({"name": args.name, "password": args.password}),
49+
)?
50+
.print()
51+
}
52+
53+
#[derive(Args)]
54+
pub struct SignupArgs {
55+
pub name: String,
56+
pub password: String,
57+
}
58+
59+
pub fn sign_up(args: &SignupArgs) -> Result<()> {
60+
rpc::call(
61+
"add_user",
62+
json!({"name": args.name, "password": args.password}),
63+
)?
64+
.print()?;
65+
let res = rpc::call(
66+
"create_api_key",
67+
json!({"username": args.name, "password": args.password, "label": "Created by btcmap-cli during signup"}),
68+
)?;
69+
let res = res.result.unwrap();
70+
let api_key = res["token"].as_str().unwrap();
71+
settings::put_str("password", api_key)?;
72+
println!("You are now logged in as {}", args.name);
73+
Ok(())
5074
}
5175

5276
#[derive(Args)]

src/command/event.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ use serde_json::{json, Map, Value};
77

88
#[derive(Args)]
99
pub struct CreateEventArgs {
10-
pub lat: i64,
11-
pub lon: i64,
10+
#[clap(allow_hyphen_values = true)]
11+
pub lat: f64,
12+
#[clap(allow_hyphen_values = true)]
13+
pub lon: f64,
1214
pub name: String,
1315
pub website: String,
1416
pub starts_at: String,

src/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ enum Commands {
5656
GenerateElementCategories(GenerateElementCategoriesArgs),
5757

5858
// Auth - https://github.com/teambtcmap/btcmap-api/blob/master/docs/rpc-api/auth.md
59+
/// Sign up and get an auth token
60+
Signup(command::admin::SignupArgs),
5961
/// Change admin password. Knowledge of an old password is required
6062
ChangePassword(command::admin::ChangePasswordArgs),
6163
/// Create API key. You need to provide your username and password, as well as a key label
6264
CreateApiKey(command::admin::CreateApiKeyArgs),
6365
/// Login with your username and password and get an auth token
6466
Login(command::admin::LoginArgs),
6567
/// Create a new admin user. New admins have no permissions by default, use add-admin-action to allow certain acitons
66-
AddAdmin(command::admin::AddAdminArgs),
68+
AddUser(command::admin::AddUserArgs),
6769
/// Allow other admin to perform a certain action. You must be super admin to use this command
6870
AddAdminAction(command::admin::AddAdminActionArgs),
6971
/// Block other admin from using a certain action. You must be super admin to use this command
@@ -171,7 +173,8 @@ fn main() -> Result<()> {
171173
Commands::GenerateElementIcons(args) => element::generate_element_icons(args),
172174
Commands::GenerateElementCategories(args) => element::generate_element_categories(args),
173175
// Admin
174-
Commands::AddAdmin(args) => command::admin::add_admin(args),
176+
Commands::Signup(args) => command::admin::sign_up(args),
177+
Commands::AddUser(args) => command::admin::add_user(args),
175178
Commands::AddAdminAction(args) => command::admin::add_admin_action(args),
176179
Commands::RemoveAdminAction(args) => command::admin::remove_admin_action(args),
177180
Commands::GenerateInvoice(args) => command::admin::generate_invoice(args),

0 commit comments

Comments
 (0)