Skip to content

Commit

Permalink
Merge pull request #76 from kochamaltki/remember-password
Browse files Browse the repository at this point in the history
Remember password
  • Loading branch information
malinowy5 authored Apr 23, 2024
2 parents c5d5bb9 + 3dd294a commit 7fdf236
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/api_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,10 +707,14 @@ pub async fn login(request: LoginRequest) -> Result<impl warp::Reply, warp::Reje
if passwd == request.passwd {
info!("User {} logged in", name);
let token = get_token(user_id, is_admin);
let mut cookie_params = "Path=/; HttpOnly; Secure; SameSite=None; Partitioned;".to_string();
if request.remember_password == true {
cookie_params += "Max-Age=1209600;";
}
Ok(warp::reply::with_header(
token.clone(),
"set-cookie",
format!("token={}; Path=/; Max-Age=1209600; HttpOnly; Secure; SameSite=None; Partitioned", token),
format!("token={}; {}", token, cookie_params),
))
} else {
info!("User {} failed to log in", name);
Expand All @@ -737,11 +741,15 @@ pub async fn signup(request: SignupRequest) -> Result<impl warp::Reply, warp::Re
if check_user_name(&connection, request.user_name.clone()).await {
Err(warp::reject::custom(UserAlereadyExists))
} else {
let mut cookie_params = "Path=/; HttpOnly; Secure; SameSite=None; Partitioned;".to_string();
if request.remember_password == true {
cookie_params += "Max-Age=1209600;";
}
let token = add_user_db(&connection, request).await;
Ok(warp::reply::with_header(
token.clone(),
"set-cookie",
format!("token={}; Path=/; Max-Age=1209600; HttpOnly; Secure; SameSite=None; Partitioned", token),
format!("token={}; {}", token, cookie_params),
))
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ pub struct LikeCount {
pub struct LoginRequest {
pub user_name: String,
pub passwd: String,
pub remember_password: bool
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct SignupRequest {
pub user_name: String,
pub passwd: String
pub passwd: String,
pub remember_password: bool
}

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand Down

0 comments on commit 7fdf236

Please sign in to comment.