ultisuite-client/lib/api/auth-store.ts
R3D347HR4Y 6ec95262af Add OnlyOffice integration and update project configurations
- Updated .env.example to include configuration for OnlyOffice Document Server.
- Modified the workspace configuration to remove the drive-suite path.
- Adjusted TypeScript environment imports for consistency.
- Enhanced Next.js configuration to disable canvas in Webpack.
- Updated package.json to include new dependencies for OnlyOffice and PDF.js.
- Added global styles for OnlyOffice theme integration in the CSS.
- Created new layout and page components for the Drive feature, including public sharing and editing functionalities.
- Updated metadata handling across various layouts to reflect the new app structure.
2026-06-07 15:49:21 +02:00

82 lines
2.1 KiB
TypeScript

"use client"
import { create } from "zustand"
import { persist } from "zustand/middleware"
import { debouncedPersistJSONStorage } from "@/lib/stores/debounced-json-storage"
import type { PlatformUser } from "@/lib/auth/jwt-claims"
const AUTH_STORAGE_KEY = "ulti-auth"
const LEGACY_AUTH_KEYS = ["ultimail-auth", "ultidrive-auth"] as const
export { AUTH_STORAGE_KEY, LEGACY_AUTH_KEYS }
function migrateLegacyAuthStorage() {
if (typeof window === "undefined") return
try {
if (localStorage.getItem(AUTH_STORAGE_KEY)) return
for (const legacy of LEGACY_AUTH_KEYS) {
const raw = localStorage.getItem(legacy)
if (raw) {
localStorage.setItem(AUTH_STORAGE_KEY, raw)
return
}
}
} catch {
/* private mode / quota */
}
}
migrateLegacyAuthStorage()
interface AuthState {
accessToken: string | null
refreshToken: string | null
expiresAt: number | null
user: PlatformUser | null
login: (
accessToken: string,
refreshToken: string,
expiresAt: number,
user?: PlatformUser | null
) => void
logout: () => void
isAuthenticated: () => boolean
}
export const useAuthStore = create<AuthState>()(
persist(
(set, get) => ({
accessToken: null,
refreshToken: null,
expiresAt: null,
user: null,
login: (accessToken, refreshToken, expiresAt, user = null) =>
set({ accessToken, refreshToken, expiresAt, user }),
logout: () =>
set({
accessToken: null,
refreshToken: null,
expiresAt: null,
user: null,
}),
isAuthenticated: () => {
const { accessToken, expiresAt, refreshToken } = get()
if (!accessToken) return false
if (expiresAt && Date.now() < expiresAt) return true
// Access token expired — session may still be renewed via refresh token.
return Boolean(refreshToken)
},
}),
{
name: AUTH_STORAGE_KEY,
storage: debouncedPersistJSONStorage,
partialize: (state) => ({
accessToken: state.accessToken,
refreshToken: state.refreshToken,
expiresAt: state.expiresAt,
user: state.user,
}),
}
)
)