Skip to main content

agentmux_srv_core/identity/
sign_in_hook.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! What the sign-in code tells the account episodes. A sign-in that completed, or a validate that passed, means
5//! the account's credentials are good, so the panes stopped on a refused sign-in can go on. The episodes live in
6//! the server layer, which registers its reader here once at startup ([`set_after_sign_in`]), so `identity` does
7//! not name `server` (Area54 #1350). Before that registration there is nothing to tell.
8
9use std::sync::OnceLock;
10
11static AFTER_SIGN_IN: OnceLock<fn(&str)> = OnceLock::new();
12
13/// Register what to run when an account's credentials were just proven good. Later calls change nothing.
14pub fn set_after_sign_in(f: fn(&str)) {
15    let _ = AFTER_SIGN_IN.set(f);
16}
17
18/// `account_id`'s credentials were just proven good. Does nothing before [`set_after_sign_in`].
19pub fn after_sign_in(account_id: &str) {
20    if let Some(f) = AFTER_SIGN_IN.get() {
21        f(account_id);
22    }
23}
24
25#[cfg(test)]
26mod tests {
27    use std::sync::atomic::{AtomicUsize, Ordering};
28
29    use super::*;
30
31    static CALLS: AtomicUsize = AtomicUsize::new(0);
32
33    fn count(_: &str) {
34        CALLS.fetch_add(1, Ordering::SeqCst);
35    }
36
37    #[test]
38    fn it_tells_the_registered_reader_and_nothing_else() {
39        // Nothing registered yet in this process, or the same reader if another test got here first.
40        after_sign_in("acct-before");
41        set_after_sign_in(count);
42        let before = CALLS.load(Ordering::SeqCst);
43        after_sign_in("acct-1");
44        assert_eq!(CALLS.load(Ordering::SeqCst), before + 1);
45        set_after_sign_in(|_| panic!("a later registration must change nothing"));
46        after_sign_in("acct-2");
47        assert_eq!(CALLS.load(Ordering::SeqCst), before + 2);
48    }
49}