1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use super::{consts::CREDENTIALS_FILE, errors::ApiError};
use common::environment::get_user_dot_grafbase_path;
use std::fs;

/// Deletes the login credentials file
///
/// # Errors
///
/// - returns [`BackendError::NotLoggedIn`] if the user is not logged in when attempting to log out
///
/// - returns [`BackendError::DeleteCredentialsFile`] if ~/.grafbase/credentials.json could not be deleted
///
/// - returns [`BackendError::ReadCredentialsFile`] if ~/.grafbase/credentials.json could not be read
pub fn logout() -> Result<(), ApiError> {
    let user_dot_grafbase_path = get_user_dot_grafbase_path().ok_or(ApiError::NotLoggedIn)?;

    let credentials_path = user_dot_grafbase_path.join(CREDENTIALS_FILE);

    match credentials_path.try_exists() {
        Ok(true) => fs::remove_file(credentials_path).map_err(ApiError::DeleteCredentialsFile),
        Ok(false) => Err(ApiError::NotLoggedIn),
        Err(error) => Err(ApiError::ReadCredentialsFile(error)),
    }
}