Getting so many errors related to mongo db

I'm trying to use better auth in nextjs I'm currently learning so most probably my mistake but what can someone please explain I'm using mongo db adapter and email password It is a test project to learn better auth before implementing on main project For database mongo db atlas I'm using google idx as ide right now I only have android devices Tech stack,- next js tailwind react vanilla javascript
No description
No description
No description
No description
No description
No description
19 Replies
lonelyplanet
lonelyplanet3mo ago
Why are you fetching the api with axios
lonelyplanet
lonelyplanet3mo ago
This is the correct way to call an endpoint server side The console errors is you are trying to call the mongodb in a client component aka in the browser environment
NekoDev
NekoDevOP3mo ago
Because I don't know and chat gpt instructed and blindly followed Thanks I would give it try let you know
lonelyplanet
lonelyplanet3mo ago
And you can't I recommend have a solid look at the docs and also chatgpt wont know what to do as it probably wont have knowledge on it yet
NekoDev
NekoDevOP3mo ago
Then I should only call mongo db in server
lonelyplanet
lonelyplanet3mo ago
What is your page for login the page.jsx
NekoDev
NekoDevOP3mo ago
Its just have form that for better auth signup with email I was testing the better auth
lonelyplanet
lonelyplanet3mo ago
yeah but your calling your having errors inside of your page file to help you it would be great to have the full context
NekoDev
NekoDevOP3mo ago
One more thing I don't know how to code yet so I would read docs Just try bunch of things and if doesn't work follow a youtube tutorial Sure wait a little
lonelyplanet
lonelyplanet3mo ago
Are you confident that you implement all of these technologies together?
NekoDev
NekoDevOP3mo ago
Well I couldn't understand fully I'm not confident But will make it work it's my determination Im making saas platform So I need to learn these technologies And my method (try and try again )may be inefficient but I like it I first took help of youtube Then llms then I'm here
NekoDev
NekoDevOP3mo ago
No description
No description
No description
NekoDev
NekoDevOP3mo ago
I had gemni generate a form I was planning to Edit it but I get so many errors I didn't know what to do
Ping
Ping3mo ago
If you're using the next middleware, it's completely fine to use axios. You can't use the better-auth instance in middleware since Next middleware doesn't support Node (yet) In saying that, it's also possible that axios doesn't work in middleware too.
Ping
Ping3mo ago
I would recommend you move your middleware verification logic to check at a per-page level, instead of in the middleware. It's still fine to do this, I just wouldn't recommend it. If you want to continue using middleware, then I recommend you look into better-fetch and utilize better-fetch to fetch your session using that same route, instead of using axios since I can't verify if that works or not.
Better Fetch
Better Fetch
Advanced fetch wrapper for typescript.
NekoDev
NekoDevOP3mo ago
I will remove the middleware completely and implement per page logic Since I don't want to add one more complex piece of puzzle to it And thank you
Ping
Ping3mo ago
Make sure to run authentication on a server component to ensure the user is allowed to view that page. If they are not allowed, use Next.js built-in functionallity to redirect users away. (eg NotFound()) You can get the users session like so:
function MyServerComponent(){
const session = await auth.api.getSession({headers: await headers()});
//...
}
function MyServerComponent(){
const session = await auth.api.getSession({headers: await headers()});
//...
}
make sure to import headers from next/headers.
NekoDev
NekoDevOP3mo ago
I will try and surely update you

Did you find this page helpful?