From 1fc62246abe28f0a72965cdb8ba6be4af60e38f8 Mon Sep 17 00:00:00 2001 From: kenken714 Date: Mon, 28 Oct 2024 20:38:34 +0900 Subject: [PATCH 1/3] feat: impl session --- src/repository/users_session.rs | 47 ++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/repository/users_session.rs b/src/repository/users_session.rs index d70446d..9c34232 100644 --- a/src/repository/users_session.rs +++ b/src/repository/users_session.rs @@ -1,7 +1,52 @@ use super::Repository; -use async_session::SessionStore; +use anyhow::Context; +use async_session::{Session, SessionStore}; + +use super::users::User; impl Repository { + pub async fn create_session(&self, user: User) -> anyhow::Result { + let mut session = Session::new(); + session + .insert("user_id", user.id) + .with_context(|| "Failed to insert user_id to session")?; + session + .insert("display_id", user.display_id) + .with_context(|| "Failed to insert display_id to session")?; + let result = self + .session_store + .store_session(session) + .await + .with_context(|| "Failed to store session to database") + .with_context(|| "Failed to create session")?; + match result { + Some(session_id) => Ok(session_id), + None => anyhow::bail!("unexpected error while creating session"), + } + } + + pub async fn delete_session(&self, session_id: &str) -> anyhow::Result> { + let Some(session) = self + .session_store + .load_session(session_id.to_string()) + .await? + else { + return Ok(None); + }; + + self.session_store.destroy_session(session).await?; + Ok(Some(())) + } + + pub async fn get_user_id_by_session_id(&self, session_id: &str) -> anyhow::Result> { + let session = self + .session_store + .load_session(session_id.to_owned()) + .await?; + + Ok(session.and_then(|s| s.get("user_id"))) + } + pub async fn get_display_id_by_session_id( &self, session_id: &str, From ae2dd495027af15403f3b9d00246ac0cd5b40552 Mon Sep 17 00:00:00 2001 From: kenken714 Date: Tue, 29 Oct 2024 01:03:12 +0900 Subject: [PATCH 2/3] fix: use to_string --- src/repository/users_session.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/repository/users_session.rs b/src/repository/users_session.rs index 9c34232..ebb3802 100644 --- a/src/repository/users_session.rs +++ b/src/repository/users_session.rs @@ -41,7 +41,7 @@ impl Repository { pub async fn get_user_id_by_session_id(&self, session_id: &str) -> anyhow::Result> { let session = self .session_store - .load_session(session_id.to_owned()) + .load_session(session_id.to_string()) .await?; Ok(session.and_then(|s| s.get("user_id"))) @@ -53,7 +53,7 @@ impl Repository { ) -> anyhow::Result> { let session = self .session_store - .load_session(session_id.to_owned()) + .load_session(session_id.to_string()) .await?; Ok(session.and_then(|s| s.get("display_id"))) From fd95293c692e9b51551ee03ac261af1fe747cb3d Mon Sep 17 00:00:00 2001 From: kenken714 Date: Tue, 29 Oct 2024 12:09:09 +0900 Subject: [PATCH 3/3] fix: use to_string at some point --- src/repository/jwt.rs | 4 +-- src/utils/mail.rs | 68 +++++++++++++++++++++---------------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/repository/jwt.rs b/src/repository/jwt.rs index e08fe4a..dfba375 100644 --- a/src/repository/jwt.rs +++ b/src/repository/jwt.rs @@ -75,7 +75,7 @@ impl Repository { iat, nbf, user_id: Some(user_id), - email: email.to_owned(), + email: email.to_string(), action: Action::change_email, }; @@ -92,7 +92,7 @@ impl Repository { iat, nbf, user_id: None, - email: email.to_owned(), + email: email.to_string(), action: Action::register_email, }; diff --git a/src/utils/mail.rs b/src/utils/mail.rs index db3f469..629c6eb 100644 --- a/src/utils/mail.rs +++ b/src/utils/mail.rs @@ -1,34 +1,34 @@ -use lettre::{ - message::{header, Mailbox, SinglePart}, - transport::smtp::authentication::Credentials, - Address, Message, SmtpTransport, Transport, -}; - -pub async fn send_email(send_to: Address, subject: &str, message: &str) -> anyhow::Result<()> { - let app_address = std::env::var("MAIL_ADDRESS").unwrap(); - let app_password = std::env::var("MAIL_PASSWORD").unwrap(); - let smtp = "smtp.gmail.com"; - - let email = Message::builder() - .from(Mailbox::new( - Some("traOJudge".to_owned()), - app_address.parse::
().unwrap(), - )) - .to(Mailbox::new(None, send_to)) - .subject(subject) - .singlepart( - SinglePart::builder() - .header(header::ContentType::TEXT_PLAIN) - .body(message.to_owned()), - )?; - - let credentials = Credentials::new(app_address, app_password); - - let mailer = SmtpTransport::starttls_relay(smtp)? - .credentials(credentials) - .build(); - - mailer.send(&email)?; - - Ok(()) -} +use lettre::{ + message::{header, Mailbox, SinglePart}, + transport::smtp::authentication::Credentials, + Address, Message, SmtpTransport, Transport, +}; + +pub async fn send_email(send_to: Address, subject: &str, message: &str) -> anyhow::Result<()> { + let app_address = std::env::var("MAIL_ADDRESS").unwrap(); + let app_password = std::env::var("MAIL_PASSWORD").unwrap(); + let smtp = "smtp.gmail.com"; + + let email = Message::builder() + .from(Mailbox::new( + Some("traOJudge".to_string()), + app_address.parse::
().unwrap(), + )) + .to(Mailbox::new(None, send_to)) + .subject(subject) + .singlepart( + SinglePart::builder() + .header(header::ContentType::TEXT_PLAIN) + .body(message.to_string()), + )?; + + let credentials = Credentials::new(app_address, app_password); + + let mailer = SmtpTransport::starttls_relay(smtp)? + .credentials(credentials) + .build(); + + mailer.send(&email)?; + + Ok(()) +}