AppWrite -JWT不允许访问文档

发布于 2025-02-07 13:26:02 字数 1051 浏览 0 评论 0原文

当用户在前端登录时,我无法使用创建的JWT向后端提出请求。

每次尝试尝试,即使对集合的访问设置为角色:所有,我都会从后端获得此错误:

{
    "statusCode": 500,
    "code": "401",
    "error": "Internal Server Error",
    "message": "<User> (role: member) missing scope (collections.read)"
}

我正在使用appwrite node sdk用于服务器part和appwrite web sdk在前端。

这是我生成JWT令牌的登录脚本:

import { Appwrite } from "appwrite";

export const login = async (email, password) => {
    const api = new Appwrite();
    api.setEndpoint(import.meta.env.VITE_APPWRITE_URL);
    api.setProject(import.meta.env.VITE_APPWRITE_PROJECT);
    await api.account.createSession(email, password);
    const user = await api.account.get();
    const jwt = await api.account.createJWT();
    return {
        jwt: jwt.jwt,
        user: {
            id: user.$id,
            email: user.email,
            name: user.name
        },
    }
}

我缺少什么?

注意:我正在运行Docker容器中的所有内容。

I'm unable to make a request to my backend using a JWT created when the user logs in in the frontend.

Everytime I try, even if the access to the collection is set to role:all, I get this error from the backend:

{
    "statusCode": 500,
    "code": "401",
    "error": "Internal Server Error",
    "message": "<User> (role: member) missing scope (collections.read)"
}

I'm using Appwrite Node SDK for server part and Appwrite web sdk in the frontend.

This is my login script where I generate the JWT token:

import { Appwrite } from "appwrite";

export const login = async (email, password) => {
    const api = new Appwrite();
    api.setEndpoint(import.meta.env.VITE_APPWRITE_URL);
    api.setProject(import.meta.env.VITE_APPWRITE_PROJECT);
    await api.account.createSession(email, password);
    const user = await api.account.get();
    const jwt = await api.account.createJWT();
    return {
        jwt: jwt.jwt,
        user: {
            id: user.$id,
            email: user.email,
            name: user.name
        },
    }
}

What am I missing?

Note: I am running everything inside Docker containers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2025-02-14 13:26:02

(角色:成员)缺少范围(collections.read)

此错误表明您正在尝试获取收集,而不是收集中的文档。确保您正在使用databases.listDocuments()

const sdk = require('node-appwrite');

// Init SDK
const client = new sdk.Client();

const databases = new sdk.Databases(client, '[DATABASE_ID]');

client
    .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
    .setJWT('919c2d18fb5d4...a2ae413da83346ad2') // Your JWT token
;

const promise = databases.listDocuments('[COLLECTION_ID]');

promise.then(function (response) {
    console.log(response);
}, function (error) {
    console.log(error);
});

注意:此示例代码使用Node-AppWrite版本7.0.2,用于AppWrite版本0.15.x。

(role: member) missing scope (collections.read)

This error indicates you're trying to fetch a collection rather than documents in your collection. Make sure you're using databases.listDocuments().

const sdk = require('node-appwrite');

// Init SDK
const client = new sdk.Client();

const databases = new sdk.Databases(client, '[DATABASE_ID]');

client
    .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
    .setJWT('919c2d18fb5d4...a2ae413da83346ad2') // Your JWT token
;

const promise = databases.listDocuments('[COLLECTION_ID]');

promise.then(function (response) {
    console.log(response);
}, function (error) {
    console.log(error);
});

Note: this sample code uses node-appwrite version 7.0.2 and is meant for Appwrite version 0.15.X.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文