Back

Invalid redirect with github oauth2 flow (ssr)

  • 0
  • Auth
  • Web
  • Cloud
Riccardo Perra
20 Oct, 2024, 19:11

Error:

TypeScript
AppwriteException: Invalid redirect\n    at _Client.redirect 

Code:

TypeScript
  const origin = getHeaders().origin;
  const successUrl = `${origin}/api/oauth`;
  const failureUrl = `${origin}/`;
  const redirectUrl = await account.createOAuth2Token(
    OAuthProvider.Github,
    successUrl,
    failureUrl,
  );
  return redirect(redirectUrl);

i'm receiving this error while the origin is my real domain. Everything is working in dev mode with localhost:3000

I'm using latest version of node-appwrite. This code is entirely run by node.

My client is built like the documentation says:

TypeScript
import {Client} from 'node-appwrite';
  const client = new Client()
    .setProject(projectId)
    .setEndpoint(endpoint)
    .setKey(apiKey);
TL;DR
Developers had an issue with Github OAuth2 redirect error due to incorrect env variable retrieval. The problem was resolved by updating the way env variables were accessed. The error stemmed from using the wrong URLs in different environments. The GitHub OAuth flow was fixed by adjusting the URLs to match the correct environment. Make sure to verify the node-appwrite SDK version in the package.json file.
Steven
20 Oct, 2024, 19:14

i know you're saying you're using the latest version of the node-appwrite SDK, but would you please check the package in your node_modules to verify what version is installed?

Riccardo Perra
20 Oct, 2024, 19:15

it's 14.1.0

Riccardo Perra
20 Oct, 2024, 19:16

I did a test also with 15-rc1

Steven
20 Oct, 2024, 19:16

and you're saying it works locally, but not after deploying?

Riccardo Perra
20 Oct, 2024, 19:17

yes, using localhost as success/failure url works fine. Once i put my real domain, i got that error (also in dev mode)

Steven
20 Oct, 2024, 19:17

have you double checked your env vars are set up?

Steven
20 Oct, 2024, 19:18

and you've added a web platform to your project?

Steven
20 Oct, 2024, 19:19

can you share the URL of your app?

Riccardo Perra
20 Oct, 2024, 19:33

Just added some logs in my server functions and I noticed that in prod build I was retrieving the env variables wrongly (import.meta.env instead of process.env) πŸ€¦β€β™‚οΈ

I had the cloud url undefined πŸ˜„ everything is working fine now.

Riccardo Perra
20 Oct, 2024, 19:36

thanks for the quick response

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more