so://phie

verbal

A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal

blob: 0581719f3da193727f2456c9d6a2fdf47a15b3b5 (1774 bytes)[doc][blame][raw]

1use sqlx::postgres::Postgres;

2use sqlx::Acquire;

3use tide_sqlx::SQLxRequestExt;

4

5use crate::model;

6use crate::unwrap_option_or_throw;

7use crate::unwrap_result_or_throw;

8

9#[derive(Default)]

10pub struct Auth;

11

12#[tide::utils::async_trait]

13impl<State: Clone + Send + Sync + 'static> tide::Middleware<State> for Auth {

14 async fn handle(

15 &self,

16 mut req: tide::Request<State>,

17 next: tide::Next<'_, State>,

18 ) -> tide::Result {

19 if req.url().path().starts_with("/api") {

20 let token =

21 unwrap_option_or_throw!(req.header("verbal-token"), "no token was provided!")

22 .as_str();

23 let query = format!(

24 "

25 SELECT account.*

26 FROM account

27 INNER JOIN device

28 ON account.id = device.account_id

29 WHERE device.uid = '{}'

30 ",

31 &token,

32 );

33 let option_account;

34 {

35 let mut conn = req.sqlx_conn::<Postgres>().await;

36 option_account = unwrap_result_or_throw!(

37 sqlx::query_as::<_, model::Account>(&query)

38 .fetch_optional(unwrap_result_or_throw!(

39 conn.acquire().await,

40 "cannot acquire connection to database!"

41 ))

42 .await,

43 "cannot acquire account for given device!"

44 );

45 }

46 req.set_ext(unwrap_option_or_throw!(

47 option_account,

48 "no account found for given token!"

49 ));

50 }

51 let res = next.run(req).await;

52 Ok(res)

53 }

54}