Jack TFBOriginal post
React Developer
I am trying to set up a "Sign in with Google" button using OAuth2. While I was successful in getting the button to work for web, when I try with mobile I get the error "Invalid 'success' param: URL host must be of: ..." I was able to get it working for web fine without this error, but for my android app, I get this error. Is it due to a wrong configuration?
Here is my code for the login with Google function
async function loginWithGoogle() {
try {
const redirectUri = Linking.createURL("/");
console.log("Platform:", Platform.OS);
console.log("Redirect URI:", redirectUri);
if (Platform.OS === 'web') {
const response = account.createOAuth2Session(
OAuthProvider.Google,
redirectUri,
redirectUri
);
if (!response) throw new Error("Failed to create OAuth2 session");
const browserResult = await openAuthSessionAsync(
response.toString(),
redirectUri
);
if(browserResult.type !== "success") {
throw new Error("OAuth authentication was cancelled or failed");
}
const currentSession = await account.getSession('current');
if (!currentSession) {
throw new Error("No session found after OAuth authentication");
}
return currentSession;
} else {
const response = account.createOAuth2Token(
OAuthProvider.Google,
redirectUri,
redirectUri
);
if (!response) throw new Error("Create OAuth2 token failed");
const browserResult = await openAuthSessionAsync(
response.toString(),
redirectUri
);
if (browserResult.type !== "success")
throw new Error("Create OAuth2 toekn failed");
console.log("Browser Result: ", browserResult);
const url = new URL(browserResult.url);
const secret = url.searchParams.get("secret")?.toString();
const userId = url.searchParams.get("userId")?.toString();
if (!secret || !userId) throw new Error("Create OAuth2 token failed");
await account.createSession(userId, secret);
const currentSession = await account.getSession('current');
if (!currentSession) {
throw new Error("Failed to create session after OAuth authentication");
}
return currentSession;
}
} catch (error) {
console.error("Login error:", error);
return null;
}
}
Summary
Developers trying to set up "Sign in with Google" button in React Native Android app are encountering an "Invalid 'success' param" error due to incorrect configuration. The issue seems to arise from OAuth2's partial support in React Native. To fix the problem, developers can refer to the provided GitHub comment for more information on OAuth2 support status.