首页 > 解决方案 > React Native AuthSession.startAsync 登录成功后不关闭窗口

问题描述

我正在构建一个带有 react native 和 expo 的 web 应用程序。我正在尝试与 auth0 集成并调用授权 API。当我使用 AuthSession.startAsync 时,它会打开一个新窗口并重定向到 auth0,但是当我完成登录时,它不会将用户重定向回应用程序,而只会重定向到该弹出窗口中的 webapp。当我关闭窗口时,从 authSession.startAsync 返回的响应是:{type: "dismiss"}。它不会从 auth0 URL 获取参数。我不确定我做错了什么。

login = async () => {
    // Retrieve the redirect URL, add this to the callback URL list
    // of your Auth0 application.
    const redirectUrl = AuthSession.getRedirectUrl();
    console.log(`Redirect URL: ${redirectUrl}`);

    // Structure the auth parameters and URL
    let authUrl =
      `${auth0Domain}/authorize` +
      toQueryString({
        client_id: auth0ClientId,
        response_type: "token",
        scope: "openid profile email",
        redirect_uri: redirectUrl,
      });
    console.log(`AuthURL is:  ${authUrl}`);

    // Perform the authentication
    const response: any = await AuthSession.startAsync({
      authUrl: authUrl,
      returnUrl: redirectUrl,
    });
    console.log("Authentication response", response);

    if (response.type === "success") {
      this.handleResponse(response.params);
    }
  };

  handleResponse = (response: any) => {
    if (response.error) {
      Alert.alert(
        "Authentication error",
        response.error_description || "something went wrong"
      );
      return;
    }

    // Retrieve the JWT token and decode it
    const jwtToken = response.id_token;
    const decoded = jwtDecode(jwtToken);

    const name = { decoded };
    this.setState({ name });
  };

标签: react-nativeexpoauth0

解决方案


推荐阅读