doido_auth/framework_views.rs
1//! Built-in, overridable auth views (the Devise "views live in the gem" analogue).
2//!
3//! `doido new --auth` and `auth:install` no longer copy auth controllers/views
4//! into the app. The built-in controllers under [`crate::controllers`] render
5//! templates like `auth/sign_in`; [`register_views`] makes those templates
6//! resolvable out of the box by registering them as *framework templates* in
7//! `doido-view`. An app that writes its own `app/views/auth/*.html.tera` (e.g.
8//! after `doido generate auth:controllers`) overrides them by name.
9//!
10//! Call [`register_views`] once at boot, **before** `doido_view::init`.
11
12/// Registers the built-in auth view templates with `doido-view` so the framework
13/// controllers can render HTML without the app copying any view files. Idempotent.
14pub fn register_views() {
15 for (name, content) in VIEWS {
16 doido_view::register_framework_template(name, content);
17 }
18}
19
20/// `(tera template name, source)` pairs for the built-in HTML auth views. Names
21/// mirror what the built-in controllers pass to `Context::render` (with the
22/// `.html.tera` suffix `doido-view` appends).
23const VIEWS: &[(&str, &str)] = &[
24 (
25 "auth/sign_in.html.tera",
26 include_str!("../templates/auth/views/sign_in.html.tera"),
27 ),
28 (
29 "auth/sign_up.html.tera",
30 include_str!("../templates/auth/views/sign_up.html.tera"),
31 ),
32 (
33 "auth/password_new.html.tera",
34 include_str!("../templates/auth/views/password_new.html.tera"),
35 ),
36 (
37 "auth/password_edit.html.tera",
38 include_str!("../templates/auth/views/password_edit.html.tera"),
39 ),
40 #[cfg(feature = "auth-2fa")]
41 (
42 "auth/two_factor.html.tera",
43 include_str!("../templates/auth/views/two_factor.html.tera"),
44 ),
45];
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn registers_core_auth_views() {
53 register_views();
54 register_views(); // idempotent
55 let snapshot = doido_view::global::framework_template_snapshot();
56 for name in [
57 "auth/sign_in.html.tera",
58 "auth/sign_up.html.tera",
59 "auth/password_new.html.tera",
60 "auth/password_edit.html.tera",
61 ] {
62 assert!(
63 snapshot.iter().any(|(n, _)| n == name),
64 "expected framework view {name} to be registered"
65 );
66 }
67 }
68}