Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import { isTauri } from "@/lib/api-base";
import { installAccountTransitionListener } from "@/lib/account-transition";
import { getLoginMode } from "./login-client";
import {
AUTH_SESSION_CLEARED_EVENT,
AUTH_SESSION_STORED_EVENT,
} from "./session-events";
export { AUTH_SESSION_CLEARED_EVENT, AUTH_SESSION_STORED_EVENT } from "./session-events";
export const AUTH_TOKEN_KEY = "unsloth_auth_token";
export const AUTH_REFRESH_TOKEN_KEY = "unsloth_auth_refresh_token";
export const AUTH_MUST_CHANGE_PASSWORD_KEY = "unsloth_auth_must_change_password";
/**
* Cross-tab counterpart to `authSessionEpoch`: written when a session begins, removed when it
* ends, never on refresh, so a `storage` listener can tell a switch from a rotation.
*/
export const AUTH_SESSION_MARK_KEY = "unsloth_auth_session_mark";
if (typeof window !== "undefined" && typeof window.addEventListener === "function") {
installAccountTransitionListener();
}
let authSessionEpoch = 0;
/** Stable across access-token refreshes; changes when an auth session starts or clears. */
export function getAuthSessionEpoch(): number {
return authSessionEpoch;
}
type PostAuthRoute = "/change-password" | "/chat";
function canUseStorage(): boolean {
return typeof window !== "undefined";
}
export function hasAuthToken(): boolean {
if (!canUseStorage()) return false;
return Boolean(localStorage.getItem(AUTH_TOKEN_KEY));
}
export function hasRefreshToken(): boolean {
if (!canUseStorage()) return false;
return Boolean(localStorage.getItem(AUTH_REFRESH_TOKEN_KEY));
}
export function getAuthToken(): string | null {
if (!canUseStorage()) return null;
return localStorage.getItem(AUTH_TOKEN_KEY);
}
export function getRefreshToken(): string | null {
if (!canUseStorage()) return null;
return localStorage.getItem(AUTH_REFRESH_TOKEN_KEY);
}
export function getAuthSessionMark(): string | null {
if (!canUseStorage()) return null;
return localStorage.getItem(AUTH_SESSION_MARK_KEY);
}
/** Give sessions created before session markers the same cross-window boundary as new ones. */
export function ensureAuthSessionMark(): string | null {
if (!canUseStorage()) return null;
let mark = getAuthSessionMark();
if (!mark) {
mark = `${Date.now()}.${Math.random()}`;
localStorage.setItem(AUTH_SESSION_MARK_KEY, mark);
}
return mark;
}
export function storeAuthTokens(
accessToken: string,
refreshToken: string,
preserveSessionMark = false,
): void {
// must_change_password is set via setMustChangePassword() so CodeQL does not flag the JWT writes.
if (!canUseStorage()) return;
const sessionStarted = !localStorage.getItem(AUTH_TOKEN_KEY);
localStorage.setItem(AUTH_TOKEN_KEY, accessToken);
localStorage.setItem(AUTH_REFRESH_TOKEN_KEY, refreshToken);
if (sessionStarted) {
authSessionEpoch += 1;
if (!preserveSessionMark || !getAuthSessionMark()) {
localStorage.setItem(
AUTH_SESSION_MARK_KEY,
`${Date.now()}.${Math.random()}`,
);
}
window.dispatchEvent(new Event(AUTH_SESSION_STORED_EVENT));
} else if (!localStorage.getItem(AUTH_SESSION_MARK_KEY)) {
// Backfill the key for pre-existing sessions so a later sign-out has something to remove (and
// fire a storage event). Not a new session, so no epoch bump.
localStorage.setItem(
AUTH_SESSION_MARK_KEY,
`${Date.now()}.${Math.random()}`,
);
}
}
export function clearAuthTokens(): void {
if (!canUseStorage()) return;
authSessionEpoch += 1;
localStorage.removeItem(AUTH_TOKEN_KEY);
localStorage.removeItem(AUTH_REFRESH_TOKEN_KEY);
localStorage.removeItem(AUTH_MUST_CHANGE_PASSWORD_KEY);
localStorage.removeItem(AUTH_SESSION_MARK_KEY);
window.dispatchEvent(new Event(AUTH_SESSION_CLEARED_EVENT));
}
// Stored as key presence so CodeQL does not flow must_change_password into setItem.
// The value is a route hint, not a secret.
export function mustChangePassword(): boolean {
if (!canUseStorage()) return false;
return localStorage.getItem(AUTH_MUST_CHANGE_PASSWORD_KEY) !== null;
}
export function setMustChangePassword(required: boolean): void {
if (!canUseStorage()) return;
if (required) {
localStorage.setItem(AUTH_MUST_CHANGE_PASSWORD_KEY, "1");
} else {
localStorage.removeItem(AUTH_MUST_CHANGE_PASSWORD_KEY);
}
}
export function getPostAuthRoute(): PostAuthRoute {
if (isTauri && getLoginMode() === "single") return "/chat";
if (mustChangePassword()) return "/change-password";
return "/chat";
}
/** Until a required password change lands, the backend 403s every other route. */
export function hasSettledAuthSession(): boolean {
return hasAuthToken() && getPostAuthRoute() === "/chat";
}