mmkv bug fix
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
// axiosInstance
|
||||
import axios from 'axios';
|
||||
import { authEvents, getLanguage, getToken, setToken } from 'helpers/event';
|
||||
|
||||
let isLoggingOut = false;
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import axios, { AxiosError } from 'axios';
|
||||
import { navigate } from 'components/NavigationRef';
|
||||
|
||||
const axiosInstance = axios.create({
|
||||
baseURL: 'https://api.cpcargo.uz/api/v1',
|
||||
@@ -12,38 +10,30 @@ const axiosInstance = axios.create({
|
||||
},
|
||||
});
|
||||
|
||||
axiosInstance.interceptors.request.use(config => {
|
||||
const token = getToken();
|
||||
const lang = getLanguage();
|
||||
|
||||
axiosInstance.interceptors.request.use(async config => {
|
||||
// Tokenni olish
|
||||
const token = await AsyncStorage.getItem('token');
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
if (lang) {
|
||||
config.headers['Accept-Language'] = lang;
|
||||
// Language’ni olish
|
||||
const language = await AsyncStorage.getItem('language');
|
||||
if (language) {
|
||||
config.headers['Accept-Language'] = language;
|
||||
}
|
||||
|
||||
return config;
|
||||
});
|
||||
|
||||
axiosInstance.interceptors.response.use(
|
||||
res => res,
|
||||
err => {
|
||||
const status = err.response?.status;
|
||||
|
||||
if (status === 401 && !isLoggingOut) {
|
||||
isLoggingOut = true;
|
||||
|
||||
setToken(null);
|
||||
authEvents.emit('logout');
|
||||
|
||||
setTimeout(() => {
|
||||
isLoggingOut = false;
|
||||
}, 500);
|
||||
response => response,
|
||||
async (error: AxiosError) => {
|
||||
if (error.response?.status === 401) {
|
||||
await AsyncStorage.removeItem('token');
|
||||
navigate('Login');
|
||||
}
|
||||
|
||||
return Promise.reject(err);
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
// helpers/event.ts
|
||||
import { EventEmitter } from 'events';
|
||||
import { createMMKV } from 'react-native-mmkv';
|
||||
|
||||
export const authEvents = new EventEmitter();
|
||||
|
||||
export const enum AUTH {
|
||||
USER = 'user',
|
||||
AUTH = 'auth',
|
||||
}
|
||||
|
||||
// MMKV instance
|
||||
export const storage = createMMKV();
|
||||
|
||||
// Memory cache
|
||||
let tokenCache: string | null = null;
|
||||
let languageCache: string | null = null;
|
||||
|
||||
// Load on app start (synchronous)
|
||||
export function loadInitialAuthData() {
|
||||
tokenCache = storage.getString('token') || null;
|
||||
languageCache = storage.getString('language') || null;
|
||||
}
|
||||
|
||||
export function getToken() {
|
||||
return storage.getString('token');
|
||||
}
|
||||
|
||||
export function setToken(token: string | null) {
|
||||
tokenCache = token;
|
||||
if (token) {
|
||||
storage.set('token', token);
|
||||
} else {
|
||||
storage.remove('token');
|
||||
}
|
||||
}
|
||||
|
||||
export function saveAuth() {
|
||||
storage.set('user', AUTH.USER);
|
||||
}
|
||||
|
||||
export function removeAuth() {
|
||||
storage.set('user', AUTH.AUTH);
|
||||
}
|
||||
|
||||
export function getAuth() {
|
||||
storage.getString('user');
|
||||
}
|
||||
|
||||
export function getLanguage() {
|
||||
return languageCache;
|
||||
}
|
||||
|
||||
export function setLanguage(lang: string) {
|
||||
languageCache = lang;
|
||||
storage.set('language', lang);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import { useNavigation } from '@react-navigation/native';
|
||||
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
@@ -5,7 +6,6 @@ import { authApi } from 'api/auth';
|
||||
import { otpPayload, resendPayload } from 'api/auth/type';
|
||||
import AppText from 'components/AppText';
|
||||
import ErrorNotification from 'components/ErrorNotification';
|
||||
import { saveAuth, setToken } from 'helpers/event';
|
||||
import formatPhone from 'helpers/formatPhone';
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
@@ -77,12 +77,8 @@ const Confirm = () => {
|
||||
const { mutate, isPending } = useMutation({
|
||||
mutationFn: (payload: otpPayload) => authApi.verifyOtp(payload),
|
||||
onSuccess: async res => {
|
||||
setToken(res.data.accessToken);
|
||||
saveAuth();
|
||||
navigation.reset({
|
||||
index: 0,
|
||||
routes: [{ name: 'Home' }], // login sahifasiga qaytarish
|
||||
});
|
||||
await AsyncStorage.setItem('token', res.data.accessToken);
|
||||
navigation.navigate('Home');
|
||||
setVisible(false);
|
||||
},
|
||||
onError: (err: any) => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
// import { getApp } from '@react-native-firebase/app';
|
||||
// import { getMessaging, getToken } from '@react-native-firebase/messaging';
|
||||
import { useNavigation } from '@react-navigation/native';
|
||||
@@ -7,7 +8,6 @@ import { authApi } from 'api/auth';
|
||||
import { otpPayload, resendPayload } from 'api/auth/type';
|
||||
import AppText from 'components/AppText';
|
||||
import ErrorNotification from 'components/ErrorNotification';
|
||||
import { setToken } from 'helpers/event';
|
||||
import formatPhone from 'helpers/formatPhone';
|
||||
import React, { useRef, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
@@ -83,11 +83,8 @@ const Confirm = ({
|
||||
const { mutate, isPending } = useMutation({
|
||||
mutationFn: (payload: otpPayload) => authApi.verifyOtp(payload),
|
||||
onSuccess: async res => {
|
||||
setToken(res.data.accessToken);
|
||||
navigation.reset({
|
||||
index: 0,
|
||||
routes: [{ name: 'Home' }], // login sahifasiga qaytarish
|
||||
});
|
||||
await AsyncStorage.setItem('token', res.data.accessToken);
|
||||
navigation.navigate('Confirm');
|
||||
setErrorConfirm(null);
|
||||
},
|
||||
onError: (err: any) => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import { useNavigation } from '@react-navigation/native';
|
||||
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
|
||||
import AppText from 'components/AppText';
|
||||
import { removeAuth, setToken } from 'helpers/event';
|
||||
import * as React from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import {
|
||||
@@ -36,8 +36,7 @@ const ProfilePages = (props: componentNameProps) => {
|
||||
style: 'destructive',
|
||||
onPress: async () => {
|
||||
try {
|
||||
setToken('');
|
||||
removeAuth();
|
||||
await AsyncStorage.removeItem('token');
|
||||
navigation.reset({
|
||||
index: 0,
|
||||
routes: [{ name: 'Login' }], // login sahifasiga qaytarish
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import AnimatedScreen from 'components/AnimatedScreen';
|
||||
import { getLanguage, storage } from 'helpers/event';
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import FirstStep from 'screens/welcome/FirstStep';
|
||||
import SecondStep from 'screens/welcome/SecondStep';
|
||||
@@ -17,7 +17,7 @@ const Onboarding = ({ onFinish }: OnboardingProps) => {
|
||||
|
||||
useEffect(() => {
|
||||
const loadLang = async () => {
|
||||
const savedLang = getLanguage();
|
||||
const savedLang = await AsyncStorage.getItem('selectedLanguage');
|
||||
if (savedLang === 'ru' || savedLang === 'uz') {
|
||||
setLang(savedLang);
|
||||
setStep(1);
|
||||
@@ -30,7 +30,7 @@ const Onboarding = ({ onFinish }: OnboardingProps) => {
|
||||
if (step < 3) {
|
||||
setStep(step + 1);
|
||||
} else {
|
||||
storage.set('hasSeenOnboarding', 'true');
|
||||
await AsyncStorage.setItem('hasSeenOnboarding', 'true');
|
||||
onFinish();
|
||||
}
|
||||
}, [step, onFinish]);
|
||||
|
||||
Reference in New Issue
Block a user