Skip to content

Commit

Permalink
Add "Max-Age" option for cookies
Browse files Browse the repository at this point in the history
prevent cookies expires before tokens
  • Loading branch information
chenx6 committed Apr 8, 2023
1 parent e812b02 commit 4558cf5
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,18 @@ pub async fn authorize(
Some(p) if check_hash(&payload.password, &p) => (),
_ => return Err(AuthError::WrongCredentials),
}
let expire_age = 60 * 60 * 24; // Token/Cookies expire age

// Create the authorization token
let claims = Claim {
sub: "file".to_owned(),
username: payload.username,
exp: get_unix_timestamp() + 60 * 60 * 24,
exp: expire_age + get_unix_timestamp(),
};
let token =
encode(&Header::default(), &claims, &ENCRYPT_KEY).map_err(|_| AuthError::TokenCreation)?;
// Add token to cookies
let cookie = format!("Authorization=Bearer {}; ", &token);
let cookie = format!("Authorization=Bearer {}; Max-Age={}", &token, expire_age);
let mut response = Json(Token { token }).into_response();
response
.headers_mut()
Expand Down

0 comments on commit 4558cf5

Please sign in to comment.