spacetimedb_cli/subcommands/
logout.rs

1use crate::Config;
2use clap::{Arg, ArgMatches, Command};
3use reqwest::Url;
4
5pub fn cli() -> Command {
6    Command::new("logout").arg(
7        Arg::new("auth-host")
8            .long("auth-host")
9            .default_value("https://spacetimedb.com")
10            .help("Log out from a custom auth server"),
11    )
12}
13
14pub async fn exec(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::Error> {
15    let host: &String = args.get_one("auth-host").unwrap();
16    let host = Url::parse(host)?;
17
18    if let Some(web_session_token) = config.web_session_token() {
19        let client = reqwest::Client::new();
20        client
21            .post(host.join("auth/cli/logout")?)
22            .header("Authorization", format!("Bearer {web_session_token}"))
23            .send()
24            .await?;
25    }
26
27    config.clear_login_tokens();
28    config.save();
29
30    Ok(())
31}