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?
see where you're calling account.get()
I call account get in a server action:
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;```
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).
are you sure it's that account.get() that is getting executed? maybe add a log statement to confirm.
if (!sessionCookie) {
redirect("/login"); // add return so that it will not execute further
}
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"}'
}```
please use backticks to format things like this
Recommended threads
- Student plan issue
I am using GitHub student plan, I even got access to appwrite's mock phone number. but when I try to use it, it says the phone number limit has reached
- Request for temporary 3 to 4 hours datab...
Hi Appwrite Team, I hope you are doing well.We are an early-stage startup currently running on Appwrite Cloud. We have unfortunately exhausted our database rea...
- Storage Chunk upload bug
Hi, I'm experiencing an issue with Appwrite Storage chunked uploads. Small files upload successfully, but larger video files that require chunking fail. For ...