vendredi 27 janvier 2017

laravel middleware almost always redirects

can you guys please, help me. my project running on laravel 5.3 locally everything works fine,I uploaded my project on server, it also works as it should.then I uploaded my project on another server - and it almost always redirects to login page. here's logic - if user is not authenticated, it redirects to login page.if user is authenticated, user will get access to content. on my 'another' server - when i go to main page it gives

page isn’t working
redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS

so it's not laravel's error. when i go to login page, it loads, but when I log in, it redirects to main page with that ERR_TOO_MANY_REDIRECTS error. and it always works in this way.

routes

Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

login controller

class LoginController extends Controller
{

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = '/';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}


/**
 * Handle a login request to the application.
 */

public function login(Request $request)
{
    $this->validateLogin($request);

    $user = $this->findOrCreateUser($request);

    if($user)
    {
        Auth::login($user);

        $user->incrementVisists();

        if($user->count_visits > 3)
        {
            return redirect('/campaigns');
        }else{
            return redirect('/');
        }

    }else{

        if ($this->hasTooManyLoginAttempts($request))
        {
            $this->fireLockoutEvent($request);
            return $this->sendLockoutResponse($request);
        }

        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);

    }

}

/**
 * Validate the user login request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 */
protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->username() => 'required',
        $this->password() => 'required'
    ]);
}

/**
 * Get the login username to be used by the controller.
 *
 * @return string
 */
public function username()
{
    return 'username';
}

/**
 * Get the login password to be used by the controller.
 *
 * @return string
 */
public function password()
{
    return 'password';
}

/**
 *  find or create user
 */
public function findOrCreateUser(Request $request)
{
    $auth = $this->getUserDataFromRemote($request->username, $request->password);

    if($auth == "OK")
    {
        if(User::where('name', $request->username)->count() < 1)
        {
            $user = User::create([
                'name' => $request->username,
                'email' => '',
                'role' => 1,
                'password' => bcrypt($request->password),
            ]);
        }else{
            $user = User::where('name', $request->username)->first();
        }

        return $user;
    }

    return false;
}

}

I found out that it happens cause RedirectIfAuthenticated middleware

class RedirectIfAuthenticated
{

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        //return redirect('/home');
        return redirect('/');
    }

    return $next($request);
}
}

but don't know how should i fix it. even if problem is in this middleware, why it works locally and om my first server?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire