#[SOLVED] Overriding cloud provider requirement

78 messages · Page 1 of 1 (latest)

gray vortex
#

I am trying to build the Appwrite adapter for NextAuth.js which provides an easy setup to add authentication to NextJS, Svelte, etc. using different providers and adapters. The adapter connects the database with the front end and you don't have to write any query to create, update, delete, and read any user, session, or token. Adapters have methods defined to query the database.

I created the adapter and tested it locally in isolation and in integration with NextJS, everything worked as expected now I had to write the script which can create a database, create all the collections with attributes and indexes, and create relationships. So far so good but I got stuck at one point, in the test we cannot use credentials to create a project or use an existing project because it will be tested by many different members at NextAuth and they do not want to create an account and project just for testing this adapter. So they said, "We need a way to test it locally without any cloud provider requirement, like Firebase local emulator". As far as I know, even for using self-hosted Appwrite I will need to log in to create a project. Now is there any way we can override authentication? or any alternative

I hope my problem is clear, if not then please let me know. Thanks

sharp pilot
#

You mean like for example, to create the database, collection, etc you use the server SDK, you don't even want to use the token?
Because if you use a token, and then give all permission to the collection you won't need authentication.

In your case, you want to avoid any authentication? (I don't know Fibrebase local emulator)

gray vortex
gray vortex
sharp pilot
gray vortex
#

Yes it will run locally.

#

Is there a way I can set the project ID in the compose file?

umbral hill
#

It's like trying to test a MariaDB database without installing it 🤷‍♂️

sharp pilot
gray vortex
sharp pilot
gray vortex
umbral hill
gray vortex
#

Then do I have to login to create a project? Because last time I checked I had to login to create a project

umbral hill
#

Yes, you need to login in your local instance obviously for security reasons. I think supabase doesn't have that, but what's the advantage of omitting that? Also it's not that difficult to login 😅

gray vortex
#

I know it's not difficult to login but NextAuth members do not want to login just to test the adapter. All the adapters they currently have runs the tests without signing into the local instances.

#

It should be a fully automated process

#

I wish I could use my creds in script to authenticate but that's not gonna happen

sharp pilot
gray vortex
#

Yes I actually checked the table and it also had the team relationship so maybe it is needed right? or I can just create the project without any team is that possible?

sharp pilot
#

I'm not sure, but I think isn't necessary to have a team, just as you don't need a team to create an account

gray vortex
#

Aha gotcha! Worth a try. Will keep you posted thanks 🙏

gray vortex
#

Update:
I tried to back door myself in the DB using some script here and there. But then as I keep patching things up to override the authentication, things are getting much more complex. So I guess it's just not worth it. Now I need to find another way for this.

#

But one thing is sure that if in future I have enough bandwidth I will try to work on this feature like supabase has

sharp pilot
gray vortex
#

Right now not exactly, but during my exploration I found a few ways that we can use to achieve complete local instance feature

sharp pilot
#

If you find any information to make this happen, please update this topic, so we can give this information to the team, database will have more improvements/features after 1.5

eternal venture
#

@gray vortex local self-hosting does not require a cloud signup. The signup remains localised to your setup. Is that also a problem?

gray vortex
eternal venture
#

cc: @novel sparrow

novel sparrow
#

you should be able to set spin up a self hosted instance in the CI pipeline and then use curl or something similar to create a console account, org, and project. We do this for our E2E tests. i would suggest inspecting the network logs to see what API calls are needed

gray vortex
#

Thanks @eternal venture

gray vortex
#

@novel sparrow is there a better way to check if the appwrite docker container has been successfully connected to the DB and all the collections have been created? Rather than waiting for 20-40 seconds more or less depending on the host device

gray vortex
#

Is there something wrong in the scopes because after creating the API key I am not able to create the database, here is my script:

response=$(curl -b /tmp/appwrite_adapter_session.txt \
      --location "http://localhost/v1/projects/${PROJECT_ID}/keys" \
      --header 'Content-Type: application/json' \
      --data-raw '{
      "name": "some key",
      "scopes": [
          "users.read",
          "users.write",
          "teams.read",
          "teams.write",
          "databases.read",
          "databases.write",
          "collections.read",
          "collections.write",
          "attributes.read",
          "attributes.write",
          "indexes.read",
          "indexes.write",
          "documents.read",
          "documents.write",
          "files.read",
          "files.write",
          "buckets.read",
          "buckets.write",
          "functions.read",
          "functions.write",
          "execution.read",
          "execution.write",
          "locale.read",
          "avatars.read",
          "health.read",
          "migrations.read",
          "migrations.write"
      ]
  }')
  secret=$(echo "$response" | jq -r '.secret')
  echo "API Key Secret created successfully, writing .env file"
  if [ -f .env ]; then
      sed -i "s/API_KEY_SECRET=.*/API_KEY_SECRET='$secret'/" .env || echo "API_KEY_SECRET='$secret'" >>.env
      exit 0
  fi

And here is the error:

Serialized Error: { code: 401, response: { message: 'User (role: guests) missing scope (databases.write)', code: 401, type: 'general_unauthorized_scope', version: '1.4.13' } }
novel sparrow
#

If I recall correctly, 1 flag is for requests and another is for response

gray vortex
#

Actually I am getting this error when I use node sdk. When I use this generated API key to create a client instance and use that to create the database at that moment I am getting this error

#

Also I checked the console to make sure if all the 27 scopes were selected for this API key, it was all there

gray vortex
#

This is the code:

import { AppwriteAdapter, AppwriteAdapterOptions, formatter } from "../src";
import { Client, Databases, ID, Query } from "node-appwrite";

const config: AppwriteAdapterOptions = {
    endpoint: process.env.ENDPOINT as string,
    project_id: process.env.PROJECT_ID as string,
    api_key_secret: process.env.API_KEY_SECRET as string,
    database_id: "",
    user_collection_id: "",
    session_collection_id: "",
    account_collection_id: "",
    verification_token_collection_id: "",
}

const client = new Client();

client
    .setEndpoint(config.endpoint)
    .setProject(config.project_id)
    .setKey(config.api_key_secret);


const database = new Databases(client);
const database_id = ID.unique();

await database.create(database_id, "Auth TEST Database")

Error:

Serialized Error: { code: 401, response: { message: 'User (role: guests) missing scope (databases.write)', code: 401, type: 'general_unauthorized_scope', version: '1.4.13' } }
novel sparrow
gray vortex
#

Yes did that and all the variables have the value

novel sparrow
gray vortex
#

No extra spaces and also used the trim function and still getting the same error

novel sparrow
gray vortex
#

No generating them by calling the API in the shell script

novel sparrow
gray vortex
gray vortex
novel sparrow
novel sparrow
gray vortex
novel sparrow
gray vortex
novel sparrow
#

i used the API key and manually made a curl call to list databases and that worked

gray vortex
#

Yes I tested that in curl it works but why not with the SDK?

novel sparrow
gray vortex
#

For curl I don't see any error

novel sparrow
gray vortex
#

Ok I found the issue, the curl request fails because of the missing header x-appwrite-mode=admin and so this error is thrown

novel sparrow
novel sparrow
gray vortex
#

Finally solved all the problems thanks a lot @novel sparrow if not for you I would have suffered long

gray vortex
#

After generating the Api key I was not sourcing the .env file and because of which the SDK was using the old Api key.