Hello, I am trying to implement a middleware to protect the routes in my website. However it is not working as it is throwing a AppwriteException: User (role: guests) missing scope (account) at Generator.next (<anonymous>).
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { Client, Account } from "appwrite";
import { appwriteConfig } from "@/lib/appwrite/config";
export async function middleware(request: NextRequest) {
const client = new Client()
.setProject(appwriteConfig.projectId);
const account = new Account(client);
let isAuthenticated;
isAuthenticated = await account.get();
if (!isAuthenticated && request.nextUrl.pathname === "/home" ) {
console.log(isAuthenticated);
const signInUrl = new URL('/sign-in', request.url);
return NextResponse.redirect(signInUrl);
}
return NextResponse.next();
}
For now I have the code so that the user can't acess the home page if they have not logged in
Recommended threads
- general_route_not_found - Auth Guide
If you’ve just added a subdomain to your project, verified your DNS records, and confirmed your SSL certificate is working, but you're still hitting a `general_...
- Impossible to get USER after createEmail...
Am using provider to deal with functions linked to appwrite. Here is my login. Future<String?> login(String email, String password) async { try { aw...
- User ID case sensitivity
I see that through REST (and SDK as well), getting a user is not case sensitive. And even though documentation does not clearly state that it is, the wording "V...