Skip to content
Back

Error: User (role: guests) missing scope (account) after redirect in Nextjs

  • 0
  • Auth
  • Web
  • Cloud
Robert Ramirez
22 Apr, 2025, 17:09

If I go to a protected page as an authorized user and logout from that page and get redirected to the login page, I get the following error: Error: User (role: guests) missing scope (account). It appears that the code after the redirect is still executed. Why?

TL;DR
Developers are facing an error "User (role: guests) missing scope (account)" after redirect in Nextjs due to code execution after the redirect. To prevent this, they can use a return statement after the redirect call. Additionally, they should ensure that the `account.get()` function is being executed where intended.
Steven
22 Apr, 2025, 17:31

see where you're calling account.get()

Robert Ramirez
22 Apr, 2025, 17:39

I call account get in a server action:

Robert Ramirez
22 Apr, 2025, 17:39
TypeScript

import { createSessionClient } from "@/config/appwrite";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { Query } from "node-appwrite";

async function getMyRooms() {
  const cookieStore = await cookies();
  const sessionCookie = cookieStore.get("appwrite-session");

  if (!sessionCookie) {
    redirect("/login");
  }

  try {
    const { account, databases } = await createSessionClient(
      sessionCookie.value
    );

    // Get user id
    const user = await account.get();
    const userId = user.$id;

    // Fetch users rooms
    const { documents: rooms } = await databases.listDocuments(
      process.env.NEXT_PUBLIC_APPWRITE_DATABASE,
      process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_ROOMS,
      [Query.equal("user_id", userId)]
    );

    return rooms;
  } catch (error) {
    console.log("Failed to get users rooms", error);
    redirect("/error");
  }
}

export default getMyRooms;```
Steven
22 Apr, 2025, 17:41

FYI, it's best to wrap code in backticks to format a bit nicer. You can use 1 backtick for inline code (https://www.markdownguide.org/basic-syntax/#code) and 3 backticks for multiline code (https://www.markdownguide.org/extended-syntax/#syntax-highlighting).

Steven
22 Apr, 2025, 17:43

are you sure it's that account.get() that is getting executed? maybe add a log statement to confirm.

Ankit Maniya
22 Apr, 2025, 17:45
TypeScript
if (!sessionCookie) {
  redirect("/login"); // add return so that it will not execute further
}
Robert Ramirez
22 Apr, 2025, 17:47
TypeScript
    at async getMyRooms (app/actions/getMyRooms.js:22:17)
    at async MyRoomsPage (app/rooms/my/page.jsx:6:16)
  20 |
  21 |     // Get user id
> 22 |     const user = await account.get();
     |                 ^
  23 |     const userId = user.$id;
  24 |
  25 |     // Fetch users rooms {
  code: 401,
  type: 'general_unauthorized_scope',
  response: '{"message":"User (role: guests) missing scope (account)","code":401,"type":"general_unauthorized_scope","version":"1.6.2"}'
}```
Steven
22 Apr, 2025, 17:48

please use backticks to format things like this

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more