1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
use crate::cqrs::infra::token::services::jwt_hmac_encoder::JwtHMACEncoderTokenService;
use crate::cqrs::models::errors::{Error, ResultErr};
use std::sync::Arc;

pub struct AuthenticationComponent {
    pub jwt_token_encoder_service: Arc<JwtHMACEncoderTokenService>,
}

impl AuthenticationComponent {
    pub fn new() -> ResultErr<Self> {
        let jwt_secret = std::env::var("JWT_SECRET")
            .map_err(|_|
                Error::Simple("env variable JWT_SECRET is not defined".to_string())
            )?;
        Ok(
            Self {
                jwt_token_encoder_service: Arc::new(JwtHMACEncoderTokenService::new(jwt_secret)),
            }
        )
    }
}