【问题标题】:Appwrite - JWT doesn't allow to access documentsAppwrite - JWT 不允许访问文档
【发布时间】:2022-09-27 11:42:54
【问题描述】:

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

每次我尝试,即使对集合的访问权限设置为角色:全部,我从后端收到此错误:

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

我在服务器部分使用 Appwrite Node SDK,在前端使用 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 容器中运行所有内容。

    标签: javascript docker account appwrite


    【解决方案1】:

    (角色:成员)缺少范围(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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 2020-05-03
      相关资源
      最近更新 更多