summaryrefslogtreecommitdiffstats
path: root/src/routes/login.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-04-03 16:45:22 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-04-03 16:45:43 -0500
commit6c9a5896f1d1b1bb3175dda4ef5232bc5245898f (patch)
tree4394231047569139041d9a7e33bbce98d5370bf1 /src/routes/login.rs
parent5fabfaafd6a560fc93029f19338fac090f0f5d85 (diff)
feat: impl logout route
Diffstat (limited to 'src/routes/login.rs')
-rw-r--r--src/routes/login.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/routes/login.rs b/src/routes/login.rs
index a3c52f8..2441a21 100644
--- a/src/routes/login.rs
+++ b/src/routes/login.rs
@@ -66,6 +66,30 @@ impl Login {
}
}
+#[derive(Debug, Deserialize, TypedPath)]
+#[typed_path("/api/logout")]
+pub struct Logout;
+
+impl Logout {
+ #[tracing::instrument]
+ pub async fn get(self) -> impl IntoResponse {
+ let cookie = Cookie::build(("token", ""))
+ .path("/")
+ .max_age(time::Duration::hours(-1))
+ .same_site(SameSite::Lax)
+ .http_only(true)
+ .build();
+
+ let mut response = Json(json!({"status": "success"})).into_response();
+
+ response
+ .headers_mut()
+ .insert(SET_COOKIE, cookie.to_string().parse().unwrap());
+
+ response
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -137,4 +161,25 @@ mod tests {
Ok(())
}
+
+ #[sqlx::test]
+ async fn test_logout(pool: PgPool) -> TestResult {
+ let state = Arc::new(AppState {
+ pool,
+ jwt_secret: JWT_SECRET.to_string(),
+ jwt_max_age: JWT_MAX_AGE,
+ });
+ let router = init_router(state.clone());
+
+ let request = Request::builder()
+ .uri("/api/logout")
+ .method("GET")
+ .body(Body::empty())?;
+
+ let response = router.oneshot(request).await?;
+
+ assert_eq!(StatusCode::OK, response.status());
+
+ Ok(())
+ }
}