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
- is `account.get()` safe to be used in th...
I want to user's `id` for authentication. However, a while ago I was told in this server not to use `account.get()` and instead add user preferences for that us...
- Appwrite console is too heavy
The Appwrite console is too heavy And all of my services broken Any support , please
- Usage of the new Client() and dealing wi...
Hey guys, just a quick one - we had some web traffic the other day and it ended up bombing out - To put in perspective of how the app works, we have a Nuxt Ap...