0

can you help me how to create if isadmin is true it will be redirected to admin page, else home page.

AuthController

public function postLogin(Request $request){ if(!auth()->attempt(['email' => $request->email, 'password' => $request->password])){ return redirect()->back(); } return redirect()->route('home'); } 

the main reason's maybe because this

return redirect()->route('home'); 

when tried change to ('admin') it successfully redirecting.

when i tried to add

protected function authenticated(\Illuminate\Http\Request $request, $user) { if( $user->isadmin){ return redirect('admin'); } return redirect('home'); } 

it didnt works too

2
  • Its possible using middleware and You can find the answer here: laracasts.com/discuss/channels/laravel/… Commented Nov 30, 2019 at 13:02
  • still didnt work Commented Nov 30, 2019 at 13:19

2 Answers 2

2

My approach to this situation is using middleware as @sssurii told you

I have a roles table which states normal users and admin user, and additionally I have a middleware such the following:

namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Response; class AdminMiddleware { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $user = Auth::user(); if(!$user->role->title==='Admin'){ return route('user'); } return $next($request); } } 

Then in kernel class, I've added that middleware in routes

protected $routeMiddleware = [ .... 'isAdmin' => \App\Http\Middleware\AdminMiddleware::class ]; 

Now you need to protect your admin routes, it is solver by

 Route::group(['middleware' => ['auth', 'isAdmin'], 'prefix' => 'admin', 'as' => 'admin.'], function () { Route::get('/home', 'Admin\HomeController@index')->name('dashboard'); ..... } 

Here you have a way to filter requests to admin routes, and let get in only users that belongs to group/role Admin. After that if you want an automatic redirect at login, you should modify redirectPath function in Auth controller (usually at app/http/controllers/auth/AuthController.php)

public function redirectPath() { if (\Auth::user()->role->title === 'Admin') { return redirect()->route('admin.dashboard'); } return redirect()->route('user.dashboard'); } 
Sign up to request clarification or add additional context in comments.

Comments

1

I suggest creating a middleware and using it to protect the route

Example, you can create an Admin middleware

php artisan make:middleware Admin 

In App\Http\Middleware\Admin.php

use Auth; use Session; use Closure; public function handle($request, Closure $next) { // Check if user has permission to access route if(!Auth::user()->admin) { Session::flash('info', 'You do not have permission to perform this operation!'); return redirect()->back(); } return $next($request); } 

Then in the protected route(assuming only your admin can view all posts in this route),

Route::post('admin/post/index', 'PostController@index')->middleware('auth'); 

Or in the controller

public function __construct() { $this->middleware('auth'); } 

Use except to exclude routes or only to include methods.

In the kernel.php

protected $routeMiddleware = [ ... 'admin' => \App\Http\Middleware\Admin::class ]; 

9 Comments

did we need to add column admin to user databases?
yes, there should be an admin column in the database
what should i choose for the type? it is boolean? or something else
something like this in your create_users_table migration $table->boolean('admin')->default(0);
any one you prefer to use. Just ensure it matches in the your code
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.