mardi 1 décembre 2015

kwebs/multiauth middleware authentication issues

I am experiencing this is logging in users. I am using the kwebs/multiauth package to handle multiple authentication. Everything works fine except when i do this and add the middleware to authenticate users like below

Route::get('ac/dashboard', ['middleware'=>'auth', 'uses' => 'Auth\AuthController@dashboard']);

It redirects user back to the login page (User is only redirected when valid credentials are provided), all users on any level is redirected to the page specified in the Authenticate file. Every login page is affected when i apply the middleware authentication above to, even when applied to just a route.

config/auth.php

<?php

return [

/*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This driver manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "database", "eloquent"
|
*/

'multi-auth' => [
    'accountmanager' => [
        'driver' => 'database',
        // 'model'  => App\AccountManager::class
        'table' => 'accountmanager'
    ],
    'user' => [
        'driver' => 'database',
        // 'model'  => App\User::class
        'table' => 'users'
    ],
    'account' => [
        'driver' => 'database',
        // 'model' => App\User::class
        'table' => 'accounts'
    ],
],

'password' => [
    'email' => 'emails.password',
    'table' => 'password_resets',
    'expire' => 60,
],

];

Auth/AuthController.php

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Requests\CustomLoginRequest;
use App\Http\Requests\AcLoginRequest;
use App\Http\Requests\UserAuthRequest;
use App\Http\Controllers\Controller;
// use App\Http\Controllers\Auth\Auth;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/

use AuthenticatesAndRegistersUsers, ThrottlesLogins;

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

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'active' => 1,
    ]);
}

/* Custom login */
public function getAdminLogin(){
    return view('auth.login');
}

public function postAdminLogin(CustomLoginRequest $request){

    if(\Auth::user()->attempt(['email' => $request['email'], 'password' =>    $request['password'], 'active' => 1, 'approved' => 1])){
        return \Redirect::intended('cp/dashboard');
    }else{
        \Session::flash('error', 'Invalid username or password provided.');
        return \Redirect::to('auth/login');
    }
}

public function getLogout(){
    \Auth::logout();
    \Session::flash('success_message', 'You have been logged out.');
    return \Redirect::to('auth/login');
}

/* Account Manager Login */
public function getAcLogin(){
    return view('auth.account_manager_login');
}

public function postAcLogin(AcLoginRequest $request){
    if(\Auth::accountmanager()->attempt(['username' => $request->username, 'password' => $request->password, 'active' => 1, 'user_level' => 2])){
        \Session::flash('success_message', 'You have been logged in');
        return \Redirect::intended('ac/dashboard');
    } else {
        \Session::flash('error', 'Invalid username or password provided');
        return \Redirect::to('ac/login');
    }
}

public function getAcLogout(){
    // return 'loogut';
    \Auth::logout();
    \Session::flash('success_message', 'You have been logged out.');
    return \Redirect::to('ac/login');
}


}

Middleware/Authenticate.php

<?php

namespace App\Http\Middleware;

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

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

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

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

    return $next($request);
}

public function logout(){
    Auth::logout();
    \Session::flash('error_message', 'You have been logged out');
    return \Redirect::to('auth/logout');
}
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire