mirror of
https://github.com/danbulant/Portfolio
synced 2026-05-27 14:02:14 +00:00
refactor: dry decode token
This commit is contained in:
parent
9eaf0eb347
commit
4a852fa915
1 changed files with 8 additions and 7 deletions
|
|
@ -11,6 +11,7 @@ use jsonwebtoken::{Header, Validation};
|
||||||
|
|
||||||
use admin_token::AdminToken;
|
use admin_token::AdminToken;
|
||||||
use candidate_token::CandidateToken;
|
use candidate_token::CandidateToken;
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
const ONE_WEEK: i64 = 60 * 60 * 24 * 7;
|
const ONE_WEEK: i64 = 60 * 60 * 24 * 7;
|
||||||
|
|
||||||
|
|
@ -46,20 +47,20 @@ pub fn generate_admin_token(_admin: admin::Model) -> String {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decode_candidate_token(token: String) -> Result<TokenData<CandidateToken>> {
|
pub fn decode_token<T: for<'a> Deserialize<'a>>(token: String) -> Result<TokenData<T>> {
|
||||||
jsonwebtoken::decode::<CandidateToken>(
|
jsonwebtoken::decode::<T>(
|
||||||
&token,
|
&token,
|
||||||
&DecodingKey::from_secret(include_bytes!("secret.key")),
|
&DecodingKey::from_secret(include_bytes!("secret.key")),
|
||||||
&Validation::default(),
|
&Validation::default(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn decode_candidate_token(token: String) -> Result<TokenData<CandidateToken>> {
|
||||||
|
decode_token(token)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn decode_admin_token(token: String) -> Result<TokenData<AdminToken>> {
|
pub fn decode_admin_token(token: String) -> Result<TokenData<AdminToken>> {
|
||||||
jsonwebtoken::decode::<AdminToken>(
|
decode_token(token)
|
||||||
&token,
|
|
||||||
&DecodingKey::from_secret(include_bytes!("secret.key")),
|
|
||||||
&Validation::default(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*pub fn verify_token(token_data: &TokenData<UserToken>, conn: &DbConn) -> bool {
|
/*pub fn verify_token(token_data: &TokenData<UserToken>, conn: &DbConn) -> bool {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue