Modulo makers3d desarrollado con codex V 0.0.1
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
export const browserApiBase = process.env.NEXT_PUBLIC_API_BASE_URL || "/api/v1";
|
||||
const internalApiBase = process.env.INTERNAL_API_URL || "http://api:4000/api/v1";
|
||||
|
||||
export async function serverFetch<T>(path: string): Promise<T> {
|
||||
const response = await fetch(`${internalApiBase}${path}`, {
|
||||
cache: "no-store"
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`API error: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json() as Promise<T>;
|
||||
}
|
||||
|
||||
export async function browserFetch<T>(path: string, options?: RequestInit): Promise<T> {
|
||||
const headers = new Headers(options?.headers);
|
||||
const isFormData = typeof FormData !== "undefined" && options?.body instanceof FormData;
|
||||
if (options?.body && !isFormData && !headers.has("Content-Type")) {
|
||||
headers.set("Content-Type", "application/json");
|
||||
}
|
||||
|
||||
const response = await fetch(`${browserApiBase}${path}`, {
|
||||
...options,
|
||||
credentials: "include",
|
||||
cache: "no-store",
|
||||
headers
|
||||
});
|
||||
|
||||
const data = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
const error = new Error(data.error || `Request failed: ${response.status}`) as Error & { status?: number };
|
||||
error.status = response.status;
|
||||
throw error;
|
||||
}
|
||||
return data as T;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
const authChannelName = "makers3d-auth";
|
||||
const authTabIdKey = "makers3d-auth-tab";
|
||||
|
||||
export type AuthEventType = "login" | "logout";
|
||||
|
||||
function getTabId() {
|
||||
let tabId = window.sessionStorage.getItem(authTabIdKey);
|
||||
if (!tabId) {
|
||||
tabId = crypto.randomUUID();
|
||||
window.sessionStorage.setItem(authTabIdKey, tabId);
|
||||
}
|
||||
return tabId;
|
||||
}
|
||||
|
||||
export function notifyAuthChanged(type: AuthEventType) {
|
||||
if (typeof window === "undefined") {
|
||||
return;
|
||||
}
|
||||
|
||||
const payload = JSON.stringify({ type, source: getTabId(), at: Date.now() });
|
||||
|
||||
if ("BroadcastChannel" in window) {
|
||||
const channel = new BroadcastChannel(authChannelName);
|
||||
channel.postMessage(payload);
|
||||
channel.close();
|
||||
}
|
||||
|
||||
window.localStorage.setItem(authChannelName, payload);
|
||||
}
|
||||
|
||||
export function listenAuthChanged(callback: (type: AuthEventType) => void) {
|
||||
if (typeof window === "undefined") {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
function handlePayload(payload: unknown) {
|
||||
if (typeof payload !== "string") {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const event = JSON.parse(payload) as { type?: AuthEventType; source?: string };
|
||||
if (event.source === getTabId()) {
|
||||
return;
|
||||
}
|
||||
if (event.type === "login" || event.type === "logout") {
|
||||
callback(event.type);
|
||||
}
|
||||
} catch {
|
||||
// Ignore malformed cross-tab payloads.
|
||||
}
|
||||
}
|
||||
|
||||
const channel = "BroadcastChannel" in window ? new BroadcastChannel(authChannelName) : null;
|
||||
channel?.addEventListener("message", (event) => handlePayload(event.data));
|
||||
|
||||
const storageListener = (event: StorageEvent) => {
|
||||
if (event.key === authChannelName) {
|
||||
handlePayload(event.newValue);
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener("storage", storageListener);
|
||||
|
||||
return () => {
|
||||
channel?.close();
|
||||
window.removeEventListener("storage", storageListener);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
"use client";
|
||||
|
||||
import { browserApiBase } from "./api";
|
||||
import { notifyAuthChanged } from "./authEvents";
|
||||
|
||||
export function logoutByNavigation(redirectTo = "/login") {
|
||||
notifyAuthChanged("logout");
|
||||
window.location.assign(`${browserApiBase}/auth/logout?redirect=${encodeURIComponent(redirectTo)}`);
|
||||
}
|
||||
Reference in New Issue
Block a user