Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

セッションのセーブ、ロード等実装 #41

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここだけto_stringになってるの気になる

Suggested change
.load_session(session_id.to_string())
.load_session(session_id.to_owned())

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全部to_string でもいい?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全て合わせるんだったらどっちでもいいよ

.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(())
}