Macro diana::if_authed[][src]

macro_rules! if_authed {
    ($auth_state : expr, { $($key : expr => $value : expr), + }, $code : block) => { ... };
}
👎 Deprecated since 0.2.8:

please use is_authed! instead, it exposes a boolean and lets you use your own error logic

Expand description

Checks to see if the given authentication state matches the series of given claims. This must be provided with the authentication state, a series of claims to check against, and code to execute if the user is authenticated. This will call bail! with an ErrorKind::Unauthorised error if the user is unauthenticated, so that must be handled in your function’s return type!

Example

This is a simplified version of the internal logic that publishes data to the subscriptions server.

use diana::{
    errors::{Result, GQLResult},
    graphql_utils::get_auth_data_from_ctx,
    async_graphql::{Object as GQLObject},
    if_authed,
};

#[derive(Default, Clone)]
pub struct PublishMutation;
#[GQLObject]
impl PublishMutation {
    async fn publish(
        &self,
        raw_ctx: &async_graphql::Context<'_>,
        channel: String,
        data: String,
    ) -> Result<bool> {
        let auth_state = get_auth_data_from_ctx(raw_ctx)?;
        if_authed!(
            auth_state,
            {
                "role" => "graphql_server"
            },
            {
                // Your code here
                Ok(true)
            }
        )
    }
}