~vpzom/hitide

ccae2cb90ac79f1c138d1a5e5da6cb0f20b50607 — Colin Reeder 28 days ago af6366c
Add logout button in header
5 files changed, 49 insertions(+), 0 deletions(-)

A icons/res/logout.svg
M icons/src/lib.rs
M res/main.css
M src/components/mod.rs
M src/routes/mod.rs
A icons/res/logout.svg => icons/res/logout.svg +6 -0
@@ 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>

M icons/src/lib.rs => icons/src/lib.rs +1 -0
@@ 41,6 41,7 @@ macro_rules! icons {
}

icons! {
    LOGOUT => "logout.svg",
    NOTIFICATIONS => "notifications.svg",
    NOTIFICATIONS_SOME => "notifications-some.svg",
    PERSON => "person.svg",

M res/main.css => res/main.css +1 -0
@@ 81,6 81,7 @@ body {
	background: none;
	border: none;
	padding: 0;
	cursor: pointer;
}

.votebox {

M src/components/mod.rs => src/components/mod.rs +5 -0
@@ 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 {

M src/routes/mod.rs => src/routes/mod.rs +36 -0
@@ 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),
        )