0

I have a Laravel website with a table 'users' that stores the role_id, and a table 'companies' that stores custom permalinks. The default login routes are used for the users with the ADMIN role. I need to add other views and routes for the users with the CLIENT role. So, when a user accesses the URL '/custom-company-permalink-from-db' and he is not logged in, he should be redirected to the URL '/custom-company-permalink-from-db/login'.

Here are my routes:

Route::middleware('web')->group(function() {
    Auth::routes(['register' => false]);

    Route::get('/', ['uses' => 'HomeController@index', 'as' => 'home']);
    Route::get('/{company}', ['uses' => 'HomeController@overview', 'as' => 'overview']);
});

Route::namespace('Admin')->prefix('admin')->name('admin.')->middleware(['auth', 'check.if.admin'])->group(function() {
    Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'AdminController@dashboard']);
});

I am thinking about adding this to the routes:

Route::get('/{company?}/login','Frontend\Auth\LoginController@showLoginForm')->name('client.login');

And then copying the app/Http/Controllers/Auth files to app/Http/Controllers/Frontend/Auth and making the necessary edits.

Is there another way of achieving this? What's the recommended way?

Mihaela
  • 87
  • 1
  • 1
  • 6
  • There is simple approaches i think you need some example of how you would do the checks :) – mrhn Feb 19 '20 at 23:27

1 Answers1

0

Since you are separating users based on roles, you can use the same login and differentiate routes based on namespace. Inside auth group, create two groups, one for admin (existing) and one for clients. You are already using a middleware to check if user is admin. Similarly you can create a new middleware to check if user is a client.

 Route::middleware('auth')->group....
    Route::namspace..prefix..->middleware('check.if.admin')->group(...
    ) //end check if admin group
    Route::namespace('client')->prefix('client')->middleware('check.client')->group(...)
 )//end auth group
Avi
  • 134
  • 8