:question: How to Log In Programmatically as Any User (Super Admin Impersonation Use Case)
Hi Better Auth team
I'm trying to build a super admin panel where I can manage multiple organizations and their users. I already fetch the list of users and want to support a "Login As" feature โ where I click a button and get logged in as that user (without needing their password).
What I'm Doing:
* I can fetch the user's info from the org * I generate a session manually in the DB (using the same structure as Better Authโs session table) * I open a link like
/impersonate?token=...
/impersonate?token=...
in the CMS app * On the server, I check the token and insert a session into the session table * I set the cookie
better-auth.session_token
better-auth.session_token
manually
The Problem:
Even after inserting the session and setting the cookie, Better Auth doesnโt recognize the sessionโ it returns
null
null
in the auth middleware.
Question:
Whatโs the correct or recommended way to programmatically log in as a user (impersonation use case)? How can I create a valid session Better Auth will accept?