
I want to check if the user has a valid session before fetching data
import { Client, Users } from 'node-appwrite';
export async function GET() {
const client = new Client()
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT as string)
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID as string)
.setKey(process.env.APPWRITE_API_KEY as string); // Securely use API key
const usersAPI = new Users(client);
try {
// Fetch the user list from Appwrite
const usersList = await usersAPI.list();
// Log the labels for each user
usersList.users.forEach((user) => {
console.log(`User: ${user.name}, Labels: `, user.labels);
});
const users = usersList.users.map((user) => ({
name: user.name || 'N/A',
email: user.email,
phone: user.phone || 'N/A',
emailVerification: user.emailVerification,
phoneVerification: user.phoneVerification,
isAdmin: user.labels.includes('admin') // Check if 'admin' label exists in labels array
}));
return NextResponse.json({ totalUsers: usersList.total, users });
} catch (error: any) {
console.error('Error fetching users:', error);
return NextResponse.json({ totalUsers: 0, users: [] }, { status: 500 });
}
}
I am trying to make some middleware that validates a users session and checkf for some custom permissions or the admin label before allowing them to proceed. Using app router in NextJs.

If you're using API key, then you have full access to everything


I think you should use .setSession in your case in order to work as the user instead of full admin server side
Recommended threads
- Sharing cookies
Hi, I’m using Appwrite Cloud, and I have a setup where my Appwrite backend is hosted on a subdomain (e.g., api.example.com), while my frontend (Next.js app) and...
- Organization not exists anymore
Hello! We have a problem with a cloud database. We are on the Free plan, but after a refresh the site wants me to create a new organisation, and I not see the c...
- JSON and Object Support in Collection do...
I am working with Next.Js and Appwrite Cloud, I am relatively New to Appwrite but i have noticed there is no direct support of JSON and Object support in attrib...
