grafbase_local_backend/api/
logout.rs

1use super::{consts::CREDENTIALS_FILE, errors::ApiError};
2use common::environment::get_user_dot_grafbase_path;
3use std::fs;
4
5/// Deletes the login credentials file
6///
7/// # Errors
8///
9/// - returns [`BackendError::NotLoggedIn`] if the user is not logged in when attempting to log out
10///
11/// - returns [`BackendError::DeleteCredentialsFile`] if ~/.grafbase/credentials.json could not be deleted
12///
13/// - returns [`BackendError::ReadCredentialsFile`] if ~/.grafbase/credentials.json could not be read
14pub fn logout() -> Result<(), ApiError> {
15    let user_dot_grafbase_path = get_user_dot_grafbase_path().ok_or(ApiError::NotLoggedIn)?;
16
17    let credentials_path = user_dot_grafbase_path.join(CREDENTIALS_FILE);
18
19    match credentials_path.try_exists() {
20        Ok(true) => fs::remove_file(credentials_path).map_err(ApiError::DeleteCredentialsFile),
21        Ok(false) => Err(ApiError::NotLoggedIn),
22        Err(error) => Err(ApiError::ReadCredentialsFile(error)),
23    }
24}