diff options
author | Colin Reeder <vpzomtrrfrt@gmail.com> | 2020-09-27 12:21:37 -0600 |
---|---|---|
committer | Colin Reeder <vpzomtrrfrt@gmail.com> | 2020-09-27 12:21:37 -0600 |
commit | ccae2cb90ac79f1c138d1a5e5da6cb0f20b50607 (patch) | |
tree | 96cd9d2d52ab61351412becddcb72253ad7b6a6c | |
parent | af6366c336ee4ded8200362adb3f9e7156601ec1 (diff) |
Add logout button in header
-rw-r--r-- | icons/res/logout.svg | 6 | ||||
-rw-r--r-- | icons/src/lib.rs | 1 | ||||
-rw-r--r-- | res/main.css | 1 | ||||
-rw-r--r-- | src/components/mod.rs | 5 | ||||
-rw-r--r-- | src/routes/mod.rs | 36 |
5 files changed, 49 insertions, 0 deletions
diff --git a/icons/res/logout.svg b/icons/res/logout.svg new file mode 100644 index 0000000..da1e0e2 --- /dev/null +++ b/icons/res/logout.svg @@ -0,0 +1,6 @@ +<?xml version="1.0"?> +<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke="black" fill="none" stroke-width="2"> + <path d="M 12 2 h -8 q -3 0 -3 3 v 14 q 0 3 3 3 h 8" /> + <line x1="8" y1="12" x2="20" y2="12" /> + <path d="M 15 7 l 5 5 l -5 5" /> +</svg> diff --git a/icons/src/lib.rs b/icons/src/lib.rs index 0728295..e9ef054 100644 --- a/icons/src/lib.rs +++ b/icons/src/lib.rs @@ -41,6 +41,7 @@ macro_rules! icons { } icons! { + LOGOUT => "logout.svg", NOTIFICATIONS => "notifications.svg", NOTIFICATIONS_SOME => "notifications-some.svg", PERSON => "person.svg", diff --git a/res/main.css b/res/main.css index e365b2d..c23309a 100644 --- a/res/main.css +++ b/res/main.css @@ -81,6 +81,7 @@ body { background: none; border: none; padding: 0; + cursor: pointer; } .votebox { diff --git a/src/components/mod.rs b/src/components/mod.rs index 255f38a..9f7db1f 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -248,6 +248,11 @@ pub fn HTPage<'a, Children: render::Render>( <a href={format!("/users/{}", login.user.id)}> {hitide_icons::PERSON.img()} </a> + <form method={"POST"} action={"/logout"} class={"inline"}> + <button type={"submit"} class={"iconbutton"}> + {hitide_icons::LOGOUT.img()} + </button> + </form> </> }) } else { diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 5fde279..d2718e9 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -719,6 +719,38 @@ async fn handler_login_submit( } } +async fn handler_logout( + _: (), + ctx: Arc<crate::RouteContext>, + req: hyper::Request<hyper::Body>, +) -> Result<hyper::Response<hyper::Body>, crate::Error> { + let cookies = get_cookie_map_for_req(&req)?; + + res_to_error( + ctx.http_client + .request(for_client( + hyper::Request::delete(format!( + "{}/api/unstable/logins/~current", + ctx.backend_host, + )) + .body(Default::default())?, + req.headers(), + &cookies, + )?) + .await?, + ) + .await?; + + Ok(hyper::Response::builder() + .status(hyper::StatusCode::SEE_OTHER) + .header(hyper::header::LOCATION, "/") + .header( + hyper::header::SET_COOKIE, + format!("hitideToken=\"\"; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT"), + ) + .body("Successfully logged out.".into())?) +} + async fn page_lookup( _: (), ctx: Arc<crate::RouteContext>, @@ -1589,6 +1621,10 @@ pub fn route_root() -> crate::RouteNode<()> { ), ) .with_child( + "logout", + crate::RouteNode::new().with_handler_async("POST", handler_logout), + ) + .with_child( "lookup", crate::RouteNode::new().with_handler_async("GET", page_lookup), ) |