log in as another user programmatically #1594
-
Hi, I'm exploring building an internal tool that would allow our customer support team to log in as a user to help debug issues/bugs they're running into. Has anyone built anything like this? Would appreciate pointers or examples! I ran across #45 which definitely seems relevant, although it's not clear to me yet how to adapt this example for users who don't have a password (magic link login). Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
In case anyone else is running into this, here's how I did it for JWT: import jwt from "next-auth/jwt";
import { set } from "next-auth/dist/server/lib/cookie";
// ideally there would be a way to get the payload from a next-auth primitive
const jwtPayload = {
name: <user name>,
email: <user email>,
picture: <user picture>,
sub: <user id>,
... // any additional fields you include in your jwt callback
};
const newEncodedJwt = await jwt.encode({
secret: <your secret>,
token: jwtPayload,
});
const cookieExpires = new Date();
const maxAge = 30 * 24 * 60 * 60; // should be the same value you use in your NextApiHandler
cookieExpires.setTime(cookieExpires.getTime() + maxAge);
set(res, "next-auth.session-token", newEncodedJwt, {
expires: cookieExpires.toISOString(),
}); Not ideal but it works. This is modeled off the flow that happens here: next-auth/src/server/routes/callback.js Lines 181 to 197 in aacc34b |
Beta Was this translation helpful? Give feedback.
In case anyone else is running into this, here's how I did it for JWT: