supabase_auth_redux/
logout.rs

1use tracing::{debug, error, instrument, trace_span, Instrument};
2
3use crate::util::handle_response_code;
4use crate::{AuthClient, AuthError};
5
6impl AuthClient {
7    /// Logs out a user by invalidating their authentication token
8    ///
9    /// This method revokes the provided access token, effectively logging the user out.
10    /// After calling this method, the token will no longer be valid for authentication.
11    ///
12    /// # Arguments
13    ///
14    /// * `token` - The access token to invalidate
15    ///
16    /// # Errors
17    ///
18    /// Returns `AuthError::Http` if the API request fails.
19    ///
20    /// # Example
21    ///
22    /// ```rust,no_run
23    /// # use supabase_auth_redux::AuthClient;
24    /// # async fn example() -> Result<(), supabase_auth_redux::AuthError> {
25    /// let client = AuthClient::new("https://your-project.supabase.co", "your-anon-key")?;
26    ///
27    /// // After user signs in and you have their access token
28    /// let access_token = "user-access-token";
29    /// client.logout(access_token).await?;
30    ///
31    /// // The token is now invalid and cannot be used for authentication
32    /// # Ok(())
33    /// # }
34    /// ```
35    #[instrument(skip_all)]
36    pub async fn logout(&self, token: &str) -> Result<(), AuthError> {
37        let resp = match self
38            .http_client
39            .post(format!("{}/auth/v1/{}", self.supabase_api_url, "logout"))
40            .bearer_auth(token)
41            .header("apiKey", &self.supabase_anon_key)
42            .send()
43            .instrument(trace_span!("gotrue logout user"))
44            .await
45        {
46            Ok(resp) => resp,
47            Err(e) => {
48                error!("{}", e);
49                return Err(AuthError::Http);
50            }
51        };
52
53        let resp_code_result = handle_response_code(resp.status()).await;
54        let resp_text = match resp.text().await {
55            Ok(resp_text) => resp_text,
56            Err(e) => {
57                log::error!("{}", e);
58                return Err(AuthError::Http);
59            }
60        };
61        debug!("resp_text: {}", resp_text);
62        resp_code_result?;
63
64        Ok(())
65    }
66}