#Is it Next.js or Appwrite?

1 messages · Page 1 of 1 (latest)

twin matrix
#

I'm not sure if this bug is caused by Appwrite or Next.js. Here I've described the full scenario in detail.
Any help will be appreciated

twin matrix
#

after following the stacktrace, I noticed that Appwrite uses the isomorphic-form-data library that's using the window object in their lib/browser.js file.

  1. Is there any way to get rid of this?
  2. Does anyone have any idea why this library is important for Appwrite to use?
tardy wing
#

What happened is that the isomorphic-form-data load the browser file and not the main one.

  1. You can customize the package but it ain't maintainable
  2. It creates the form-data for when you're uploading files
#

To solve that can you share your packages.json

#

Are you using vite?

twin matrix
twin matrix
twin matrix
tardy wing
#

If you can go with Steven's suggestions.
If not you can customize the package file itself to load the main module

twin matrix
# onyx moon Responded

You suggested not using the web sdk in the middleware. what can be the workaround then as I need to check for the user logged-in status in the middleware? Hit /v1/account manually?

twin matrix
tardy wing
#

The appwrite one, change it to load isomorphic-form-data main module, change it to:

-import 'isomorphic-form-data';
+import 'isomorphic-form-data/lib';

Or

-import 'isomorphic-form-data';
+global.FormData = module.exports = require('form-data')

Both should work, but in this case you'll need to redeploy custom-version of the appwrite package.

teal robin
#

What I have done in nextjs is:

  1. create the session in the client side (I'm using OAuth2 but should work with any auth method)
  2. In the success call I store the user info in a new cookie (this solves third party cookies problems)
  3. I read the new cookie in the middleware
twin matrix
twin matrix
teal robin
#

Sure!, the middleware looks like this:

export const middleware = async (req: NextRequest) => {
  let user: Models.User<UserPreferences> | null = null;
  const userSession = req.cookies.get("userSession")?.value;

  try {
    user = JSON.parse(userSession || "");
  } catch {}

  if (!user && !req.url.endsWith("/login") && !req.url.endsWith("/success")) {
    return NextResponse.redirect(`${process.env.NEXT_PUBLIC_FRONTEND_URL}/login`);
  }

  return NextResponse.next();
};

export const config = {
  matcher: ["/((?!failure|auth|_next/static|_next/image|favicon.ico|.*\\..*).*)"],
};

onyx moon
teal robin
#

Oh I forgot to mention, in my case the cookie have a short time, that is why I don't validate it, at the moment if the session isn't valid, appwrite just return the corresponding error

twin matrix
teal robin
#

You can improve this, I use this method to avoid calls to appwrite, and because ssr was hard to implement

twin matrix
teal robin
#

But if the appwrite cookie isn't created, the sdk will return (role: xxxx) missing scope (xxxx)
I catch error, and delete the cookie, so the middleware redirect to login again

#

@twin matrix

#

I keep relying on appwrite validation

#

ofcourse it would be better to make that validation on the server, but I couldn't make it work at that moment, and this was my workaround

teal robin
proud bison