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;
}
}
OAuth2 with React Native is not fully supported. The GitHub comment explains the current state: https://github.com/appwrite/sdk-for-react-native/issues/34#issuecomment-2654940715
Gotcha, thank you!
Recommended threads
- Websocket error Realtime
Hi peeps, I am trying to create realtime updates and I am having websocket error Code is below this msg
- {"code": 1008, "message": "Invalid Origi...
Nothing has changed in my application or console settings so I'm curious as to what I need to do to fix this. I already have the client registered so I'm not en...
- React Native/iOS platform integrations h...
Anyone else have this issue where platform identifiers have been lost/wiped and no option/field available to update them in the console?