ForgeFlux/src/auth/adapter/input/web/login/template.rs

69 lines
1.9 KiB
Rust

use std::sync::Arc;
use actix_web::web;
use serde::{Deserialize, Serialize};
use crate::auth::adapter::input::web::routes::RoutesRepository;
use crate::auth::adapter::out::forge::SupportedForges;
use crate::auth::adapter::input::web::template::{tera_context, TemplateFile, TEMPLATES};
pub trait LoginPageInterface: Send + Sync {
fn get_login_page(&self, ctx: LoginCtx) -> Result<String, Box<dyn std::error::Error>>;
}
const LOGIN_TEMPLATE: TemplateFile = TemplateFile::new("login", "login/page.html");
pub fn register_templates(t: &mut tera::Tera) {
println!("registering login template");
LOGIN_TEMPLATE.register(t).expect(LOGIN_TEMPLATE.name);
}
pub fn load_templates(cfg: &mut web::ServiceConfig) {
let t = super::WebLoginPageInterface::new(Arc::new(LoginPageTemplate));
cfg.app_data(t);
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
struct Forge {
name: String,
path: String,
}
impl Forge {
fn new(name: String, path: String) -> Self {
Self { name, path }
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct LoginCtx {
forges: Vec<Forge>,
}
pub struct LoginCtxFactory;
impl LoginCtxFactory {
pub fn get_ctx(supported_forges: Vec<SupportedForges>, routes: &RoutesRepository) -> LoginCtx {
let mut forges = Vec::with_capacity(supported_forges.len());
for s in supported_forges.iter() {
forges.push(Forge::new(s.to_string(), routes.oauth_login(s)));
}
LoginCtx { forges }
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
struct LoginPageTemplate;
impl LoginPageInterface for LoginPageTemplate {
fn get_login_page(&self, ctx: LoginCtx) -> Result<String, Box<dyn std::error::Error>> {
let ctx = tera_context(&ctx);
let page = TEMPLATES
.render(LOGIN_TEMPLATE.name, &ctx.borrow())
.unwrap();
Ok(page)
}
}