use actix_web::{
  web::{Data, Json},
  HttpRequest,
};
use lemmy_api_common::{
  context::LemmyContext,
  utils::{local_user_view_from_jwt, read_auth_token},
  SuccessResponse,
};
use lemmy_utils::error::{LemmyErrorType, LemmyResult};

/// Returns an error message if the auth token is invalid for any reason. Necessary because other
/// endpoints silently treat any call with invalid auth as unauthenticated.
pub async fn validate_auth(
  req: HttpRequest,
  context: Data<LemmyContext>,
) -> LemmyResult<Json<SuccessResponse>> {
  let jwt = read_auth_token(&req)?;
  if let Some(jwt) = jwt {
    local_user_view_from_jwt(&jwt, &context).await?;
  } else {
    Err(LemmyErrorType::NotLoggedIn)?;
  }
  Ok(Json(SuccessResponse::default()))
}
