forge
Explore
Log in

Don't you dare go Hollow

There is a way to sign out

Browse files at this commit

3836ec344238d14c6e35e8f8cf0b32ee4ae2a0f0Ahmet Kaan GümüşParent: 8462f29
  • feat
    • there is a way to sign out
Show full message
✨ feat(settings): there is a way to sign out

The server function has existed since the auth slice; nothing in the interface
ever called it, so the only way to end a session was to wait a fortnight for it
to expire or clear the cookie by hand. On a shared machine that is not a
missing button, it is a missing lock.

It sits in the profile panel, which is the panel about you, under a line saying
what it does and does not do — this browser ends, your other ones do not.

The call now answers with a redirect home rather than a value, because there is
nothing left to say to the page that asked: every panel on it is about somebody
who is no longer signed in. Home with no session is the sign-in page, so a
browser without script lands where a hydrated one does instead of sitting on a
page that appears not to have reacted.

Tested at the endpoint: the cookie stops working on the server after the call,
the no-script post answers `302 /`, and one person signing out leaves everybody
else's session alone.
crates/dizey-web/src/auth.rsmodified+7 -0
@@ -423,6 +423,12 @@ pub async fn sign_in(email: String, password: String) -> Result<Option<Refusal>,
423 }423 }
424 424
425 /// Ends this browser's session. Other browsers keep theirs.425 /// Ends this browser's session. Other browsers keep theirs.
426+///
427+/// The answer is a redirect home rather than a value, because there is nothing
428+/// left to say to the page that asked: whoever was signed in is not any more,
429+/// and every panel on it is about them. Home with no session is the sign-in
430+/// page, so both a hydrated browser and one without script land in the same
431+/// place, which is the one that makes sense.
426 #[server]432 #[server]
427 pub async fn sign_out() -> Result<(), ServerFnError> {433 pub async fn sign_out() -> Result<(), ServerFnError> {
428 use crate::server::{accounts, clear_session_cookie, presented_session};434 use crate::server::{accounts, clear_session_cookie, presented_session};
@@ -431,6 +437,7 @@ pub async fn sign_out() -> Result<(), ServerFnError> {
431 let _ = accounts().sign_out(&presented).await;437 let _ = accounts().sign_out(&presented).await;
432 }438 }
433 clear_session_cookie();439 clear_session_cookie();
440+ leptos_axum::redirect("/", false);
434 Ok(())441 Ok(())
435 }442 }
436 443
crates/dizey-web/src/settings.rsmodified+11 -0
@@ -547,6 +547,7 @@ fn SettingsScreen(
547 #[component]547 #[component]
548 fn ProfilePanel(me: Me) -> impl IntoView {548 fn ProfilePanel(me: Me) -> impl IntoView {
549 let action = ServerAction::<SaveProfile>::new();549 let action = ServerAction::<SaveProfile>::new();
550+ let out = ServerAction::<crate::auth::SignOut>::new();
550 let value = action.value();551 let value = action.value();
551 let saved = move || matches!(value.get(), Some(Ok(None)));552 let saved = move || matches!(value.get(), Some(Ok(None)));
552 let refusal = move || match value.get() {553 let refusal = move || match value.get() {
@@ -592,6 +593,16 @@ fn ProfilePanel(me: Me) -> impl IntoView {
592 </button>593 </button>
593 </div>594 </div>
594 </ActionForm>595 </ActionForm>
596+ <div class="panel-body panel-foot panel-foot-split">
597+ <span class="field-note">
598+ "Signing out ends this browser's session. Anywhere else you are signed in stays signed in."
599+ </span>
600+ <ActionForm action=out>
601+ <button class="quiet" type="submit">
602+ "Sign out"
603+ </button>
604+ </ActionForm>
605+ </div>
595 </section>606 </section>
596 }607 }
597 }608 }
crates/dizey-web/tests/http.rsmodified+81 -0
@@ -1485,3 +1485,84 @@ async fn an_invitation_names_the_admin_who_made_it_and_not_the_invitee() {
1485 answer.body1485 answer.body
1486 );1486 );
1487 }1487 }
1488+
1489+/// Signing out ends the session on the server, not merely in the browser. A
1490+/// cookie that survived in a copied header, a bookmarked tab or a shared
1491+/// machine must stop working the moment the button is pressed.
1492+#[tokio::test]
1493+async fn signing_out_stops_the_session_being_worth_anything() {
1494+ let app = App::open().await;
1495+ let cookie = admin(&app).await;
1496+
1497+ let works = app
1498+ .post(
1499+ path::<dizey_web::settings::CurrentSettings>(),
1500+ Some(&cookie),
1501+ &[],
1502+ )
1503+ .await;
1504+ assert!(works.body.contains("administers"), "{}", works.body);
1505+
1506+ let out = app
1507+ .post(path::<dizey_web::auth::SignOut>(), Some(&cookie), &[])
1508+ .await;
1509+ assert_eq!(out.status, StatusCode::OK, "{}", out.body);
1510+
1511+ // The same cookie, replayed. The server has to be the one refusing.
1512+ let after = app
1513+ .post(
1514+ path::<dizey_web::settings::CurrentSettings>(),
1515+ Some(&cookie),
1516+ &[],
1517+ )
1518+ .await;
1519+ assert!(
1520+ !after.body.contains("administers"),
1521+ "the session outlived signing out: {}",
1522+ after.body
1523+ );
1524+}
1525+
1526+/// A browser with no script is sent home, where no session means the sign-in
1527+/// page. Without this the click looks like nothing happening on the very page
1528+/// that is now about nobody.
1529+#[tokio::test]
1530+async fn signing_out_without_script_lands_on_the_sign_in_page() {
1531+ let app = App::open().await;
1532+ let cookie = admin(&app).await;
1533+
1534+ let out = app
1535+ .post_without_script(
1536+ path::<dizey_web::auth::SignOut>(),
1537+ Some(&cookie),
1538+ "http://dizey.test/settings",
1539+ &[],
1540+ )
1541+ .await;
1542+
1543+ assert_eq!(out.status, StatusCode::FOUND, "{}", out.body);
1544+ assert_eq!(out.location.as_deref(), Some("/"), "{:?}", out.location);
1545+}
1546+
1547+/// Signing out is not a way to sign anybody else out. The other person's
1548+/// session is untouched, and so is every other browser of the person leaving.
1549+#[tokio::test]
1550+async fn signing_out_leaves_every_other_session_alone() {
1551+ let app = App::open().await;
1552+ let admin_cookie = admin(&app).await;
1553+ let member = invited(&app, &admin_cookie, "emre@dizey.sh", "Emre", Role::Member).await;
1554+
1555+ let out = app
1556+ .post(path::<dizey_web::auth::SignOut>(), Some(&member), &[])
1557+ .await;
1558+ assert_eq!(out.status, StatusCode::OK, "{}", out.body);
1559+
1560+ let still = app
1561+ .post(
1562+ path::<dizey_web::settings::CurrentSettings>(),
1563+ Some(&admin_cookie),
1564+ &[],
1565+ )
1566+ .await;
1567+ assert!(still.body.contains("administers"), "{}", still.body);
1568+}
style/main.scssmodified+20 -0
@@ -1575,6 +1575,26 @@ a {
1575 align-items: center;1575 align-items: center;
1576 gap: 12px;1576 gap: 12px;
1577 justify-content: flex-end;1577 justify-content: flex-end;
1578+
1579+ // A foot with something to say on the left and the button on the right.
1580+ &-split { justify-content: space-between; }
1581+}
1582+
1583+// The quiet counterpart of `.primary`, for an action that is real but is not
1584+// what the panel is for — signing out of a panel about your name, say.
1585+.quiet {
1586+ height: 36px;
1587+ padding: 0 16px;
1588+ background: transparent;
1589+ border: 1px solid var(--border-strong);
1590+ border-radius: var(--radius);
1591+ color: var(--ink);
1592+ font-family: var(--font-sans);
1593+ font-size: 13px;
1594+ font-weight: 600;
1595+ cursor: pointer;
1596+
1597+ &:hover { background: #f1efe8; }
1578 }1598 }
1579 1599
1580 .field {1600 .field {