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
- No server error on selfhosted appwrite
Please help me, my clients is ask what happen on their data? How can i make it up again?
- Upgrading selfhost version?
It is okay to upgrade version to higher one, of my current version is 1.7.4 to 1.8.1. Is that safe to do cause my clients already have data on that? Also is a...
- Local Serverless Function Testing: Are D...
I have followed the instructions to get the CLI working, and have been able to log-in, initialize my project, and created a simple Python function, which calls ...