Back

Unable to create an oAuth2 session

  • 0
  • General
  • Accounts
  • Web
Drake
14 Jul, 2023, 04:25

No, Appwrite needs to be a subdomain of your app

TL;DR
User is having trouble creating an oAuth2 session. They are getting an error that the URL host must be either localhost or the appwrite subdomain. They are unsure about the initial setup and where to make changes. They have implemented oAuth and it is now working. Solution: Appwrite needs to be a subdomain of the app, not a separate domain. They should follow the steps in the provided documentation for creating custom domains.
kathelia.dokgu
14 Jul, 2023, 04:28

Oh.. would it be possible at all to have both as a separate TLD? When I hosted Appwrite, I planned on using it as a BaaS for all the projects that I'll be doing. And those projects are not related to each other. And that's why I installed Appwrite on a completely separate VM/domain name. I want it to be my personal Firebase for all the projects I'll be working on.

kathelia.dokgu
14 Jul, 2023, 04:28

Was an instance only meant for a single project?

Drake
14 Jul, 2023, 04:28

You can make custom domains for each project

Drake
14 Jul, 2023, 04:30

No you have a generic appwrite.example.com domain for your Appwrite instance (the domain env vars in your .env file). Then, for each project that needs it, you'll create a custom domain in the project settings.

See https://appwrite.io/docs/custom-domains

kathelia.dokgu
14 Jul, 2023, 04:37

Yeah I've already seen that. It's late, I'm gonna look at this again tomorrow. I understand that I need Appwrite to be a subdomain of my app so that the cookies will look like it's coming from my app. But I'm having difficulties understanding what I need to do. I mean I do understand once I've deployed my app to the cloud. I'll just add a CNAME to my DNS for a subdomain to point to my self hosted instance. But during development where I'm only working on localhost, I'm like stumped and I don't know what I first need to do.

Drake
14 Jul, 2023, 04:40

On localhost, if both your app and Appwrite are on localhost, it should work

kathelia.dokgu
14 Jul, 2023, 04:41

No, my Appwrite is already hosted on the cloud and I'm already accessing it during local development. I have a project labeled development so I don't use it for production, eventually I'll duplicate the project for a production environment. The only thing on localhost is my NextJS app that I'm trying to build.

Drake
14 Jul, 2023, 04:42

Oh well then it's probably a 3rd party cookie problem

kathelia.dokgu
14 Jul, 2023, 04:42

So would you advice that I spin up a local install of appwrite during development and only use the self hosted domain for production?

Drake
14 Jul, 2023, 04:44

In general, yes, I would use a separate instance for dev vs prod.

kathelia.dokgu
14 Jul, 2023, 04:46

Ugh.. that's a bummer.. it's not really how I tried to set all of my projects up. My idea was to host appwrite on the cloud and use that for both development and production (including dev and prod for other projects that aren't related to my main app).. they'll just be separate projects. I guess I need to rethink my plan. Thanks!

Drake
14 Jul, 2023, 04:47

The big reason why is when you upgrade, you should upgrade your dev instance and test thoroughly. Then, upgrade prod

kathelia.dokgu
14 Jul, 2023, 04:48

Oh yeah that is true. Okay, perhaps tomorrow or during the weekend, I'll try to plan this out again. Thank you for your help!

kathelia.dokgu
14 Jul, 2023, 05:38

I've done all the steps in this doc https://appwrite.io/docs/custom-domains and I made sure that I'm using the new subdomain as the appwrite endpoint when creating the client. I'm not talking about localhost here, I deployed what I currently have on the cloud and it's displaying the button to sign in to Google, but when I click it, I get this error:

Invalid success: URL host must be one of: localhost, appwrite.myapp.com

Drake
14 Jul, 2023, 12:59

What's your code?

kathelia.dokgu
14 Jul, 2023, 15:07
TypeScript
import { MouseEvent, useEffect, useState } from 'react';
import { Models } from 'appwrite';
import { account } from '@/clients/appwrite';
import Constants from '@/utilities/constants';

const Home = () => {
  const [session, setSession] = useState<Models.User<Models.Preferences> | undefined>(undefined);

  const checkSession = async () => {
    const validSession = await account.get();

    if (validSession) {
      setSession(validSession);
    }
  };

  useEffect(() => {
    checkSession();
  }, []);

  const googleAuth = async (e: MouseEvent<HTMLButtonElement>) => {
    e.preventDefault();

    account.createOAuth2Session('google', Constants.BASE_URL, Constants.BASE_URL);
    const newSession = await account.get();

    setSession(newSession);
  };

  const handleLogout = async (e: MouseEvent<HTMLButtonElement>) => {
    e.preventDefault();

    await account.deleteSessions();
    setSession(undefined);
  };

  return (
    <div>
      {session ? (
        <button
          type='button'
          onClick={handleLogout}>
          Logout
        </button>
      ) : (
        <button
          type='button'
          onClick={googleAuth}>
          Sign in with Google
        </button>
      )}
    </div>
  );
};

export default Home;
kathelia.dokgu
14 Jul, 2023, 15:08
TypeScript
import { Client, Account, Storage } from 'appwrite';
import Constants from '@/utilities/constants';

const client = new Client().setEndpoint(Constants.APPWRITE_BASE_URL).setProject(Constants.APPWRITE_PROJECT_ID);

export const account = new Account(client);
export const storage = new Storage(client);

export default client;
Drake
14 Jul, 2023, 16:04

can you try logging Constants.BASE_URL before account.createOAuth2Session('google', Constants.BASE_URL, Constants.BASE_URL);?

kathelia.dokgu
14 Jul, 2023, 16:55

It's showing my app's domain name: https://myapp.com.

Drake
14 Jul, 2023, 17:02

So did you add that hostname as a web platform in your project?

kathelia.dokgu
14 Jul, 2023, 18:01

Where do I go to change that? I don't remember what I initially set it to. I might have set it as localhost because I was on a development environment.

kathelia.dokgu
14 Jul, 2023, 18:03

Nevermind I found it.

kathelia.dokgu
14 Jul, 2023, 18:03

Now it works.

kathelia.dokgu
14 Jul, 2023, 18:05

Okay so I was able to implement oAuth. I still need to figure out how I'm going to set my projects up but thank you for your help!

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