Skip to content

How do I make all the controller routes for authorized users only? #1095

Answered by zhuravkov
zhuravkov asked this question in Q&A
Discussion options

You must be logged in to vote

I did it this way, if there is a better way, please correct

use axum::{
    extract::Request,
    http::{HeaderMap, StatusCode},
    middleware::Next,
    response::Response,
};
use loco_rs::auth::jwt;

use loco_rs::prelude::*;

use crate::models::users;
pub async fn auth(
    State(ctx): State<AppContext>,
    headers: HeaderMap,
    mut request: Request,
    next: Next,

) -> Result<Response, StatusCode> {
    let token = loco_rs::controller::middleware::auth::extract_token_from_header(&headers).map_err(|_| StatusCode::UNAUTHORIZED)?;
    let jwt_secret = ctx.config.get_jwt_config()
        .map_err(|_| StatusCode::UNAUTHORIZED)?;
    let jwt  = jwt::JWT::new(&jwt_secret.secret);

    m…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by zhuravkov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant