![[已解决] appwrite 用户角色缺失或缺失范围错误](https://img.php.cn/upload/article/001/246/273/172848162052369.jpg)
如果您想快速构建应用程序,appwrite 是一个很棒的工具,但有时您可能会遇到令人沮丧的错误,对我来说,这些错误总是关于“用户角色缺失”或“用户无权执行此操作”等即使我可以完全访问我的应用程序的任何实例,执行任何操作。
但最终我找到了解决所有问题的方法(也许不是全部,但我想这样认为)。
所以这篇不和谐的帖子实际上以一种非常微妙的方式解释了它。
问题是要确保使用这些方法中的任何一种都存在会话,我的意思是无论您在项目中使用哪种方法。
让我举一个我遇到此错误的例子,这样可能会更清楚。
我有一个注册页面,我想要做的是,一旦用户单击创建帐户或注册,它应该触发验证电子邮件,但我收到用户未授权的错误。解决方案是在触发电子邮件之前创建一个会话,因此请参阅以下代码我如何在触发电子邮件之前创建会话:
"use client";
import Link from "next/link";
import { FormEvent } from "react";
import { Button } from "@/components/ui/button";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { createAuthAccount } from "@/app/appwrite/createAuthAccount";
import { createLoginSession } from "@/app/appwrite/createLoginSession";
import { useRouter } from "next/navigation";
import { sendVerificationEmail } from "@/app/appwrite/sendVerificationEmail";
export const description =
"A sign up form with first name, last name, email and password inside a card. There's an option to sign up with GitHub and a link to login if you already have an account";
export default function LoginForm() {
const router = useRouter();
const signUpFormHandler = async (event: FormEvent) => {
event.preventDefault();
const formData = new FormData(event.target as HTMLFormElement);
const data = Object.fromEntries(formData.entries());
const createdAccount = await createAuthAccount({
email: data?.email.toString(),
password: data?.password.toString(),
name: data?.["full-name"].toString(),
});
if (createdAccount?.$id) {
await createLoginSession({
email: data?.email.toString(),
password: data?.password.toString(),
});
await sendVerificationEmail();
}
};
return (
<Card className="mx-auto max-w-sm">
<CardHeader>
<CardTitle className="text-xl">Sign Up</CardTitle>
<CardDescription>
Enter your information to create an account
</CardDescription>
</CardHeader>
<CardContent>
<form onSubmit={signUpFormHandler} className="grid gap-4">
<div className="grid gap-2">
<Label htmlFor="full-name">Full name</Label>
<Input name="full-name" id="full-name" placeholder="Max" required />
</div>
<div className="grid gap-2">
<Label htmlFor="email">Email</Label>
<Input
id="email"
type="email"
placeholder="m@example.com"
required
name="email"
/>
</div>
<div className="grid gap-2">
<Label htmlFor="password">Password</Label>
<Input name="password" id="password" type="password" />
</div>
<Button type="submit" className="w-full">
Create an account
</Button>
</form>
<div className="mt-4 text-center text-sm">
Already have an account?{" "}
<Link href="#" className="underline">
Sign in
</Link>
</div>
</CardContent>
</Card>
);
}
这只是一个示例,描述了预期的行为、正在发生的事情以及应该做什么。
只是想分享一下,以防像我这样的 appwrite 新手遇到这个错误。总而言之,我发现几乎在所有情况下,当我遇到任何范围错误或用户未经授权的错误时,都会创建一个会话,或者至少在调用该方法修复这些问题之前确保会话存在。所以请尝试一下并让我知道会发生什么
以上就是[已解决] Appwrite 用户角色缺失或缺失范围错误的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号