mardi 2 avril 2019

Why does Laravel 5 keep redirecting to /login after authenticating user (post upgrade)

I used LaraShift to upgrade my Laravel 4.2 app to 5.0. LaraShift got me about halfway there, and I've spent 10 hours getting 90+% of the remaining 50%. Where I'm completely stuck is on the login. Laravel is authenticating the user, but then immediately redirecting back to the login page. This is the top of my sessionsController:

<?php

namespace App\Http\Controllers;

use App\Services\Custom\proxyPermissions;
use App\Http\Requests\Request;
use App\Services\Custom\sendEmail;
use App\Site;
use App\User;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;

class SessionsController extends Controller
{

    public function index()
    {
        if (Auth::check()) {
            return redirect('/');
        }
        return view('sessions.login');
    }

    public function loginPost()
    {
        if ($t = Auth::attempt(Input::only('email', 'password'), Input::only('remember'))) {
            $u = Auth::user();

            if (Auth::user()->role == 'admin') {
                $user = User::whereId(Auth::user()->id)->with('sites')->first();
                if (isset($user->sites[0])) {
                    Site::store2Session($user->sites[0]->id);
                } else {
                    Site::store2Session(1);
                }
            }

            if (Auth::user()->role == 'admin') {
                return Redirect::home();
            } else {
                // User is not an admin
                /*  After user logs in, control comes here, but user is redirected back to the login page
                    instead of to the intended URL or / 
                */
                return Redirect::intended('/');
                //return Redirect::intended();
                //return Redirect::intended('/')->with('flash_notice','Welcome back!');
            }
        }

        return Redirect::back()->with('flash_error', 'Login Failed')->withInput();
    }

With the exception of the namespace and use statements at the top, this is the same code I had in 4.2. The app is getting to this line:

return Redirect::intended('/');

at the bottom and then redirecting to /login

The password reset function works correctly. The user is authenticating when entering the correct username & password. I have a standard user model with id, email, password.

Any ideas?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2CRqX3g
via IFTTT

Aucun commentaire:

Enregistrer un commentaire