react-native - EXPO AuthSession 在 Android 设备上返回关闭

标签 react-native oauth expo

我正在使用 Expo AuthSession 通过我的 IdentityServer4 实现登录我的应用。

当我尝试使用我的 Google 帐户登录我的 IdentityServer 时,我只在 ANDROID 中遇到问题。

AuthSession 在我的设备中打开浏览器,我可以登录,但之后,当我对询问我是否要允许访问的通知回答"is"时,我在 React Native 应用程序中收到的访问被关闭。

我注意到,如果我先在浏览器中注册我的 google 帐户,问题就会消失并且我能够登录。

我使用的代码是这样的:

import * as AuthSession from "expo-auth-session";
import jwtDecode from "jwt-decode";
import * as React from "react";
import { Alert, Button, Platform, StyleSheet, Text, View } from "react-native";

// You need to swap out the Auth0 client id and domain with the one from your Auth0 client.
// In your Auth0 client, you need to also add a url to your authorized redirect urls.
//
// For this application, I added https://auth.expo.io/@arielweinberger/with-auth0 because I am
// signed in as the "arielweinberger" account on Expo and the name/slug for this app is "with-auth0".
//
// You can open this app in the Expo client and check your logs to find out your redirect URL.

const auth0ClientId = "ppSP6KhARumIUKQl5J02GTABifmBdDGy";
const auth0Domain = "expo-auth0.us.auth0.com"
const authorizationEndpoint = `https://${auth0Domain}/authorize`;

const useProxy = Platform.select({ default: false });
const redirectUri = AuthSession.makeRedirectUri({ useProxy, native:"myapp://" });

export default function App() {
  const [name, setName] = React.useState(null);

  const [request, result, promptAsync] = AuthSession.useAuthRequest(
    {
      redirectUri,
      clientId: auth0ClientId,
      // id_token will return a JWT token
      responseType: "id_token",
      // retrieve the user's profile
      scopes: ["openid", "profile"],
      extraParams: {
        // ideally, this will be a random value
        nonce: "nonce",
      },
    },
    { authorizationEndpoint }
  );

  // Retrieve the redirect URL, add this to the callback URL list
  // of your Auth0 application.
  console.log(`Redirect URL: ${redirectUri}`);

  React.useEffect(() => {
    if (result) {
      if (result.error) {
        Alert.alert(
          "Authentication error",
          result.params.error_description || "something went wrong"
        );
        return;
      }
      if (result.type === "success") {
        // Retrieve the JWT token and decode it
        const jwtToken = result.params.id_token;
        const decoded = jwtDecode(jwtToken);

        const { name } = decoded;
        setName(name);
      }
    }
  }, [result]);

  return (
    <View style={styles.container}>
      {name ? (
        <Text style={styles.title}>You are logged in, {name}!</Text>
      ) : (
        <Button
          disabled={!request}
          title="Log in with Auth0"
          onPress={() => promptAsync({ useProxy })}
        />
      )}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  },
  title: {
    fontSize: 20,
    textAlign: "center",
    marginTop: 40,
  },
});

最佳答案

我解决了。当我调用 promptAsync 时,我可以添加一些选项,其中之一是 showInRecents(浏览的网站应该在 Android 多任务处理程序中显示为单独的条目)默认值为 false。将其设置为 true 解决了我的问题。

promptAsync(discovery, { useProxy, showInRecents:true });

关于react-native - EXPO AuthSession 在 Android 设备上返回关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66080607/

相关文章:

react-native - 在react-native中创建CSS圆圈

oauth - 在 ASP.NET Core 2.0 中设置 LinkedIn/OAuth 身份验证

soap - 使用 OAuth 访问 token 访问 SOAP 服务?

android - 独立世博项目中的视频播放器

android - React Native,任务 ':app:mergeDexDebug' 执行失败

react-native - NetInfo已从react-native核心中提取并将被删除

javascript - 当 ListView 放置在 ScrollView 中时,ListView 的 onEndReached 属性不起作用

android - 无法使用谷歌 OAuth 2 获取刷新 token

expo - 如何使用 React Navigation 6 和 expo 制作透明模态

javascript - (React Native) 组件异常 - 文本字符串必须在 <Text> 组件内呈现