AppwriteException: User (role: guest) missing scope (account), not able to get account after creating a session

huangapple go评论73阅读模式
英文:

AppwriteException: User (role: guest) missing scope (account), not able to get account after creating a session

问题

抛出错误在第 account.get() 行。

过去三天一直卡在这里,请有人可以帮我吗?

注册账户控制器代码:

const User = require('../model/User');
const { Client, Account } = require('appwrite');

exports.register = async (req, res) => {
    try {
        const { name, email, password } = req.body;

        // 如果数据不存在则进行验证
        if (!(name && email && password)) {
            return res.status(401).json({
                success: false,
                message: "所有字段都是必填的"
            })
        }

        // 检查用户是否已存在
        const doesExist = await User.findOne({ email })
        if (doesExist) {
            return res.status(401).json({
                success: false,
                message: "用户已存在"
            })
        }

        // 检查电子邮件格式是否正确
        if (!(validateEmail(email))) {
            return res.status(401).json({
                success: false,
                message: "无效的电子邮件地址"
            })
        }

        // 保存到数据库
        const user = await User.create({
            name,
            email,
        })

        user.password = undefined

        const client = new Client()
            .setEndpoint('http://localhost/v1')
            .setProject(`${process.env.APPWRITE_PROJECT_ID}`);
        ;

        const account = new Account(client);

        await account.create(
            user._id.toString(),
            email,
            password,
            name
        );

        await account.get()

        return res.status(200).json({
            success: true,
            user,
        })

    } catch (error) {
        console.log(`错误 :: 注册路由 :: ${error}`);
        return res.status(500).json({
            success: false,
            message: error.message
        })
    }
}

function validateEmail(email) {
    var re = /\S+@\S+\.\S+/;
    return re.test(email);
}

登录账户控制器代码:

const User = require('../model/User');
const { Client, Account } = require('appwrite');

exports.login = async (req, res) => {
    try {
        const { email, password } = req.body;

        // 验证数据
        if (!(email && password)) {
            res.status(401).send('所有字段都是必填的')
        }

        // 检查用户是否存在
        const user = await User.findOne({ email })

        // 如果用户不存在
        if (!user) {
            return res.status(401).json({
                success: false,
                message: "用户不存在"
            })
        }

        const client = new Client()
            .setEndpoint('http://localhost/v1')
            .setProject(`${process.env.APPWRITE_PROJECT_ID}`);
        ;

        const account = new Account(client);

        await account.createEmailSession(
            email,
            password,
        );

        await account.get()

        return res.status(200).json({
            success: true,
            user,
        })


    } catch (error) {
        console.log(`错误 :: 登录路由 :: ${error}`);
        return res.status(500).json({
            success: false,
            message: error.message
        })
    }
}

我尝试阅读 Appwrite 文档以更改访客角色的权限,但在 account.get() 方面没有找到相关信息。文档中提到的是关于数据库和文件的内容。我也阅读了一些与此相关的问题,建议在本地使用 Appwrite,但即使在本地安装了它,问题仍然存在。

英文:

Throwing error on line account.get()
Stuck on this from past 3 days can anyone help me please

Register account controller code :

const User = require('../model/User');
const { Client, Account } = require('appwrite');
exports.register = async (req, res) => {
try {
const { name, email, password } = req.body;
// Validate data if exist
if (!(name && email && password)) {
return res.status(401).json({
success: false,
message: "All fields are required"
})
}
// Check user already exist or not
const doesExist = await User.findOne({ email })
if (doesExist) {
return res.status(401).json({
success: false,
message: "User already exists"
})
}
//check if email is in correct format
if (!(validateEmail(email))) {
return res.status(401).json({
success: false,
message: "Invalid Email"
})
}
// Save to DB
const user = await User.create({
name,
email,
})
user.password = undefined
const client = new Client()
.setEndpoint('http://localhost/v1')
.setProject(`${process.env.APPWRITE_PROJECT_ID}`);
;
const account = new Account(client);
await account.create(
user._id.toString(),
email,
password,
name
);
await account.get()
return res.status(200).json({
success: true,
user,
})
} catch (error) {
console.log(`Error :: register route :: ${error}`);
return res.status(500).json({
success: false,
message: error.message
})
}
}
function validateEmail(email) {
var re = /\S+@\S+\.\S+/;
return re.test(email);
}

Login account controller code :

const User = require('../model/User');
const { Client, Account } = require('appwrite');
exports.login = async (req, res) => {
try {
const { email, password } = req.body;
// Validate data
if (!(email && password)) {
res.status(401).send('All fields are required')
}
// check if user exist
const user = await User.findOne({ email })
// if user does not exist
if (!user) {
return res.status(401).json({
success: false,
message: "User does not exist"
})
}
const client = new Client()
.setEndpoint('http://localhost/v1')
.setProject(`${process.env.APPWRITE_PROJECT_ID}`);
;
const account = new Account(client);
await account.createEmailSession(
email,
password,
);
await account.get()
return res.status(200).json({
success: true,
user,
})
} catch (error) {
console.log(`Error :: login route :: ${error}`);
return res.status(500).json({
success: false,
message: error.message
})
}
}

I tried reading Appwrite docs to change permission for guest role, but didn't find anything for account.get(). It was for databases and files. And read some issues regarding this that i should use appwrite locally but even after install it locally, problem stays the same.

答案1

得分: 0

你可以在客户端创建会话。如果你尝试在服务器端创建会话,将会返回一个 cookies 头,但服务器会忽略它,会话将会创建,但会话信息不会被存储在任何地方。

另外,我可以问一下,为什么你需要在客户端和后端之间加入一个服务器?

Appwrite 作为后端即服务为你提供了很多功能,因此你不需要自己实现这些功能。如果你告诉我你的用例,也许我可以帮助你消除手动实现的需要。

英文:

You can make the session on the client-side. If you attempt to create a session server-side, it will result in a cookies header being returned, which the server will ignore and the session will be created, but the session information will not be stored anywhere.

Also, if I may ask, why do you need a server in the middle of your Client and backend?

Appwrite does a lot of heavy lifting for you as a Backend as a Service and hence you don't need to implement things yourself. If you point me to your use-case, maybe I can help you eliminate manual implementations.

huangapple
  • 本文由 发表于 2023年6月26日 21:42:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定