Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication redirects fail

when I login with wrong credentials I got the right response. when I login with the right credentials the login page reload with 302 request but it never redirect to statistics page. when I debug it I found that the code goes to this authinticate.php in the middleware folder, it redirect to the guest login state

if (Auth::guard($guard)->guest()) {
            if ($request->ajax() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('login');
            }
        }

see the code:-

Route.php

Route::get('login', 'LoginController@index');
Route::post('signin', 'LoginController@signin');
Route::get('signout', 'LoginController@signout');


Route::group(['prefix' => 'api'], function() {
    Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
    Route::post('authenticate', 'AuthenticateController@authenticate');
});

Route::group(['middleware' => ['web']], function () {
    Route::auth();
    Route::get('/', 'StatisticsController@index');
    Route::get('/statistics', 'StatisticsController@statistics');

});

Login Controller

    public function index() {

        return view('login');
    }

    public function signin(Request $request) {

        $errors = [];
        $email=$request['email'];
        $password= $request['password'];
        $credentials = array('email' => $email, 'password' => $password);

        if(Auth::attempt($credentials))
        {
          return redirect('/statistics');


        }
        return "bad request";

    }
     public function signout()
    {
        Auth::logout();
        return redirect('/login');    }

}

Statistics Controller

class StatisticsController extends Controller {
    public function __construct()
    {
                    $this->middleware('auth');

    }
    public function index() {

    return view('statistics')->with($data);
  }

public function statistics() {
        return view('statistics');

  }



}

Kernal.php note that there is JWT auth library I use it for restful authentication with the mobile app only.

protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    ];

    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];

    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'jwt.auth' => 'Tymon\JWTAuth\Middleware\GetUserFromToken',
        'jwt.refresh' => 'Tymon\JWTAuth\Middleware\RefreshToken'
    ];

middleware/authenticate.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class Authenticate
{

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->guest()) {
            if ($request->ajax() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('login');
            }
        }

        return $next($request);
    }
}
like image 693
Hesham Watany Avatar asked Mar 13 '16 01:03

Hesham Watany


People also ask

What is redirect URL in authentication?

A redirect URI, or reply URL, is the location where the authorization server sends the user once the app has been successfully authorized and granted an authorization code or access token.

Why are my redirects not working?

First try removing and then re-adding the redirects. Make sure to clear your browser cache when you go back to test. If the problem recurs, then check your . htaccess file to see if something is there that may be interfering with your current redirects.


1 Answers

Check your cache.I had a similar problem, where I lost a couple of hours, so these where some of the steps I've made:

  • php artisan route:clear
  • clear browser cache
  • run composer update
  • Download a fresh copy of laravel(new project), and then slowly merge chunks of your code into the new project
like image 167
Maky Avatar answered Dec 01 '22 14:12

Maky