I am redirecting to “Google.com” when tried Google Login in React Native Expo App.

huangapple go评论68阅读模式
英文:

Why I am redirecting to "Google.com" when tried Google Login in React Native Expo App?

问题

I am trying to add Google Login in my Expo React Native application and following official expo documentation from here Auth with Google.

当我通过Expo Play Store App登录时,一切正常,但当我使用EAS服务构建apk时,它会加载一段时间,然后将我重定向到"www.google.com"。

我是个新手,现在已经尝试解决了20多个小时,求帮助。

这是我的登录屏幕代码和其他文件。

import { StyleSheet, View, LogBox, Pressable, Text } from "react-native";
import { useEffect, useState } from "react";
import Fonts from "../utils/Fonts";
import Colors from "../utils/Colors";
import { db } from "../utils/firebase";
import GoogleIcon from "../icons/GoogleIcon";
import * as WebBrowser from "expo-web-browser";
import { LivornLogoIcon } from "../icons/HeaderIcons";
import { ref, onValue } from "firebase/database";
import FormComponent from "../components/FormComponent";
import * as Google from "expo-auth-session/providers/google";
import { useAndroidRipple } from "../hooks/useAndroidRipple";
import AsyncStorage from "@react-native-async-storage/async-storage";
import PrimaryButtonComponent from "../components/PrimaryButtonComponent";

WebBrowser.maybeCompleteAuthSession();
LogBox.ignoreLogs(["The useProxy option is deprecated and will be removed in a future release"]);

export default function LoginScreen({ setUserLoginInfo }) {
  const [token, setToken] = useState("");
  const [inviteCode, setInviteCode] = useState();
  const [isAuthorized, setIsAuthorized] = useState(true);
  const [errorMessage, setErrorMessage] = useState("");

  const [request, response, promptAsync] = Google.useAuthRequest({
    // iosClientId: "",
    androidClientId: // My ID
    expoClientId: // My ID
    webClientId: // My ID
  });

  useEffect(() => {
    handleEffect();
  }, [response, token]);

  async function handleEffect() {
    const user = await getLocalUser();
    if (!user) {
      if (response?.type === "success") {
        setToken(response.authentication.accessToken);
        getUserInfo(response.authentication.accessToken);
      }
    } else {
      setUserLoginInfo(user);
    }
  }

  const getLocalUser = async () => {
    const data = await AsyncStorage.getItem("@user");
    if (!data) return null;
    return JSON.parse(data);
  };

  const getUserInfo = async (token) => {
    if (!token) return;
    try {
      const response = await fetch("https://www.googleapis.com/userinfo/v2/me", {
        headers: { Authorization: `Bearer ${token}` },
      });

      const user = await response.json();
      await AsyncStorage.setItem("@user", JSON.stringify(user));
      setUserLoginInfo(user);
    } catch (error) {
      // Add your own error handler here
    }
  };

  return (
    <View style={styles.container}>
      <LivornLogoIcon scale={6} marginLeft={10} />
      {isAuthorized && (
        <View style={styles.buttonContainer}>
          <Pressable
            disabled={!request}
            style={styles.googleButton}
            onPress={() => promptAsync()}
            android_ripple={useAndroidRipple(170)}
          >
            <GoogleIcon scale={0.35} marginRight={12} />
            <Text style={Fonts.primaryText}>Continue with Google</Text>
          </Pressable>
        </View>
      )}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: 100,
    alignItems: "center",
    justifyContent: "flex-start",
    backgroundColor: Colors.blackTheme.backgroundPrimary,
  },
  buttonContainer: {
    width: "80%",
    marginTop: 20,
    overflow: "hidden",
    borderRadius: 100,
  },
  googleButton: {
    height: 60,
    borderRadius: 100,
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "center",
    backgroundColor: Colors.blackTheme.buttonAndIconColor,
  },
  errorMessage: {
    color: "red",
    marginTop: 20,
    marginBottom: -40,
  },
});

This is my app.json

{
  "expo": {
    "name": "Livorn",
    "slug": "Livorn",
    "version": "1.0.0",
    "orientation": "portrait",
    "icon": "./assets/LivornAdaptiveIcon.png",
    "userInterfaceStyle": "light",
    "splash": {
      "image": "./assets/LivornSplashScreen.png",
      "resizeMode": "contain",
      "backgroundColor": "#000000"
    },
    "updates": {
      "fallbackToCacheTimeout": 0
    },
    "assetBundlePatterns": [
      "**/*"
    ],
    "ios": {
      "supportsTablet": true
    },
    "android": {
      "adaptiveIcon": {
        "foregroundImage": "./assets/LivornAdaptiveIcon.png",
        "backgroundColor": "#000000"
      },
      "package": "company.cubes.Livorn"
    },
    "web": {
      "favicon": "./assets/LivornFavicon.png"
    },
    "scheme": "Livorn",
    "extra": {
      "eas": {
        "projectId": "b198162a-e569-4414-8d5f-86148568b764"
      }
    }
  }
}
英文:

I am trying to add Google Login in my Expo React Native application and following official expo documentation from here Auth with Google.

When I login through Expo Play Store App it is working completely fine, but when I build apk by EAS services it is loading for some time and redirecting me to "www.google.com".

I am a beginner and not able to solve this for more than 20 hours now. Please help.

Here is my Login Screen Code and other files.

import { StyleSheet, View, LogBox, Pressable, Text } from &quot;react-native&quot;;
import { useEffect, useState } from &quot;react&quot;;
import Fonts from &quot;../utils/Fonts&quot;;
import Colors from &quot;../utils/Colors&quot;;
import { db } from &quot;../utils/firebase&quot;;
import GoogleIcon from &quot;../icons/GoogleIcon&quot;;
import * as WebBrowser from &quot;expo-web-browser&quot;;
import { LivornLogoIcon } from &quot;../icons/HeaderIcons&quot;;
import { ref, onValue } from &quot;firebase/database&quot;;
import FormComponent from &quot;../components/FormComponent&quot;;
import * as Google from &quot;expo-auth-session/providers/google&quot;;
import { useAndroidRipple } from &quot;../hooks/useAndroidRipple&quot;;
import AsyncStorage from &quot;@react-native-async-storage/async-storage&quot;;
import PrimaryButtonComponent from &quot;../components/PrimaryButtonComponent&quot;;
WebBrowser.maybeCompleteAuthSession();
LogBox.ignoreLogs([&quot;The useProxy option is deprecated and will be removed in a future release&quot;]);
export default function LoginScreen({ setUserLoginInfo }) {
const [token, setToken] = useState(&quot;&quot;);
const [inviteCode, setInviteCode] = useState();
const [isAuthorized, setIsAuthorized] = useState(true);
const [errorMessage, setErrorMessage] = useState(&quot;&quot;);
const [request, response, promptAsync] = Google.useAuthRequest({
// iosClientId: &quot;&quot;,
androidClientId: // My ID
expoClientId: // My ID
webClientId: // My ID
});
useEffect(() =&gt; {
handleEffect();
}, [response, token]);
async function handleEffect() {
const user = await getLocalUser();
if (!user) {
if (response?.type === &quot;success&quot;) {
setToken(response.authentication.accessToken);
getUserInfo(response.authentication.accessToken);
}
} else {
setUserLoginInfo(user);
}
}
const getLocalUser = async () =&gt; {
const data = await AsyncStorage.getItem(&quot;@user&quot;);
if (!data) return null;
return JSON.parse(data);
};
const getUserInfo = async (token) =&gt; {
if (!token) return;
try {
const response = await fetch(&quot;https://www.googleapis.com/userinfo/v2/me&quot;, {
headers: { Authorization: `Bearer ${token}` },
});
const user = await response.json();
await AsyncStorage.setItem(&quot;@user&quot;, JSON.stringify(user));
setUserLoginInfo(user);
} catch (error) {
// Add your own error handler here
}
};
return (
&lt;View style={styles.container}&gt;
&lt;LivornLogoIcon scale={6} marginLeft={10} /&gt;
{isAuthorized &amp;&amp; (
&lt;View style={styles.buttonContainer}&gt;
&lt;Pressable
disabled={!request}
style={styles.googleButton}
onPress={() =&gt; promptAsync()}
android_ripple={useAndroidRipple(170)}
&gt;
&lt;GoogleIcon scale={0.35} marginRight={12} /&gt;
&lt;Text style={Fonts.primaryText}&gt;Continue with Google&lt;/Text&gt;
&lt;/Pressable&gt;
&lt;/View&gt;
)}
&lt;/View&gt;
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 100,
alignItems: &quot;center&quot;,
justifyContent: &quot;flex-start&quot;,
backgroundColor: Colors.blackTheme.backgroundPrimary,
},
buttonContainer: {
width: &quot;80%&quot;,
marginTop: 20,
overflow: &quot;hidden&quot;,
borderRadius: 100,
},
googleButton: {
height: 60,
borderRadius: 100,
flexDirection: &quot;row&quot;,
alignItems: &quot;center&quot;,
justifyContent: &quot;center&quot;,
backgroundColor: Colors.blackTheme.buttonAndIconColor,
},
errorMessage: {
color: &quot;red&quot;,
marginTop: 20,
marginBottom: -40,
},
});

This is my app.json

{
&quot;expo&quot;: {
&quot;name&quot;: &quot;Livorn&quot;,
&quot;slug&quot;: &quot;Livorn&quot;,
&quot;version&quot;: &quot;1.0.0&quot;,
&quot;orientation&quot;: &quot;portrait&quot;,
&quot;icon&quot;: &quot;./assets/LivornAdaptiveIcon.png&quot;,
&quot;userInterfaceStyle&quot;: &quot;light&quot;,
&quot;splash&quot;: {
&quot;image&quot;: &quot;./assets/LivornSplashScreen.png&quot;,
&quot;resizeMode&quot;: &quot;contain&quot;,
&quot;backgroundColor&quot;: &quot;#000000&quot;
},
&quot;updates&quot;: {
&quot;fallbackToCacheTimeout&quot;: 0
},
&quot;assetBundlePatterns&quot;: [
&quot;**/*&quot;
],
&quot;ios&quot;: {
&quot;supportsTablet&quot;: true
},
&quot;android&quot;: {
&quot;adaptiveIcon&quot;: {
&quot;foregroundImage&quot;: &quot;./assets/LivornAdaptiveIcon.png&quot;,
&quot;backgroundColor&quot;: &quot;#000000&quot;
},
&quot;package&quot;: &quot;company.cubes.Livorn&quot;
},
&quot;web&quot;: {
&quot;favicon&quot;: &quot;./assets/LivornFavicon.png&quot;
},
&quot;scheme&quot;: &quot;Livorn&quot;,
&quot;extra&quot;: {
&quot;eas&quot;: {
&quot;projectId&quot;: &quot;b198162a-e569-4414-8d5f-86148568b764&quot;
}
}
}
}

答案1

得分: 2

将 "slug" 和 "scheme" 都转换为小写并相等。这将解决问题。

英文:

In app.json.

Make slug and scheme both to lowercase and equal. It would solve the problem.

huangapple
  • 本文由 发表于 2023年5月7日 03:01:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190617.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定