修改refresh token生成逻辑

This commit is contained in:
李运家 2024-10-03 17:12:32 +08:00
parent 1a39728d3e
commit 44bbc846df
3 changed files with 6 additions and 2 deletions

View File

@ -9,6 +9,7 @@ pub struct Claims {
pub exp: i64, // Token过期时间戳
}
#[inline]
pub fn generate_token(sub: &str) -> String {
let claim = Claims {
sub: sub.to_string(),
@ -25,10 +26,11 @@ pub fn generate_token(sub: &str) -> String {
})
}
#[inline]
pub fn generate_refresh_token(sub: &str) -> String {
let claim = Claims {
sub: sub.to_string(),
exp: (Utc::now() + TimeDelta::try_seconds(config!().jwt.expires).unwrap()).timestamp(),
exp: (Utc::now() + TimeDelta::try_seconds(config!().jwt.refresh_expires).unwrap()).timestamp(),
};
let token = jsonwebtoken::encode(
&jsonwebtoken::Header::default(),
@ -36,11 +38,12 @@ pub fn generate_refresh_token(sub: &str) -> String {
&jsonwebtoken::EncodingKey::from_secret(config!().jwt.refresh_token_secret.as_bytes()),
);
token.unwrap_or_else(|e| {
tracing::error!(error =?e, "生成Rfresh Token失败");
tracing::error!(error =?e, "生成Refresh Token失败");
"".to_string()
})
}
#[inline]
pub fn verify_token(token: &str) -> Result<Claims, jsonwebtoken::errors::Error> {
jsonwebtoken::decode::<Claims>(
token,
@ -50,6 +53,7 @@ pub fn verify_token(token: &str) -> Result<Claims, jsonwebtoken::errors::Error>
.map(|data| data.claims)
}
#[inline]
pub fn verify_refresh_token(token: &str) -> Result<Claims, jsonwebtoken::errors::Error> {
jsonwebtoken::decode::<Claims>(
token,