Skip to content

Commit

Permalink
Trying to fix not returning errors
Browse files Browse the repository at this point in the history
  • Loading branch information
GPeaky committed Dec 11, 2023
1 parent 5f4f93b commit 1a47728
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/middlewares/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ where
ctx: ServiceCtx<'a, Self>,
) -> Self::Future<'_> {
let Some(user) = req.extensions().get::<UserExtension>().cloned() else {
return Box::pin(async { Err(UserError::Unauthorized)? });
return Box::pin(async { Err(web::Error::from(UserError::Unauthorized))? });
};

if user.role != Role::Admin {
return Box::pin(async { Err(UserError::Unauthorized)? });
return Box::pin(async { Err(web::Error::from(UserError::Unauthorized))? });
}

Box::pin(ctx.call(&self.service, req))
Expand Down
12 changes: 7 additions & 5 deletions src/middlewares/authenticated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ where
let header = req.headers().get("Authorization").cloned();

let fut = async move {
let state = state.ok_or(CommonError::InternalServerError)?;
let state = state.ok_or(web::Error::from(CommonError::InternalServerError))?;
let header = {
let header = header.ok_or(TokenError::MissingToken)?;
let header_str = header.to_str().map_err(|_| TokenError::InvalidToken)?;
let header = header.ok_or(web::Error::from(TokenError::MissingToken))?;
let header_str = header
.to_str()
.map_err(|_| web::Error::from(TokenError::InvalidToken))?;

if !header_str.starts_with(BEARER_PREFIX) {
return Err(TokenError::InvalidToken.into());
return Err(web::Error::from(TokenError::InvalidToken));
}

header_str[BEARER_PREFIX.len()..].to_string()
Expand All @@ -64,7 +66,7 @@ where
.user_repository
.find(&id)
.await?
.ok_or(UserError::NotFound)?;
.ok_or(web::Error::from(UserError::NotFound))?;

if !user.active {
return Err(web::Error::from(UserError::NotVerified));
Expand Down

0 comments on commit 1a47728

Please sign in to comment.