spacetimedb_cli/subcommands/
logout.rs

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
use crate::Config;
use clap::{Arg, ArgMatches, Command};
use reqwest::Url;

pub fn cli() -> Command {
    Command::new("logout").arg(
        Arg::new("auth-host")
            .long("auth-host")
            .default_value("https://spacetimedb.com")
            .help("Log out from a custom auth server"),
    )
}

pub async fn exec(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::Error> {
    let host: &String = args.get_one("auth-host").unwrap();
    let host = Url::parse(host)?;

    if let Some(web_session_token) = config.web_session_token() {
        let client = reqwest::Client::new();
        client
            .post(host.join("auth/cli/logout")?)
            .header("Authorization", format!("Bearer {}", web_session_token))
            .send()
            .await?;
    }

    config.clear_login_tokens();
    config.save();

    Ok(())
}