How to use NextJs's SSR with Moralis Auth

I’m trying to use SSR in my NextJs app. The goal is to use SSR to fetch data on a table. In this table, some items have public ACL while others do not.

I’m looking into passing the auth info to the server, specifically through the getServerSideProps function. The goal is for users who have ACL permissions to access the private items while limiting regular users to the public ones.

I understand the user session info is saved in window.LocalStorage and that it isn’t available on the server, is there a way to grab that user session info and send it as a cookie to the backend?

thanks

This seems to be a good tutorial for one way to implement it: Client and Server Side Cookies in Next.js - YouTube

So you can send the particular info you want (e.g. sessionToken) from localStorage or the Moralis.User.current() object.

1 Like

thank you, very nice tutorial, does the job for me

1 Like

Great, could you please post an example if you have time? For other users wanting to do the same thing.

1 Like

So, I’ve taken a similar approach using Edge API. I can successfully pass the relevant user data (I’ll consider security later), however, I receive the following error when attempting to instantiate and await Moralis.User.become(user.sessionToken):

ERROR: It is not memory-safe to become a user in a server environment

Fully realizing this is a “NextJS problem”, it would still be nice to find a quality solution. I could simply pass the userId and compare that against ACL, but not certain how great of a solution that is.

Basically, it would be nice to fully rely on the Moralis object/infrastructure across the pipeline. :thinking:

Where are you running Moralis.User.become? It should be used on client side in Next.js. More info here.

Yeah, I was trying to run it server-side. I want to give caching, etc. a try via the Edge API. I can initialize an “anonymous” Moralis instance and query from the API side of things, but I need to scope it to the current “front end” user. Are there any good ways to accomplish this?