Back

Is it Next.js or Appwrite?

  • 0
  • General
  • Web
manazo
29 Aug, 2023, 20:19

Sounds like it's giving isomorphism behavior to Appwrite form data when working with multipart form. But as the middleware.ts file runs server-side, shouldn't it load the index.js file instead of browser.js?

TL;DR
- The user is trying to validate a session in their app and is using Appwrite and Next.js. - They have created a cookie for the frontend, but it is not valid for the backend. - The user is relying on Appwrite validation and catches errors to delete the cookie and redirect to the login page. - They are using a middleware that checks if the `userSession` cookie exists and redirects the user based on that. - The user is asking for clarification on the code that reads the cookies and redirects the user. - They have tried different methods to load the `isomorphic-form-data` package in the middleware. - There
manazo
29 Aug, 2023, 20:19

should i dm u? or send it here?

manazo
29 Aug, 2023, 20:19

nope

Drake
29 Aug, 2023, 22:21

Responded

Binyamin
30 Aug, 2023, 00:35

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

manazo
30 Aug, 2023, 05:41

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?

manazo
30 Aug, 2023, 05:42

package file of isomorphic-form-data lib?

Binyamin
30 Aug, 2023, 13:34

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

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

Or

TypeScript
-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.

Guille
30 Aug, 2023, 14:26

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
manazo
30 Aug, 2023, 14:51

i tried both. but it's still loading the browser.js file

manazo
30 Aug, 2023, 14:51

can you please show me the lines where you read the cookies in the middleware file and redirect the user based on that?

Guille
30 Aug, 2023, 16:56

Sure!, the middleware looks like this:

TypeScript
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|.*\\..*).*)"],
};
Drake
30 Aug, 2023, 17:01

this doesn't actually validate the session though 🧐

Guille
30 Aug, 2023, 17:05

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

manazo
30 Aug, 2023, 17:45

Correct me if I'm wrong. Your code checks if there's a cookie with the name userSession exists, and redirects the user based on that. Isn't it?

Guille
30 Aug, 2023, 18:02

Yes in this case, I just read the user object stored in the cookie and the redirect where I need to, I don't make many validations here (is planed to improve this in the future though) because if the session isn't valid, appwrite throws an error, I catch the error and it makes to remove the cookie

Guille
30 Aug, 2023, 18:04

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

manazo
30 Aug, 2023, 18:07

Your scenario might be different, but as Steven said, it ain't validating the session. i also followed the same approach at the beginning, then realized it only checks if the cookie exists. This means the user can easily create the cookie from the browser console manually to enter any protected route

Guille
30 Aug, 2023, 18:13

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

Guille
30 Aug, 2023, 18:13

@manazo

Guille
30 Aug, 2023, 18:13

I keep relying on appwrite validation

Guille
30 Aug, 2023, 18:14

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

Guille
30 Aug, 2023, 18:15

The user can create a cookie, however that cookie won't be valid for the backend, just for the frontend. It won't make an appwrite call valid

nkdem
30 Aug, 2023, 23:42

How should we validate whether the session is valid? Is there a REST endpoint that makes this doable?

Drake
30 Aug, 2023, 23:58

use the session to get the current account

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more