What a mess
This commit is contained in:
85
pwa/src/contexts/AuthContext.tsx
Normal file
85
pwa/src/contexts/AuthContext.tsx
Normal file
@@ -0,0 +1,85 @@
|
||||
import { createContext, useState, useEffect, ReactNode } from 'react'
|
||||
import api from '../services/api'
|
||||
|
||||
interface AuthContextType {
|
||||
isAuthenticated: boolean
|
||||
loading: boolean
|
||||
user: User | null
|
||||
login: (username: string, password: string) => Promise<void>
|
||||
register: (username: string, password: string) => Promise<void>
|
||||
logout: () => void
|
||||
}
|
||||
|
||||
interface User {
|
||||
id: number
|
||||
username: string
|
||||
telegram_id?: string
|
||||
}
|
||||
|
||||
export const AuthContext = createContext<AuthContextType>({
|
||||
isAuthenticated: false,
|
||||
loading: true,
|
||||
user: null,
|
||||
login: async () => {},
|
||||
register: async () => {},
|
||||
logout: () => {},
|
||||
})
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const [isAuthenticated, setIsAuthenticated] = useState(false)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [user, setUser] = useState<User | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
const token = localStorage.getItem('token')
|
||||
if (token) {
|
||||
api.defaults.headers.common['Authorization'] = `Bearer ${token}`
|
||||
fetchUser()
|
||||
} else {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [])
|
||||
|
||||
const fetchUser = async () => {
|
||||
try {
|
||||
const response = await api.get('/api/auth/me')
|
||||
setUser(response.data)
|
||||
setIsAuthenticated(true)
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch user:', error)
|
||||
localStorage.removeItem('token')
|
||||
delete api.defaults.headers.common['Authorization']
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const login = async (username: string, password: string) => {
|
||||
const response = await api.post('/api/auth/login', { username, password })
|
||||
const { access_token } = response.data
|
||||
localStorage.setItem('token', access_token)
|
||||
api.defaults.headers.common['Authorization'] = `Bearer ${access_token}`
|
||||
await fetchUser()
|
||||
}
|
||||
|
||||
const register = async (username: string, password: string) => {
|
||||
const response = await api.post('/api/auth/register', { username, password })
|
||||
const { access_token } = response.data
|
||||
localStorage.setItem('token', access_token)
|
||||
api.defaults.headers.common['Authorization'] = `Bearer ${access_token}`
|
||||
await fetchUser()
|
||||
}
|
||||
|
||||
const logout = () => {
|
||||
localStorage.removeItem('token')
|
||||
delete api.defaults.headers.common['Authorization']
|
||||
setIsAuthenticated(false)
|
||||
setUser(null)
|
||||
}
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ isAuthenticated, loading, user, login, register, logout }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user