Skip to content

Commit

Permalink
Merge pull request #41 from traP-jp/feat/session
Browse files Browse the repository at this point in the history
セッションのセーブ、ロード等実装
  • Loading branch information
kenken714 authored Oct 29, 2024
2 parents 9110651 + fd95293 commit e1e6dd9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/repository/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -92,7 +92,7 @@ impl Repository {
iat,
nbf,
user_id: None,
email: email.to_owned(),
email: email.to_string(),
action: Action::register_email,
};

Expand Down
49 changes: 47 additions & 2 deletions src/repository/users_session.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
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<String> {
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<Option<()>> {
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<Option<i64>> {
let session = self
.session_store
.load_session(session_id.to_string())
.await?;

Ok(session.and_then(|s| s.get("user_id")))
}

pub async fn get_display_id_by_session_id(
&self,
session_id: &str,
) -> anyhow::Result<Option<i64>> {
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")))
Expand Down
68 changes: 34 additions & 34 deletions src/utils/mail.rs
Original file line number Diff line number Diff line change
@@ -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::<Address>().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::<Address>().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(())
}

0 comments on commit e1e6dd9

Please sign in to comment.