Hi @mstnorris
I have used this technique but i am not able to get the output i need.
There are two types of user.
- Admin
- Public Users.
I have a field in users table named 'is_admin'.
I want that if the user who logged in is admin then it should be redirected to different path and if he is public user then he should be redirected to a different path.
My users model looks like this.
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
public function getUserSku(){
return $this->belongsToMany('App\sku')->withTimestamps();
}
public function isAdmin()
{
return $this->admin; // this looks for an admin column in your users table
}
}
My controller with middleware looks like this.
<?php
namespace App\Http\Controllers;
use App\subsribe;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Input;
use yajra\Datatables\Datatables;
class subscribeController extends Controller
{
/**
* Checks for authentication
*/
public function __construct(){
$this->middleware('auth' ,['except' => 'create']);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('admin.subscribe.index');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$email = Input::get('email');
$check = subsribe::where('email','=',$email)->first();
if($check === null){
$subscribe = new subsribe;
$subscribe->email = $email;
$subscribe->save();
return Response::json(array(
'success' => true,
'msg' => 'Thank you for subscribing.We hate spam too.'
));
}else{
return Response::json(array(
'success' => false,
'msg' => 'Thank you for love, but you have already subscribed with us.'
));
}
}
public function getListing(){
$subscribers = subsribe::select(['id', 'email','total_subscription_received'])->get();
return Datatables::of($subscribers)->make(true);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
My RedirectUsers.php looks like this.
<?php
namespace Illuminate\Foundation\Auth;
use Illuminate\Support\Facades\Auth;
trait RedirectsUsers
{
/**
* Get the post register / login redirect path.
*
* @return string
*/
public function redirectPath()
{
if (property_exists($this, 'redirectPath')) {
return $this->redirectPath;
}
$auth_check = Auth::user()->is_admin;
if($auth_check == "yes" or Auth::user()->is_admin){
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/admin/homeInformation/1';
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/';
}
}
`
My Admin.php in middleware looks like this.
`<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Admin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ( Auth::check() && Auth::user()->isAdmin() )
{
return $next($request);
}else{
return redirect ('auth/login/');
}
}
}
My kenrel.php under HTTP looks like this.
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\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,
];
/**
* The application's route middleware.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'admin' => 'App\Http\Middleware\Admin',
];
}
Now what is happening is that even when a user who is not a admin is able to access the admin section.
When i change the controller middleware with 'admin' instead of 'auth' it redirects me to '/' instead of '/admin/{path}'