Hi all! I am really stuck checking the auth in the middleware. It always returns the following error: AppwriteException: User (role: guests) missing scope (account)
My guess is that the session cookie is not placed before the middleware runs and therefore there is no user but I can't find a way to fix it. For your info my code:
Middleware.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import appwriteService from './appwrite/config';
export async function middleware(request: NextRequest) {
const isLoggedInAndCompany = await appwriteService.isLoggedInAndCompany();
if (!isLoggedInAndCompany) {
return NextResponse.redirect(new URL('/login', request.url));
}
return NextResponse.next();
}
// Specify the paths where the middleware should run
export const config = {
matcher: ['/dashboard/:path*',
'/organization/:path*'],
};
and this in the Appwrite config (client side):
async isLoggedInAndCompany() {
try {
const user = await account.get();
const session = await account.getSession('current');
const prefs = user.prefs;
if (user && prefs.type === 'company') {
console.log('isLoggedInAndCompany function has run');
return true;
} else {
return false;
}
} catch (error:any) {
throw error;
}
}
Can someone help me please? Thanks!