React 原生 AsyncStorage 在用 expo 制作的 apk 中不起作用

问题描述 投票:0回答:1

我是expo的新手,我开发了一个应用程序,可以在expo go中按预期工作,但是当我使用eas build创建apk时,应用程序无法正常工作,当我打开应用程序时,它会将我重定向到登录名,直到那时一切很好,当我登录时,我应该获得一个存储在 AsyncStorage 中的令牌,存储后它会将我重定向到主页,但是应用程序不会重定向我,我检查了 api 是否给了我令牌,以及是否将其提供给我,因此我认为 asyncStorage 没有执行其功能。任务正确并且不允许我重定向到主页。这是我的登录码

import * as AuthSession from "expo-auth-session";
import { useEffect, useState } from "react";
import * as WebBrowser from "expo-web-browser";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { ThemedView } from "@/components/ThemedView";
import { ThemedText } from "@/components/ThemedText";
import { styles } from "@/Styles/styles";
import { router, useSegments } from "expo-router";
import { useStateValue } from "@/Context/store";
import * as Linking from 'expo-linking';

import { checkToken, getAccessToken } from "@/Api/SpotifyAuth";

WebBrowser.maybeCompleteAuthSession();
export default function login() {
  const [{ sesionUsuario }, dispatch] = useStateValue();
  const [TOKEN, setToken] = useState<string | null>('')

  const [CODE, setCode] = useState('');
  const URI = AuthSession.makeRedirectUri({
    native: "myapp://",
    path: "/login",
  });
  const endpoints = {
    authorizationEndpoint: "https://accounts.spotify.com/authorize",
    tokenEndpoint: "https://accounts.spotify.com/api/token",
  };
  console.log(Linking.createURL('login'))
  const [request, response, promptAsync] = AuthSession.useAuthRequest(
    {
      clientId: process.env.EXPO_PUBLIC_CLIENTE_ID || "",
      scopes: ["user-read-email", "user-read-private", "user-top-read"],
      responseType: "code",
      redirectUri:  Linking.createURL('login') ,
      usePKCE: false,
    },
    endpoints
  );
    const storeData = async (key: string, data: string) => {
      try {


        await AsyncStorage.setItem(key, data);
      } catch (error) {
        console.log(error)
        throw new Error('${error}');
      }

  };

  const handleResponse = async (code: string) => {
    const { data } : any = await getAccessToken(code, dispatch);
    const { refresh_token, access_token, expira } = data;
     checkToken(expira);
  if (access_token) {
      await storeData("token", access_token)
      await storeData("refresh_token", refresh_token)
      router.replace("/(tabs)");
  }
   
  };
  useEffect(()=>{
    AsyncStorage.getItem('token').then((value : string | null)=>{
      setToken(value);
    }).catch((e:any)=>{
      throw new Error('${e}')
    })
  },[])

  useEffect(() => {
    
    if (response?.type === "success") {
      const { code } = response.params;
      setCode(code)
      handleResponse(code);

    }
  }, [response]);

return (
    <ThemedView style={styles.stepContainer}>
      <ThemedText type="subtitle">Conecta tu cuenta de Spotify</ThemedText>
      <Button title="Conectar" onPress={() => promptAsync()} />
      <Text>Hola</Text>
       <ThemedText>Token:{TOKEN}</ThemedText>
      <ThemedText>{Linking.createURL('login')}</ThemedText>
    </ThemedView>
  );
}

我在github上读到我可以添加inlineRequires: false,在metro.config.js中将此线性添加到文件中,安装apk,但它仍然无法正常工作


const defaultConfig = getDefaultConfig(__dirname);

module.exports = {
  ...defaultConfig,
  transformer: {
    ...defaultConfig.transformer,
    getTransformOptions: async () => ({
      transform: {
        inlineRequires: false,
      },
    }),
  },
};```

android react-native expo apk eas
1个回答
0
投票

对于redirectURI,您应该指定方案(app.json 中的方案)和路径,最好检查您是否在开发环境中。

它看起来像这样:

    const redirectUri = makeRedirectUri(
        __DEV__ ? undefined : { scheme: "your.schema: "auth" },
    );

这样你就可以直接将redirectUri传递给useAuthRequest。 另外,最好使用 SecureStore 作为您的 accessToken 和 refreshToken。

我希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.