Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Bug Report: I cannot access my collections with an api key from node.js server #6975

Closed
2 tasks done
Rosalza opened this issue Oct 21, 2023 · 2 comments
Closed
2 tasks done
Assignees
Labels
bug Something isn't working product / databases Fixes and upgrades for the Appwrite Database.

Comments

@Rosalza
Copy link

Rosalza commented Oct 21, 2023

馃憻 Reproduction steps

Hello,
i am trying to access my collections from my node.js backend server via that node-appwrite server sdk but I get the following error:

/Users/agency/Documents/website/testing/node_modules/node-appwrite/lib/client.js:174
                        throw new AppwriteException(error.response.data.message, error.response.status, error.response.data.type, error.response.data);
                              ^

AppwriteException [Error]: The current user is not authorized to perform the requested action.
    at Client.call (/Users/agency/Documents/website/testing/node_modules/node-appwrite/lib/client.js:174:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Databases.createDocument (/Users/agency/Documents/website/testing/node_modules/node-appwrite/lib/services/databases.js:1691:16)
    at async createOtpToken (file:https:///Users/agency/Documents/website/testing/app.js:19:27)
    at async file:https:///Users/agency/Documents/website/testing/app.js:77:13 {
  code: 401,
  type: 'user_unauthorized',
  response: {
    message: 'The current user is not authorized to perform the requested action.',
    code: 401,
    type: 'user_unauthorized',
    version: '0.11.13'
  }
}

Node.js v20.7.0

This is my code (example code to test):

//Imports
import * as sdk from 'node-appwrite'
import { ID, Query } from "node-appwrite";


//AppWrite connection
export const appwriteClient = new sdk.Client()
    .setEndpoint('https://cloud.appwrite.io/v1')
    .setProject('64fa13dd0e53cf8ad7e0')
    .setKey('c28260ba0463955d924274bdcfcf4967e06ecd360b48ae8d9afc16cd7bb65eb0c02a43211900e58a7e8e50c28928e486535c5c85e0dd5212ac033d4aacf9023a7e5575a44d5f44918dde954e013ecdbd2c99821c36f2aa05123e6f160fd7cf8ce192b0d0003abdcf31364c7e79450880b6616c81c2af0c845ec6d7a03730d7f0')


//Init AppWrite databases
export const appwriteDatabase = new sdk.Databases(appwriteClient)

//Create otpToken
export const createOtpToken = async (otpToken) => {
    //Add otpToken to collection
    const createdToken = (await appwriteDatabase.createDocument(
        '64fa1afba8f10996d9a2',
        '6533a7690ba77a97e025', 
        ID.unique(), 
        otpToken
    ))

    //Return promise
    return createdToken
}

//Get otpToken by userId
export const getOtpByUserId = async (userId) => {
    //Query otp documents
    const docs = await appwriteDatabase.listDocuments(
        '64fa1afba8f10996d9a2',
        '6533a7690ba77a97e025',
        [
            Query.equal('userId', userId),
            Query.limit(1)
        ]
    )

    //Fetch otp
    const otpToken = (await appwriteDatabase.getDocument(
        '64fa1afba8f10996d9a2',
        '6533a7690ba77a97e025',
        docs.documents[0].$id
    ))

    //Check if token exists
    if (docs.documents.length === 0) {
        throw new Error('No existing tokens!')
    }

    //Error handling
    if (!otpToken) {
        throw new Error('No existing tokens!')
    }

    //Return otp
    return {
        docId: docs.documents[0].$id,
        ...otpToken
    }
}

export const getUsers = async () => {
    //Fetch users
    const { documents } = await appwriteDatabase.listDocuments('64fa1afba8f10996d9a2', '64fa1afec29d619c79b3');

    //Add typesafety
    const users = documents

    //Retun users
    return users
}

console.log(await createOtpToken({
    id: 'sdkjfhdskfj',
    token: 'sdklflkjfdk',
    activatedAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
    user: {
        name: 'Luis Rosengarte',
        email: '[email protected]',
        password: '$2a$12$yINf7C8/GikqF26DPTJCd.SRGDDudXaGfb4Mzkdw9oHTHeCHPMx8O',
        role: 'agency_ceo',
        emailVerified: false,
        $id: '64fa1c6937ad7c7f371c'
    },
    userid: '64fa1c6937ad7c7f371c'
}))

馃憤 Expected behavior

Logging the created otpToken in the console.

馃憥 Actual Behavior

Error:

/Users/agency/Documents/website/testing/node_modules/node-appwrite/lib/client.js:174
                        throw new AppwriteException(error.response.data.message, error.response.status, error.response.data.type, error.response.data);
                              ^

AppwriteException [Error]: The current user is not authorized to perform the requested action.
    at Client.call (/Users/agency/Documents/website/testing/node_modules/node-appwrite/lib/client.js:174:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Databases.createDocument (/Users/agency/Documents/website/testing/node_modules/node-appwrite/lib/services/databases.js:1691:16)
    at async createOtpToken (file:https:///Users/agency/Documents/website/testing/app.js:19:27)
    at async file:https:///Users/agency/Documents/website/testing/app.js:77:13 {
  code: 401,
  type: 'user_unauthorized',
  response: {
    message: 'The current user is not authorized to perform the requested action.',
    code: 401,
    type: 'user_unauthorized',
    version: '0.11.13'
  }
}

Node.js v20.7.0

馃幉 Appwrite version

Appwrite Cloud

馃捇 Operating system

MacOS

馃П Your Environment

Just a flat out local nodejs runtime

馃憖 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

馃彚 Have you read the Code of Conduct?

@Rosalza Rosalza added the bug Something isn't working label Oct 21, 2023
@stnguyen90 stnguyen90 added the product / databases Fixes and upgrades for the Appwrite Database. label Oct 23, 2023
@stnguyen90 stnguyen90 changed the title I cannot access my collections with an api key from node.js server馃悰 Bug Report: 馃悰 Bug Report: I cannot access my collections with an api key from node.js server Oct 23, 2023
@stnguyen90
Copy link
Contributor

@Rosalza, thanks for creating this issue! 馃檹馃徏 As mentioned on Discord, you would get this error if the project ID is incorrect or the API key doesn't have enough scopes. Please check those first.

@stnguyen90
Copy link
Contributor

@Rosalza, are you still having problems? FYI, I'll need to close this due to inactivity soon.

@stnguyen90 stnguyen90 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working product / databases Fixes and upgrades for the Appwrite Database.
Projects
None yet
Development

No branches or pull requests

2 participants