/ src / mailers / auth.rs
auth.rs
 1  // auth mailer
 2  #![allow(non_upper_case_globals)]
 3  
 4  use loco_rs::prelude::*;
 5  use serde_json::json;
 6  
 7  use crate::models::users;
 8  
 9  static welcome: Dir<'_> = include_dir!("src/mailers/auth/welcome");
10  static forgot: Dir<'_> = include_dir!("src/mailers/auth/forgot");
11  static magic_link: Dir<'_> = include_dir!("src/mailers/auth/magic_link");
12  // #[derive(Mailer)] // -- disabled for faster build speed. it works. but lets
13  // move on for now.
14  
15  #[allow(clippy::module_name_repetitions)]
16  pub struct AuthMailer {}
17  impl Mailer for AuthMailer {}
18  impl AuthMailer {
19      /// Sending welcome email the the given user
20      ///
21      /// # Errors
22      ///
23      /// When email sending is failed
24      pub async fn send_welcome(ctx: &AppContext, user: &users::Model) -> Result<()> {
25          Self::mail_template(
26              ctx,
27              &welcome,
28              mailer::Args {
29                  to: user.email.to_string(),
30                  locals: json!({
31                    "name": user.name,
32                    "verifyToken": user.email_verification_token,
33                    "domain": ctx.config.server.full_url()
34                  }),
35                  ..Default::default()
36              },
37          )
38          .await?;
39  
40          Ok(())
41      }
42  
43      /// Sending forgot password email
44      ///
45      /// # Errors
46      ///
47      /// When email sending is failed
48      pub async fn forgot_password(ctx: &AppContext, user: &users::Model) -> Result<()> {
49          Self::mail_template(
50              ctx,
51              &forgot,
52              mailer::Args {
53                  to: user.email.to_string(),
54                  locals: json!({
55                    "name": user.name,
56                    "resetToken": user.reset_token,
57                    "domain": ctx.config.server.full_url()
58                  }),
59                  ..Default::default()
60              },
61          )
62          .await?;
63  
64          Ok(())
65      }
66  
67      /// Sends a magic link authentication email to the user.
68      ///
69      /// # Errors
70      ///
71      /// When email sending is failed
72      pub async fn send_magic_link(ctx: &AppContext, user: &users::Model) -> Result<()> {
73          Self::mail_template(
74              ctx,
75              &magic_link,
76              mailer::Args {
77                  to: user.email.to_string(),
78                  locals: json!({
79                    "name": user.name,
80                    "token": user.magic_link_token.clone().ok_or_else(|| Error::string(
81                              "the user model not contains magic link token",
82                      ))?,
83                    "host": ctx.config.server.full_url()
84                  }),
85                  ..Default::default()
86              },
87          )
88          .await?;
89  
90          Ok(())
91      }
92  }