From 6c9a5896f1d1b1bb3175dda4ef5232bc5245898f Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Wed, 3 Apr 2024 16:45:22 -0500 Subject: feat: impl logout route --- src/routes/login.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src/routes/login.rs') 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(()) + } } -- cgit v1.2.3-70-g09d2