use salvo::{async_trait, Depot, FlowCtrl, Handler, Request, Response, Writer};

use infra::{status::api_err::ApiErr, util::iden::Identity};

#[derive(Default)]
pub struct Auth;

impl Auth {
    #[inline]
    pub fn new() -> Self {
        Auth {}
    }
}

#[async_trait]
impl Handler for Auth {
    async fn handle(
        &self,
        req: &mut Request,
        depot: &mut Depot,
        resp: &mut Response,
        ctrl: &mut FlowCtrl,
    ) {
        let iden = match req.extensions().get::<Identity>() {
            None => {
                ApiErr::Unauthorized.write(req, depot, resp).await;
                ctrl.skip_rest();
                return;
            }
            Some(v) => v,
        };
        if iden.id() == 0 {
            ApiErr::Unauthorized.write(req, depot, resp).await;
            ctrl.skip_rest();
            return;
        }
        // TODO: ...
    }
}
