فهرست منبع

Create logout route for user

Lee Morgan 2 ماه پیش
والد
کامیت
1b82c66288
5فایلهای تغییر یافته به همراه34 افزوده شده و 1 حذف شده
  1. 12 0
      src/controllers/user/logout.rs
  2. 1 0
      src/controllers/user/mod.rs
  3. 2 0
      src/logic/mod.rs
  4. 16 0
      src/logic/remove_cookie.rs
  5. 3 1
      src/routes/user.rs

+ 12 - 0
src/controllers/user/logout.rs

@@ -0,0 +1,12 @@
+use actix_web::{HttpResponse, post};
+use serde_json::json;
+use crate::{
+    app_error::AppError,
+    logic::remove_cookie
+};
+
+#[post("/user/logout")]
+pub async fn route() -> Result<HttpResponse, AppError> {
+    let cookie = remove_cookie("user".to_string());
+    Ok(HttpResponse::Ok().cookie(cookie).json(json!({"success": true})))
+}

+ 1 - 0
src/controllers/user/mod.rs

@@ -1,3 +1,4 @@
 pub mod create;
 pub mod login;
 pub mod get_one;
+pub mod logout;

+ 2 - 0
src/logic/mod.rs

@@ -1,3 +1,5 @@
 pub mod create_cookie;
+pub mod remove_cookie;
 
 pub use create_cookie::create_cookie;
+pub use remove_cookie::remove_cookie;

+ 16 - 0
src/logic/remove_cookie.rs

@@ -0,0 +1,16 @@
+use actix_web::cookie::{Cookie, time::Duration};
+
+pub fn remove_cookie(name: String) -> Cookie<'static> {
+    if cfg!(debug_assertions) {
+        Cookie::build(name, "")
+            .path("/")
+            .max_age(Duration::ZERO)
+            .finish()
+    } else {
+        Cookie::build(name, "")
+            .domain("home.leemorgan.dev")
+            .path("/")
+            .max_age(Duration::ZERO)
+            .finish()
+    }
+}

+ 3 - 1
src/routes/user.rs

@@ -2,11 +2,13 @@ use actix_web::web;
 use crate::controllers::user::{
     create,
     login,
-    get_one
+    get_one,
+    logout
 };
 
 pub fn config(cfg: &mut web::ServiceConfig) {
     cfg.service(create::route);
     cfg.service(login::route);
     cfg.service(get_one::route);
+    cfg.service(logout::route);
 }