fitst commit

This commit is contained in:
Samandar Turgunboyev
2026-01-28 18:26:50 +05:00
parent 166a55b1e9
commit 124798419b
196 changed files with 26627 additions and 421 deletions

View File

@@ -0,0 +1,48 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import { createContext, useContext, useEffect, useState } from 'react';
type AuthContextType = {
isAuthenticated: boolean;
isLoading: boolean;
login: (token: string) => Promise<void>;
logout: () => Promise<void>;
};
const AuthContext = createContext<AuthContextType | undefined>(undefined);
export function AuthProvider({ children }: { children: React.ReactNode }) {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const checkToken = async () => {
const token = await AsyncStorage.getItem('access_token');
setIsAuthenticated(!!token);
setIsLoading(false);
};
checkToken();
}, []);
const login = async (token: string) => {
await AsyncStorage.setItem('access_token', token);
setIsAuthenticated(true);
};
const logout = async () => {
await AsyncStorage.removeItem('access_token');
await AsyncStorage.removeItem('refresh_token');
setIsAuthenticated(false);
};
return (
<AuthContext.Provider value={{ isAuthenticated, isLoading, login, logout }}>
{children}
</AuthContext.Provider>
);
}
export const useAuth = () => {
const context = useContext(AuthContext);
if (!context) throw new Error('useAuth must be used within AuthProvider');
return context;
};