首頁  >  文章  >  web前端  >  [已解決] Appwrite 使用者角色缺失或缺失範圍錯誤

[已解決] Appwrite 使用者角色缺失或缺失範圍錯誤

Patricia Arquette
Patricia Arquette原創
2024-10-09 10:53:02783瀏覽

[Solved] Appwrite user role missing or missing scope errors

如果您想快速建立應用程序,Appwrite 是一個非常棒的工具,但有時您可能會遇到令人沮喪的錯誤,對我來說,這些錯誤總是與「使用者角色缺失」或「使用者無權執行此操作」等。即使我可以完全存取我的應用程式的任何實例,執行任何操作。

但最終我找到了解決所有問題的方法(也許不是全部,但我想這樣認為)。

所以這篇不和諧的帖子實際上以一種非常微妙的方式解釋了它。

問題是要確保使用這些方法中的任何一種都存在會話,我的意思是無論您在專案中使用哪種方法。

  • 建立匿名會話
  • 建立電子郵件密碼會話
  • createOAuth2Session
  • 建立會話

讓我舉一個我遇到這個錯誤的例子,這樣可能會更清楚。

我有一個註冊頁面,我想要做的是,一旦用戶點擊建立帳戶或註冊,它應該觸發驗證電子郵件,但我收到用戶未授權的錯誤。解決方案是在觸發電子郵件之前建立一個會話,因此請參閱以下程式碼我如何在觸發電子郵件之前建立會話:

"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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn