Endpoint
Authentication
Required - User must be authenticated (protected byauth middleware).
Path parameters
The ID of the user to switch to
Request example
Response
Redirects back to the previous page with the new user’s authentication session. Status code:302 Found
Headers:
Location: [previous page URL]Set-Cookie: laravel_session=[new session]
Behavior
The endpoint performs the following actions (fromUserController.php:20-28):
- Logout current user - Terminates the current authentication session
- Regenerate CSRF token - Creates a new session token for security
- Login as target user - Authenticates using the provided user ID
- Redirect - Returns to the previous page with new authentication
Security considerations
Use cases
- Admin support - Help users debug issues by viewing their account
- Testing - Quickly switch between test user accounts during development
- Demos - Switch between different user personas when demonstrating features
Error responses
404 Not Found
The specified user ID does not exist:401 Unauthorized
No authenticated session exists:Implementation notes
- The redirect uses
redirect()->back(), which returns the user to their previous page - Session tokens are regenerated to prevent session fixation attacks
- The user ID is used directly with
Auth::loginUsingId(), bypassing password verification - This endpoint does not verify permissions - implement authorization middleware in production