Files
hotelsync/src/contexts/AuthContext.tsx
HotelSync b277e3e38d Add Availability Calendar, Modules system, and route refactoring
- Add AvailabilityPage with price grid (categories × dates), drag-select,
  channel prices, EditPanel, PeriodModal, and rate periods tab
- Add ModulesContext with localStorage persistence and dynamic sidebar items
- Add ModulesPage with WiFi Auth, Payments, TV Welcome, Smart Locks,
  OLAP Reports, Website Builder, Booking Widget modules
- Add ReportsPage (OLAP analytics with KPI cards, charts, tables)
- Add WebsitePage and BookingWidgetPage placeholders
- Remove hotel slug from URL routing; hotel context from JWT
- Add Доступность nav item in Sidebar under Управление
- Fix LoginPage redirects and HotelSync branding

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-11 14:43:18 +03:00

48 lines
1.5 KiB
TypeScript

import { createContext, useContext, useState } from 'react'
import type { User, AuthSession } from '../types'
import { MOCK_USERS } from '../data/mockData'
interface AuthContextValue {
session: AuthSession | null
user: User | null
login: (email: string, password: string) => Promise<User | null>
logout: () => void
}
const AuthContext = createContext<AuthContextValue | null>(null)
export function AuthProvider({ children }: { children: React.ReactNode }) {
const [session, setSession] = useState<AuthSession | null>(() => {
const stored = sessionStorage.getItem('hotelsync-session')
return stored ? JSON.parse(stored) : null
})
const login = async (email: string, _password: string): Promise<User | null> => {
// Mock authentication — in production, call POST /auth/login
await new Promise(r => setTimeout(r, 800))
const user = MOCK_USERS.find(u => u.email.toLowerCase() === email.toLowerCase())
if (!user) return null
const s: AuthSession = { user, token: 'mock-jwt-token-' + user.id }
setSession(s)
sessionStorage.setItem('hotelsync-session', JSON.stringify(s))
return user
}
const logout = () => {
setSession(null)
sessionStorage.removeItem('hotelsync-session')
}
return (
<AuthContext.Provider value={{ session, user: session?.user ?? null, login, logout }}>
{children}
</AuthContext.Provider>
)
}
export function useAuth() {
const ctx = useContext(AuthContext)
if (!ctx) throw new Error('useAuth must be used within AuthProvider')
return ctx
}