2024-01-19 16:21:43 +00:00
|
|
|
use crate::{local_user_view_from_jwt, read_auth_token};
|
2023-10-17 15:25:48 +00:00
|
|
|
use actix_web::{
|
|
|
|
web::{Data, Json},
|
|
|
|
HttpRequest,
|
|
|
|
};
|
2024-01-19 16:21:43 +00:00
|
|
|
use lemmy_api_common::{context::LemmyContext, SuccessResponse};
|
2024-04-10 14:14:11 +00:00
|
|
|
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
|
2023-10-17 15:25:48 +00:00
|
|
|
|
|
|
|
/// 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.
|
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn validate_auth(
|
|
|
|
req: HttpRequest,
|
|
|
|
context: Data<LemmyContext>,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<Json<SuccessResponse>> {
|
2023-10-17 15:25:48 +00:00
|
|
|
let jwt = read_auth_token(&req)?;
|
|
|
|
if let Some(jwt) = jwt {
|
2024-01-19 16:21:43 +00:00
|
|
|
local_user_view_from_jwt(&jwt, &context).await?;
|
2023-10-17 15:25:48 +00:00
|
|
|
} else {
|
|
|
|
Err(LemmyErrorType::NotLoggedIn)?;
|
|
|
|
}
|
|
|
|
Ok(Json(SuccessResponse::default()))
|
|
|
|
}
|