Your question
I use mongodb as driver. Now I'm asking myself how I can use the existing connection to create other things on my page. Maybe a route with /posts that adds a new document in the posts collection. How can I access the db connection?
What are you trying to do
The thing is, I set up a complete new connection because I don't know how I can use the existing one. When I do this and I create such post it always creates it two times. This is all I do in my api:
import dbConnect from "../../../db";
import Snippet from "../../../models/Snippet";
export default async (req, res) => {
await dbConnect();
const snippet = {
title: "test 3",
text: "copy me",
};
const snip = await Snippet.create(snippet);
res.status(200).json(snip);
};
The other part of the code is exactly set up like in the official vercel example: https://github.com/vercel/next.js/tree/canary/examples/with-mongodb-mongoose
I would really love to hear from you guys :)
Documentation feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.
With Serverless, connections are not shared between routes, there is no way to share the connection from the NextAuth.js API route to any other route (unless deploying under monolithic app, e.g. Docker).
We don't provide a way to use the same connection logic in other routes, but we do get asked about it a lot.
Technically there are reasons why we might not want to (e.g. connection specific settings for some databases could mess up things like timezone behaviour in other queries and vice versa, if UTC is not being used everywhere).
Regarding the problem of posting more than once, it's possible that is related to development mode in NextAuth.js - you could always try doing npm run build && npm start and seeing if the issue happens in production mode.
@iaincollins thx for the update.
I understand that makes sense! Another quick question:
How to check if the user is authenticated in another api route? Right now I'm doing it like this:
const token = await jwt.getJwt({ req, secret })
if(token) //user is logged in
Is this the correct way to do this? (I enabled jwt in the session configuration).
@dhuber666
Yes that's perfect! We should really have a tutorials for this. The example project now includes examples for API routes.
Note:
getJwt is now getToken but essentially works the same.getSession() from the client in an API route (this was not well supported in v2; but mostly worked) which works for both database and JWT session tokens..Thanks a ton @iaincollins
For this project I use v2, but for future projects I will surely try out v3. Thx for this awesome repo!
@iaincollins
Hi again, I have another question. I'm now at the part where I try to make a fetch call from my component to my endpoint /api/snippets/all. I have my middleware in place that looks if it can find the token in the request.
But it can't find a token in the request when using fetch from the component / client.
When I go to the api manually (e.g. navigating in the browser to it) it works and it finds the token.
I have also tried fetch with the option credentials: "include" and "same-origin" but neither works.
Do I also have to pass in a header? If yes which one?
Thx so much!
@iaincollins
Hi again, I have another question. I'm now at the part where I try to make a fetch call from my component to my endpoint/api/snippets/all. I have my middleware in place that looks if it can find the token in the request.
But it can't find a token in the request when usingfetchfrom the component / client.
When I go to the api manually (e.g. navigating in the browser to it) it works and it finds the token.I have also tried
fetchwith the optioncredentials: "include"and "same-origin" but neither works.
Do I also have to pass in a header? If yes which one?Thx so much!
I'm having the same problem, did you find any solution?