A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 53a09f7297a98839921ba61ec93441693ae5a0f3 (1697 bytes)[doc][blame][raw]
1
use sqlx::postgres::Postgres;2
use sqlx::Acquire;3
use tide_sqlx::SQLxRequestExt;5
use crate::model;6
use crate::response::Response;8
#[derive(Default)]9
pub struct Auth;11
#[tide::utils::async_trait]12
impl<State: Clone + Send + Sync + 'static> tide::Middleware<State> for Auth {13
async fn handle(14
&self,15
mut req: tide::Request<State>,16
next: tide::Next<'_, State>,17
) -> tide::Result {18
if req.url().path().starts_with("/api") {19
let token = match req.header("verbal-token") {20
Some(header) => header.as_str(),21
None => return Response::throw("No request header 'verbal-token' provided!"),22
};23
let query = format!(24
"25
SELECT account.*26
FROM account27
INNER JOIN device28
ON account.id = device.account_id29
WHERE device.uid = '{}'30
",31
&token,32
);33
let option_account;34
{35
let mut conn = req.sqlx_conn::<Postgres>().await;36
option_account = sqlx::query_as::<_, model::Account>(&query)37
.fetch_optional(conn.acquire().await.unwrap())38
.await39
.unwrap();40
}41
match option_account {42
Some(account) => {43
req.set_ext(account);44
}45
None => {46
return Response::throw("No account found for provided header 'verbal-token'!");47
}48
}49
}50
let res = next.run(req).await;51
Ok(res)52
}53
}