Replies: 4 comments 6 replies
-
As a workaround, I think you could try to use the global adapter to call
You can't access request info in import type { NextApiRequest, NextApiResponse } from "next"
import NextAuth from "next-auth"
export default async function auth(req: NextApiRequest, res: NextApiResponse) {
// Get the req info like IP
const ip = req.headers['x-forwarded-for']
return await NextAuth(req, res, {
...
callbacks: {
session({ session, token }) {
// Use the IP here
session.ip = ip
return session
}
}
})
} IMO it would be better to store request info during creating the session with nextAuthOptions = {
//... other options
isSessionValid(lastSession, nextSession) {
// decide if next session should be created
}
} |
Beta Was this translation helpful? Give feedback.
-
I've actually brought this up a while back #583, with a similar goal. This might be an interesting feature request, but we would have to work out the details. It is certainly possible in userland. As Thang suggested, you can use the Advanced initialization to get hold of the request information for now. 👍 |
Beta Was this translation helpful? Give feedback.
-
this does not work if you use the unstable get session on serversideprops. tried it and it gives me an error since the authoptions on serversideprops do not match with the authoptions of advance initialization |
Beta Was this translation helpful? Give feedback.
-
What's the best way to do this in v5? |
Beta Was this translation helpful? Give feedback.
-
Question 💬
I'd like to capture browser, last accessed and IP info when a database session is created or accessed. (Think gmail's "Last account activity" section)
In the future it would be nice to be able to invalidate sessions when I detect things change (say maybe the country changes?) or stop logins if you login from two countries at the same time. But what I need help with is;
createSession()
in my adapter orsession()
in the nextauth callback. So I can't grab ip addresses orUser-Agent
headers.Are there better approaches? Am I asking for new features?
How to reproduce ☕️
Nothing to reproduce, I'm either asking for api changes or ways to work around the issue.
Contributing 🙌🏽
Yes, I am willing to help answer this question in a PR
Beta Was this translation helpful? Give feedback.
All reactions