Laravel 5.4切换用户后重定向异常:成功登录却跳转至登录页
Hey there, let's figure out why your user switch feature is sending you back to the login page instead of the dashboard after successful authentication. Let's break down the possible issues and fixes step by step:
1. The most obvious culprit: Redirect::back()
Looking at your code, you're using return Redirect::back(); right after logging in the new user. This just sends the user back to the page they came from (which is probably the login/user-switch form page), not the dashboard you want them to land on.
Fix: Replace that line with a direct redirect to your dashboard route. For example:
// If you have a named route for your dashboard return redirect()->route('dashboard'); // Or use a direct URL if you don't have a named route return redirect('/dashboard');
2. Verify if Auth::login($newuser) is actually working
Sometimes even if you call Auth::login(), it might not properly authenticate the user. Let's add a quick debug check to confirm:
Auth::login($newuser); // Add this line to test if authentication succeeded dd(Auth::check()); // Should return true if the user is logged in
If this returns false, double-check that your Student model implements the Illuminate\Contracts\Auth\Authenticatable interface (or extends Laravel's default User model which already includes this). Also, make sure the Student record you're fetching is valid—like not soft-deleted or marked as inactive if you have such status checks in place.
3. Check your dashboard route's middleware
If your dashboard route uses the auth middleware, make sure the authenticated Student user passes any additional checks the middleware enforces. For example, if your middleware checks for a specific role or active account status, ensure the switched user meets those criteria.
4. Clean up redundant session handling
You're manually setting session values like Session::put('email', $input['email']) and session(['orig_user' => $data->id]). Storing the original user ID is necessary for switching back later, but the email session variable is redundant—Laravel's auth system already stores the authenticated user's details. Extra session data could potentially cause conflicts, so consider removing Session::put('email', $input['email']) unless you specifically need it elsewhere.
Updated Code Example
Here's a refined version of your code with these fixes and added input validation (to avoid missing parameters):
use Illuminate\Support\Facades\Validator; public function user_reauthenticate(Request $request) { // Validate incoming request data $validator = Validator::make($request->all(), [ 'email' => 'required|email', 'password' => 'required', 'new_user' => 'required|integer' ]); if ($validator->fails()) { return back()->withErrors($validator)->withInput(); } $input = $request->all(); $originalUser = User::where('email', $input['email'])->first(); if (!$originalUser) { return back()->with('error', 'User does not exist'); } if (!Hash::check($input['password'], $originalUser->password)) { return back()->with('error', 'Wrong Credentials'); } $newUser = Student::find($input['new_user']); if (!$newUser) { return back()->with('error', 'Target student not found'); } // Store original user ID for switchback functionality session(['orig_user' => $originalUser->id]); // Log in the new student user Auth::login($newUser); // Redirect directly to the dashboard return redirect()->route('dashboard'); }
Final Quick Checks
- Make sure your
dashboardroute is correctly defined inroutes/web.phpwith the right name if you're usingroute('dashboard'). - Clear your application cache and session storage (run
php artisan cache:clearandphp artisan session:clear) to rule out any cached issues.
内容的提问来源于stack exchange,提问作者Mohsin Abbas

