chore: add logs (#138)
This commit is contained in:
parent
67b3741a3d
commit
0b9d7e6d6b
|
|
@ -29,10 +29,10 @@ use shared_entity::dto::workspace_dto::{
|
||||||
use shared_entity::error_code::url_missing_param;
|
use shared_entity::error_code::url_missing_param;
|
||||||
use shared_entity::error_code::ErrorCode;
|
use shared_entity::error_code::ErrorCode;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::SystemTime;
|
use std::time::{Duration, SystemTime};
|
||||||
use tokio::fs::File;
|
use tokio::fs::File;
|
||||||
use tokio::io::AsyncReadExt;
|
use tokio::io::AsyncReadExt;
|
||||||
use tracing::instrument;
|
use tracing::{event, instrument};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use gotrue_entity::dto::SignUpResponse::{Authenticated, NotAuthenticated};
|
use gotrue_entity::dto::SignUpResponse::{Authenticated, NotAuthenticated};
|
||||||
|
|
@ -81,7 +81,7 @@ impl Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(level = "debug", skip_all, err)]
|
#[instrument(level = "debug", skip_all, err)]
|
||||||
pub fn set_token(&self, token: &str) -> Result<(), AppError> {
|
pub fn restore_token(&self, token: &str) -> Result<(), AppError> {
|
||||||
if token.is_empty() {
|
if token.is_empty() {
|
||||||
return Err(AppError::new(ErrorCode::OAuthError, "Empty token"));
|
return Err(AppError::new(ErrorCode::OAuthError, "Empty token"));
|
||||||
}
|
}
|
||||||
|
|
@ -321,7 +321,15 @@ impl Client {
|
||||||
pub fn token_expires_at(&self) -> Result<i64, AppError> {
|
pub fn token_expires_at(&self) -> Result<i64, AppError> {
|
||||||
match &self.token.try_read() {
|
match &self.token.try_read() {
|
||||||
None => Err(AppError::new(ErrorCode::Unhandled, "Failed to read token")),
|
None => Err(AppError::new(ErrorCode::Unhandled, "Failed to read token")),
|
||||||
Some(token) => Ok(token.as_ref().ok_or(ErrorCode::NotLoggedIn)?.expires_at),
|
Some(token) => Ok(
|
||||||
|
token
|
||||||
|
.as_ref()
|
||||||
|
.ok_or(AppError::new(
|
||||||
|
ErrorCode::NotLoggedIn,
|
||||||
|
"fail to get expires_at",
|
||||||
|
))?
|
||||||
|
.expires_at,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -334,12 +342,15 @@ impl Client {
|
||||||
/// - `Err(AppError)`: An `AppError` indicating either an inability to read the token or that the user is not logged in.
|
/// - `Err(AppError)`: An `AppError` indicating either an inability to read the token or that the user is not logged in.
|
||||||
///
|
///
|
||||||
pub fn access_token(&self) -> Result<String, AppError> {
|
pub fn access_token(&self) -> Result<String, AppError> {
|
||||||
match &self.token.try_read() {
|
match &self.token.try_read_for(Duration::from_secs(2)) {
|
||||||
None => Err(AppError::new(ErrorCode::Unhandled, "Failed to read token")),
|
None => Err(AppError::new(ErrorCode::Unhandled, "Failed to read token")),
|
||||||
Some(token) => Ok(
|
Some(token) => Ok(
|
||||||
token
|
token
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or(ErrorCode::NotLoggedIn)?
|
.ok_or(AppError::new(
|
||||||
|
ErrorCode::NotLoggedIn,
|
||||||
|
"fail to get access token. Token is empty",
|
||||||
|
))?
|
||||||
.access_token
|
.access_token
|
||||||
.clone(),
|
.clone(),
|
||||||
),
|
),
|
||||||
|
|
@ -524,7 +535,10 @@ impl Client {
|
||||||
.token
|
.token
|
||||||
.read()
|
.read()
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or(AppError::new(ErrorCode::NotLoggedIn, "No access token"))?
|
.ok_or(AppError::new(
|
||||||
|
ErrorCode::NotLoggedIn,
|
||||||
|
"fail to refresh user token",
|
||||||
|
))?
|
||||||
.refresh_token
|
.refresh_token
|
||||||
.as_str()
|
.as_str()
|
||||||
.to_owned();
|
.to_owned();
|
||||||
|
|
@ -538,6 +552,7 @@ impl Client {
|
||||||
Ok(())
|
Ok(())
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
event!(tracing::Level::ERROR, "refresh token failed: {}", err);
|
||||||
self.token.write().unset();
|
self.token.write().unset();
|
||||||
Err(AppError::from(err))
|
Err(AppError::from(err))
|
||||||
},
|
},
|
||||||
|
|
@ -561,6 +576,7 @@ impl Client {
|
||||||
#[instrument(level = "debug", skip_all, err)]
|
#[instrument(level = "debug", skip_all, err)]
|
||||||
pub async fn sign_out(&self) -> Result<(), AppError> {
|
pub async fn sign_out(&self) -> Result<(), AppError> {
|
||||||
self.gotrue_client.logout(&self.access_token()?).await?;
|
self.gotrue_client.logout(&self.access_token()?).await?;
|
||||||
|
self.token.write().unset();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,13 +41,13 @@ impl ClientToken {
|
||||||
///
|
///
|
||||||
/// - `token`: The new `AccessTokenResponse` to be set.
|
/// - `token`: The new `AccessTokenResponse` to be set.
|
||||||
pub(crate) fn set(&mut self, new_token: AccessTokenResponse) {
|
pub(crate) fn set(&mut self, new_token: AccessTokenResponse) {
|
||||||
tracing::trace!("Set new access token: {:?}", new_token);
|
|
||||||
let is_new = match &self.token {
|
let is_new = match &self.token {
|
||||||
None => true,
|
None => true,
|
||||||
Some(old_token) => old_token.access_token != new_token.access_token,
|
Some(old_token) => old_token.access_token != new_token.access_token,
|
||||||
};
|
};
|
||||||
self.token = Some(new_token);
|
self.token = Some(new_token);
|
||||||
if is_new {
|
if is_new {
|
||||||
|
tracing::trace!("Set new access token: {:?}", self.token);
|
||||||
let _ = self.sender.send(TokenState::Refresh);
|
let _ = self.sender.send(TokenState::Refresh);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue