feat(web-auth): implement logout with token revoke and session clear

This commit is contained in:
2026-04-05 15:05:51 +08:00
parent 25857abf26
commit 95c10eca77
2 changed files with 66 additions and 5 deletions
+23 -2
View File
@@ -1,4 +1,4 @@
export type SendEmailCodeResult = {
export type SendEmailCodeResult = {
success: boolean;
expiresInSeconds: number;
};
@@ -15,6 +15,10 @@ export type EmailLoginResult = {
};
};
type RevokeRefreshTokenResult = {
success: boolean;
};
const DEFAULT_API_BASE_URL = "http://localhost:3000";
function resolveApiBaseUrl(): string {
@@ -30,7 +34,7 @@ async function parseErrorMessage(response: Response): Promise<string> {
try {
const body = (await response.json()) as { message?: string | string[] };
if (Array.isArray(body.message)) {
return body.message.join("");
return body.message.join("");
}
if (typeof body.message === "string" && body.message.trim()) {
return body.message;
@@ -75,3 +79,20 @@ export async function loginWithEmailCode(email: string, code: string): Promise<E
const body = (await response.json()) as EmailLoginResult;
return body;
}
export async function revokeRefreshToken(refreshToken: string): Promise<RevokeRefreshTokenResult> {
const response = await fetch(`${resolveApiBaseUrl()}/auth/token/revoke`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ refreshToken })
});
if (!response.ok) {
throw new Error(await parseErrorMessage(response));
}
const body = (await response.json()) as RevokeRefreshTokenResult;
return body;
}