Replies: 3 comments 7 replies
-
Did you happen to ever find anything out, specifically regarding #2? I'm struggling here as well. |
Beta Was this translation helpful? Give feedback.
-
I would like to know how people deal with the second problem aswell. We've put the external api calls at client when using credentials provider but have problem with other providers like google. Is it ok to make http requests in the callbacks and it's not clear (for me) how to handle errors from there. Thanks. |
Beta Was this translation helpful? Give feedback.
-
We usually have the frontend (Next.js) seperate from the backend (PHP) because we do not want to expose anything about the backend to the client, so client-side external API calls would not work for us. Using Next.js API routes to handle authenticated API calls has worked like a charm for quite a few projects of ours. As suggested by the OP, we're injecting some user-identifying information to the header and proxying the request to the backend. In older projects, we used to extract and pass the JWT token, but we did not like that we also had to deal with JWT logic in the backend to get the user ID. In later projects, we're passing the user ID directly (the backend is not exposed to the outside world), so it's easier to manage auth logic in one place in the frontend. For this example, we used an API route located in import httpProxyMiddleware from "next-http-proxy-middleware"
import { getSession } from "next-auth/client"
export const config = {
api: {
bodyParser: false,
},
}
export default async function (req, res) {
const session = await getSession({ req })
return session
? httpProxyMiddleware(req, res, {
xfwd: true,
target: process.env.API_URL,
pathRewrite: {
"^/api/external": "/api",
},
headers: {
"X-Member-Id": session.id,
},
})
: res.status(401).send("You are not authorized to view this content.")
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Question 💬
Hi everyone!
I and my friend have recently started our new side project with Next.js on the frontend and Nest.js (Express) in the backend. I started with implementing REST API with JWT Auth, using a common approach with getting token via Bearer in the headers.
And this is where problems started to occur 🙂
I started reading about what is recommended approach for handling authentication in the Next.js app, and all of the documentation and articles told me to use either
next-auth
oriron-session
or some 3rd party like firebase and auth0. I am interested in keeping everything under our control, so at this point, I was left with 2 first approaches.As I understand having a cookie session with httpOnly is more secure than having it stored in the local storage. But apart from that, I have few questions that about things that are not clear to me.
next-auth
and attach it to the API request?next-auth
how should the work with API work in that case? Should just check if the token is still valid via the DB call on the backend and replace this with JWT strategy?If you could point to some example project/code, it would be awesome 🙂
Beta Was this translation helpful? Give feedback.
All reactions