import {useState, useEffect} from 'react';
import { AsyncStorage } from 'react-native'
export const Authing = () => {
const [auth, setAuth] = useState({});
useEffect(async () => {
const response = await getAuth();
if(!response){
return setAuth({loggined: false})
}
setAuth({loggined: true})
}, [auth]);
const toAuth = async (data) => {
try {
await AsyncStorage.setItem('user', JSON.stringify(data));
setAuth({loggined: true})
} catch (e) {
console.log('Ошибка при авторизации')
}
};
const getAuth = async () => {
try {
await AsyncStorage.getItem('user');
const toJson = JSON.parse(response);
console.log('Данные получены успешно')
return toJson
} catch (e) {
console.log('Ошибка при получений данных')
}
};
const longOut = async () => {
try {
await AsyncStorage.clear('user');
setAuth({loggined: false});
console.log('Выход прошел успешно')
} catch (e) {
console.log('Ошибка при выходе')
}
};
return {auth, toAuth, longOut, getAuth}
};
destroy;