Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 2x 2x 2x | // core/src/hooks/useSessions.ts
import { useCallback, useEffect, useState } from "react";
import axios from "axios";
import { IS_STANDALONE } from "@/util/standalone";
export type SessionStatus = "active" | "expired" | "ended";
export interface AccountSession {
id: string;
current: boolean;
status: SessionStatus;
end_reason: string | null;
created_at: string | null;
last_seen_at: string | null;
ended_at: string | null;
ip: string | null;
device: {
type: string | null;
browser: string | null;
os: string | null;
user_agent: string | null;
};
location: {
city: string | null;
country: string | null;
lat: number | null;
lng: number | null;
};
}
const AUTH = import.meta.env.VITE_AUTH_DOMAIN;
// In standalone (no-backend) mode, surface a representative session so the UI
// renders without svc-auth running.
const MOCK_SESSIONS: AccountSession[] = [
{
id: "standalone-current",
current: true,
status: "active",
end_reason: null,
created_at: new Date(Date.now() - 1000 * 60 * 60 * 3).toISOString(),
last_seen_at: new Date().toISOString(),
ended_at: null,
ip: "203.0.113.7",
device: { type: "desktop", browser: "Chrome 120", os: "macOS 14", user_agent: "" },
location: { city: "London", country: "GB", lat: 51.5074, lng: -0.1278 },
},
];
const MOCK_HISTORY: AccountSession[] = [
{
id: "standalone-old-1",
current: false,
status: "ended",
end_reason: "logout",
created_at: new Date(Date.now() - 1000 * 60 * 60 * 24 * 2).toISOString(),
last_seen_at: new Date(Date.now() - 1000 * 60 * 60 * 24 * 2 + 1000 * 60 * 40).toISOString(),
ended_at: new Date(Date.now() - 1000 * 60 * 60 * 24 * 2 + 1000 * 60 * 40).toISOString(),
ip: "198.51.100.4",
device: { type: "mobile", browser: "Safari 17", os: "iOS 17", user_agent: "" },
location: { city: "Manchester", country: "GB", lat: 53.4808, lng: -2.2426 },
},
{
id: "standalone-old-2",
current: false,
status: "expired",
end_reason: "expired",
created_at: new Date(Date.now() - 1000 * 60 * 60 * 24 * 9).toISOString(),
last_seen_at: new Date(Date.now() - 1000 * 60 * 60 * 24 * 8).toISOString(),
ended_at: new Date(Date.now() - 1000 * 60 * 60 * 24 * 8).toISOString(),
ip: "203.0.113.7",
device: { type: "desktop", browser: "Firefox 121", os: "Windows 11", user_agent: "" },
location: { city: "London", country: "GB", lat: 51.5074, lng: -0.1278 },
},
];
export function useSessions() {
const [sessions, setSessions] = useState<AccountSession[]>(
IS_STANDALONE ? MOCK_SESSIONS : [],
);
const [loading, setLoading] = useState(!IS_STANDALONE);
const [error, setError] = useState<string | null>(null);
const fetchSessions = useCallback(async () => {
Iif (IS_STANDALONE) return;
setLoading(true);
setError(null);
try {
const res = await axios.get<{ sessions: AccountSession[] }>(
`${AUTH}/sessions`,
{ withCredentials: true },
);
setSessions(res.data.sessions ?? []);
} catch (err) {
console.error("Failed to load sessions:", err);
setError("Could not load sessions.");
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
void fetchSessions();
}, [fetchSessions]);
const [actionError, setActionError] = useState<string | null>(null);
const revokeSession = useCallback(
async (id: string) => {
if (IS_STANDALONE) return;
setActionError(null);
try {
// POST (not DELETE): edge WAFs in front of the deployment block DELETE
// with a 403, so revoke goes over POST like every other mutation.
await axios.post(
`${AUTH}/sessions/${id}/revoke`,
{},
{ withCredentials: true },
);
await fetchSessions();
} catch (err) {
console.error("Failed to revoke session:", err);
setActionError("Could not revoke that session. Please try again.");
}
},
[fetchSessions],
);
const revokeOthers = useCallback(async () => {
if (IS_STANDALONE) return;
setActionError(null);
try {
await axios.post(
`${AUTH}/sessions/revoke-others`,
{},
{ withCredentials: true },
);
await fetchSessions();
} catch (err) {
console.error("Failed to revoke other sessions:", err);
setActionError("Could not sign out the other sessions. Please try again.");
}
}, [fetchSessions]);
return {
sessions,
loading,
error,
actionError,
refetch: fetchSessions,
revokeSession,
revokeOthers,
};
}
interface SessionHistoryResponse {
sessions: AccountSession[];
total: number;
limit: number;
offset: number;
}
/**
* Paginated view of the caller's ended sessions (logged out, revoked, expired).
* Fetches the first page on mount and exposes `loadMore` to append further
* pages until `hasMore` is false.
*/
export function useSessionHistory(pageSize = 10) {
const [sessions, setSessions] = useState<AccountSession[]>(
IS_STANDALONE ? MOCK_HISTORY : [],
);
const [total, setTotal] = useState(IS_STANDALONE ? MOCK_HISTORY.length : 0);
const [loading, setLoading] = useState(!IS_STANDALONE);
const [loadingMore, setLoadingMore] = useState(false);
const [error, setError] = useState<string | null>(null);
const fetchPage = useCallback(
async (offset: number, replace: boolean) => {
Iif (IS_STANDALONE) return;
if (replace) setLoading(true);
else EsetLoadingMore(true);
setError(null);
try {
const res = await axios.get<SessionHistoryResponse>(
`${AUTH}/sessions/history`,
{ params: { limit: pageSize, offset }, withCredentials: true },
);
setTotal(res.data.total ?? 0);
setSessions((prev) => {
const page = res.data.sessions ?? [];
return replace ? page : [...prev, ...page];
});
} catch (err) {
console.error("Failed to load session history:", err);
setError("Could not load session history.");
} finally {
if (replace) setLoading(false);
else EsetLoadingMore(false);
}
},
[pageSize],
);
useEffect(() => {
void fetchPage(0, true);
}, [fetchPage]);
const loadMore = useCallback(() => {
void fetchPage(sessions.length, false);
}, [fetchPage, sessions.length]);
const refresh = useCallback(() => {
void fetchPage(0, true);
}, [fetchPage]);
return {
sessions,
total,
loading,
loadingMore,
error,
hasMore: sessions.length < total,
loadMore,
refresh,
};
}
|