Help with Mongo Realm and NextJS with User Authentication?

Hello. I’m new to Mongo, and Next JS.
I read the article on How to Integrate MongoDB Into Your Next.js App today and now I’m looking for more!

I’d like to build a NextJS app that uses Realm for user authentication with protected API routes for user data. Does anyone have a link to an tutorial article, or Github Repo or any other resources to do this?

Looking forward to your responses.

2 Likes

Hey Brian - While I don’t think we have one, it seems like Vercel has an example of using Realm with Next.JS. Would this help? - Example with Monogdb ReamlWeb (#14555) · vercel/next.js@1b5ca78 · GitHub

I will take a look when I can.

Hey Brian,

Depending on your approach to authentication, you’d either use the Realm Web SDK or the Node.js SDK. But when it comes to Auth, are you trying to use Realm solely for auth or do you need other realm features in your app. The only reason I ask is that if you just need user authentication, a specific library like Next-Auth could be a better choice.

1 Like

I’m planning to build a Admin/Dashboard app for authenticated users only. Non-authenticated users will only see a logon screen.

I would love to use NextAuth if it supports Realm features. Other developers in my company are working with Realm for other, non-Next JS apps, so I think I need to find a way to tie into Realm for authentication.

Does NextAuth work with Realm?

Hey Brian - not sure what your implementation details are for auth in the other apps, but it seems like NextAuth would work with Realm if you use JWT Authentication. After using NextAuth to sign in, you can pass in the JWT that you get from the callback to Realm’s JWT Auth provider

2 Likes

How can assure I can call app.currentUser
in the getServerSideProps ? It seems undefined if I call It on getServerSideProps

3 Likes

Is there a concrete example for this? In the docs https://www.mongodb.com/docs/realm/web/nextjs/#log-the-user-in it says

In a real application, you would want to have a more complex authentication flow. For more information, refer to the Next.js authentication documentation.

Could anyone share of an example using Next Auth and Realm Web?

1 Like

I’d also like to see a concrete example of a production-ready authentication flow.

@Alexandar_Dimcevski did you manage to find a good example of figure out a good approach?

Unfortunately not - Let me know if you do!