vision vision - 4 months ago 115
PHP Question

How to create multiple login authentication in Laravel 5.2.39?

I am using laravel 5.2.39.after successful login,web page is not redirecting to member/add-ticket.Can any one tell where am doing wrong ?

Following is my code

Kernel.php

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,
'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
'member' => \App\Http\Middleware\RedirectIfNotMember::class,
];


In Middleware

RedirectIfNotMember.php

<?php

namespace App\Http\Middleware;

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

class RedirectIfNotMember
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = 'member')
{
if (!Auth::guard($guard)->check()) {
return redirect('member-login');
}

return $next($request);
}
}


In AuthController.php

<?php

namespace App\Http\Controllers\MemberAuth;

use App\Member;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Auth;
use Illuminate\Http\Request;
use Redirect;

class AuthController extends Controller
{


use AuthenticatesAndRegistersUsers, ThrottlesLogins;

protected $redirectTo = '/member/add-ticket';

protected $guard = 'member';


public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}




public function memberLogin()
{
if (Auth::guard('member')->check())
{
return redirect('member/add-ticket');
}

return view('login.member_login');
}
public function memberPostLogin(Request $request)
{

$email=$request->emailid;
$password=$request->password;
if(Auth::attempt(['emailid'=>$email,'password'=>$password]))
{
return redirect()->intended('member/add-ticket');
}

return Redirect::to('member-login');
//return abort(403);
}



public function memberRegister()
{
return view('login.member_register');
}

public function memberPostRegister(Request $request)
{
//$this->print_me($request->all());
$data=$request->all();
// $this->print_me($data);
$data['password']=bcrypt($request->password);

// $this->print_me($data);
$data=Member::create($data);

if($data)
return Redirect::to('member-login');
else
return Redirect::to('member-register');
}
public function resetPassword()
{
//return view('member.auth.passwords.email');
}

public function logout(){
Auth::guard('member')->logout();
return redirect('member-login');
}

public function print_me($data){
echo "<pre>";
echo print_r($data);
echo "</pre>";
}
}


route.php

Route::group(['middleware' => ['member'],'prefix' => 'member'], function () {
Route::get('add-ticket','TestController@addTicket');
//Login Routes...
Route::get('logout','MemberAuth\AuthController@logout');

});

Route::group(['middleware' => ['web']], function () {
//Member login routes
Route::get('member-login','MemberAuth\AuthController@memberLogin');
Route::post('member-login','MemberAuth\AuthController@memberPostLogin');

Route::get('member-register','MemberAuth\AuthController@memberRegister');
Route::post('member-register','MemberAuth\AuthController@memberPostRegister');

Route::get('member-password-reset','MemberAuth\PasswordController@resetPassword');

//Admin login routes
Route::get('admin-login','AdminAuth\AuthController@adminLogin');
Route::post('admin-login','AdminAuth\AuthController@adminPostLogin');
Route::get('admin-password-reset','AdminAuth\PasswordController@resetPassword');

});

Route::group(['middleware' => 'web'], function () {
Route::auth();

// Route::get('/home', 'HomeController@index');
});

Answer

instead writing your own function you should use laravel default function for authentication define in Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers Trait

So just update your route.php for this

   //Member login routes
    Route::get('member-login','MemberAuth\AuthController@getLogin');
    Route::post('member-login','MemberAuth\AuthController@PostLogin');

    //Admin login routes
    Route::get('admin-login','AdminAuth\AuthController@getLogin');
    Route::post('admin-login','AdminAuth\AuthController@PostLogin');

don't fogert to include Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers in your both controller AdminAuth\Authcontroller and MemberAuth\AuthController