Sarav-S / Laravel-Multiauth

A Simple Laravel Package for handling multiple authentication
50 stars 8 forks source link

Middleware not working #15

Closed devawal closed 7 years ago

devawal commented 8 years ago

I created login related Issue about 3 days ago and got helped by author, but laravel default Authenticate Middleware not working, so I created one but it not working, it redirect me to the login page. I need full attention of author to solve my problem because without it my project not moving forword :(

I also share my code in stackoverflow, I think author know me well. http://stackoverflow.com/questions/39248250/multi-auth-not-working-in-laravel-5-1

This is my custom Middleware

<?php namespace App\Http\Middleware;

use Closure; use Illuminate\Contracts\Auth\Guard;

class ApplicantAuth { /* * The Guard implementation. * @var Guard */ protected $auth;

/**
 * Create a new middleware instance.
 *
 * @param  Guard  $auth
 * @return void
 */
public function __construct(Guard $auth)
{
    $this->auth = $auth->with('user');
}

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{
    if ($this->auth->guest('user')) {
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('login');
        }
    }

    return $next($request);
}

}

And this is my route

Route::group(['middleware' => 'guest', 'prefix' => 'login'], function () { Route::get('/', array("as" => "applicant_login", 'uses' => 'Portal\HomeController@applicantLogin')); Route::post('applicant-auth', array("as" => "applicant_auth", 'uses' => 'Auth\AuthController@userAuth')); }); Route::group(['middleware' => 'applicant', 'prefix' => 'applicant-user'], function () { Route::get('dashboard', array("as" => "applicant_dashboard", 'uses' => 'Applicant\ApplicantController@getIndex')); });

AuthController.php public function userAuth(Request $request) { $this->validate($request, ['email' => 'required','password' => 'required']);

    $email      = $request->input('email');
    $password   = $request->input('password');

    if (Auth::attempt('user', array('email' => $email, 'password' => $password), true)) {
        return redirect()->route('applicant_dashboard');
    }
    else{
        Session::flash('error', 'Oops! Something went wrong. Please try again later!!');
        return redirect()->back();
    }
}
Sarav-S commented 7 years ago

Closing this issue as it is answered on stackoverflow