Fix bug in auth.rs
This commit is contained in:
@@ -10,13 +10,13 @@ pub struct AuthUser {
|
|||||||
pub user: User,
|
pub user: User,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> FromRequestParts<S> for AuthUser
|
impl FromRequestParts<DbPool> for AuthUser {
|
||||||
where
|
|
||||||
S: Send + Sync,
|
|
||||||
{
|
|
||||||
type Rejection = StatusCode;
|
type Rejection = StatusCode;
|
||||||
|
|
||||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
async fn from_request_parts(
|
||||||
|
parts: &mut Parts,
|
||||||
|
state: &DbPool,
|
||||||
|
) -> Result<Self, Self::Rejection> {
|
||||||
let auth_header = parts
|
let auth_header = parts
|
||||||
.headers
|
.headers
|
||||||
.get(AUTHORIZATION)
|
.get(AUTHORIZATION)
|
||||||
@@ -28,12 +28,8 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
let token = &auth_header[7..];
|
let token = &auth_header[7..];
|
||||||
let pool = parts
|
|
||||||
.extensions
|
|
||||||
.get::<DbPool>()
|
|
||||||
.ok_or(StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
|
|
||||||
let user = AuthController::authenticate_user(pool, token)
|
let user = AuthController::authenticate_user(state, token)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| StatusCode::UNAUTHORIZED)?;
|
.map_err(|_| StatusCode::UNAUTHORIZED)?;
|
||||||
|
|
||||||
@@ -47,14 +43,14 @@ pub struct AdminUser {
|
|||||||
pub user: User,
|
pub user: User,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> FromRequestParts<S> for AdminUser
|
impl FromRequestParts<DbPool> for AdminUser {
|
||||||
where
|
|
||||||
S: Send + Sync,
|
|
||||||
{
|
|
||||||
type Rejection = StatusCode;
|
type Rejection = StatusCode;
|
||||||
|
|
||||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
async fn from_request_parts(
|
||||||
let auth_user = AuthUser::from_request_parts(parts, _state).await?;
|
parts: &mut Parts,
|
||||||
|
state: &DbPool,
|
||||||
|
) -> Result<Self, Self::Rejection> {
|
||||||
|
let auth_user = AuthUser::from_request_parts(parts, state).await?;
|
||||||
|
|
||||||
if auth_user.user.role != UserRole::Admin {
|
if auth_user.user.role != UserRole::Admin {
|
||||||
return Err(StatusCode::FORBIDDEN);
|
return Err(StatusCode::FORBIDDEN);
|
||||||
|
Reference in New Issue
Block a user