-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: feat: remove duplicate operation of storing jwt in the database
- Loading branch information
Showing
3 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ use sqlx::{ | |
|
||
mod signup_jwt; | ||
mod users; | ||
mod users_jwt; | ||
mod users_session; | ||
|
||
#[derive(Clone)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use async_session::chrono::{Duration, Utc}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::Repository; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct SignupClaims { | ||
user_id: i64, | ||
email: String, | ||
exp: i64, | ||
} | ||
|
||
impl Repository { | ||
pub async fn encode_email_update_jwt( | ||
&self, | ||
user_id: i64, | ||
email: &str, | ||
) -> anyhow::Result<String> { | ||
let exp = (Utc::now() + Duration::minutes(60)).timestamp(); | ||
let claims = SignupClaims { | ||
user_id: user_id, | ||
email: email.to_owned(), | ||
exp, | ||
}; | ||
|
||
let encode_key: String = std::env::var("JWT_SECRET")?; | ||
|
||
let jwt = jsonwebtoken::encode( | ||
&jsonwebtoken::Header::default(), | ||
&claims, | ||
&jsonwebtoken::EncodingKey::from_secret(encode_key.as_ref()), | ||
)?; | ||
|
||
Ok(jwt) | ||
} | ||
} |