1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
//! Templates for users to login and register with.
use crate::templates::Template;
use crate::web::services::auth::oauth2_providers::{discord::DiscordOAuth, github::GitHubOauth};
use crate::web::services::auth::rpi_cas::RpiCas;
use crate::web::services::auth::IdentityProvider;
use serde_json::{Map, Value};
/// Path to template from template directory root.
const TEMPLATE_PATH: &'static str = "auth";
/// Handlebars key to change the header of the page.
pub const HEADER: &'static str = "header";
/// Handlebars key for list of identity provider objects to display
/// on the page.
pub const ITEMS: &'static str = "items";
/// Handlebars key on identity providers for the link to take the user to.
pub const LINK: &'static str = "link";
/// Handlebars key on identity providers for the additional classes to add
/// (beyond "btn w-100").
pub const CLASS: &'static str = "class";
/// Handlebars key on identity providers for the message to display in the
/// button.
pub const MESSAGE: &'static str = "message";
/// Optional Handlebars key on identity providers for an icon to be displayed
/// in the button next to the message.
pub const ICON: &'static str = "icon";
/// New empty template with reference to the proper handlebars file.
fn empty() -> Template {
Template::new(TEMPLATE_PATH)
}
/// Create an item to add to an auth template.
fn item(
link: String,
class: &'static str,
message: impl Into<Value>,
icon: Option<&'static str>,
) -> Map<String, Value> {
// Create map.
let mut m: Map<String, Value> = Map::new();
// Insert keys.
m.insert(LINK.into(), link.into());
m.insert(CLASS.into(), class.into());
m.insert(MESSAGE.into(), message.into());
if let Some(i) = icon {
m.insert(ICON.into(), i.into());
}
// Return map.
return m;
}
/// Create a template to offer the user options to login.
pub fn login() -> Template {
// Make list of identity providers in login configuration.
let items: Vec<Map<String, Value>> = vec![
item(
GitHubOauth::login_path(),
"btn-github mb-2",
"Login using GitHub",
Some("github"),
),
item(
DiscordOAuth::login_path(),
"btn-discord mb-2",
"Login using Discord",
// This is manually coded for in the template file and is not
// a Feather icon. Do not use it in other places, as it won't work.
Some("discord"),
),
item(RpiCas::login_path(), "btn-rpi", "Login using RPI CAS", None),
];
// Create and return template.
let mut template = empty();
template[HEADER] = json!("Sign In");
template[ITEMS] = json!(items);
return template;
}
/// Create a template to offer the users options to register a new account.
pub fn register() -> Template {
// Make list of identity providers in account creation configuration.
let items: Vec<Map<String, Value>> = vec![
// item(
// GitHubOauth::register_path(),
// "btn-github mb-2",
// "Register using GitHub",
// Some("github"),
// ),
item(
DiscordOAuth::register_path(),
"btn-discord mb-2",
"Register using Discord",
// This is manually coded for in the template file and is not
// a Feather icon. Do not use it in other places, as it won't work.
Some("discord"),
),
// item(
// RpiCas::register_path(),
// "btn-rpi",
// "Register using RPI CAS",
// None,
// ),
];
// Create and return template.
let mut template = empty();
template[HEADER] = json!("Create Account");
template[ITEMS] = json!(items);
return template;
}