Create Admin and User Login
- Nov 6, 2022
- 3 min read
Updated: Dec 13, 2022
First I have needed to add new column ‘role’ in users table. To do so, I used the bellow command in terminal to create migration.
php artisan make:migration add_role_to_users_table --table=users
I opened the migration and added role column.
$table->boolean('role')->default(0);

Now from terminal migrate, this way I added role column into the users table.
php artisan migrate
In Model/User add role column in fillable

To create middleware for admin I used the below command.
php artisan make:middleware role
In app/Http/Middleware/role.php: set a condition if role ==1 then move on otherwise redirect to user dashboard.

To create middleware for user, I used the below command.
php artisan make:middleware user
In app/Http/Middleware/user.php: set condition if role ==0 then move on otherwise redirect to admin dashboard.

app/Http/Kernel.php
Every application contains a HTTP kernel, it is live in app/Http/Kernel.php. The HTTP kernel or the console kernel serve as the central location, the incoming request is either sent to HTTP kernal or the console kernel. The HTTP kernel extents the Illuminate\Foundation\Http\Kernel class, it define an array of bootstrappers to configure error handling, configure logging, and implement other important tasks before the request is handled. Also HTTP kernel define a list of HTTP middleware to make sure requests must pass before being handled by application.

I have added role and user middlewares in kernel.php.
In app/Http/Controller/HomtController: created methods for admin and user to handle the request.
I have renamed the index method to userIndex and redirect it to index method in ContactController.

For admin, I have created adminIndex method, it retrieves all contacts, authenticates the user based on value of role column, and passes the retrieved data to admin-home view.

I made sure to import below instances and class into HomeController.
use Illuminate\Support\Facades\Auth class will check the incoming HTTP request if it is authenticated return true.
use App\Models\Contact - Contact Model
use App\Models\User - User Model
In app/Http/Controller/Atuh/LoginController
- I created login method and defined request as variable.
public function login(Request $request){
}
- Inside the login method, I created $input variable and got all requests.
$input = $request->all();
- I set validation.
$this->validate($request,[
'email'=>'required|email',
'password'=>'required'
]);
- I set condition, if user role is = 1 then redirect to admin dashboard otherwise redirect to user dashboard.
if(auth()->attempt(array('email'=>$input['email'],'password'=>$input['password'] ))){
if(auth()->user()->role ==1){
return redirect()->route('admin.home');
}else{
return redirect()->route('home');
}
}else{
return redirect()->route('login');
}

Make sure import use Illuminate\Http\Request; instance
Set up Route
Route/web.php: Rename index to userIndex from defined route and apply user middleware on it.
Route::get('/home', [App\Http\Controllers\HomeController::class, 'userIndex'])->name('home')
->middleware('user');
Define new route for admin and apply role middleware on it.
Route::get('/admin', [App\Http\Controllers\HomeController::class, 'adminIndex'])->name('admin.home')->middleware('role');
HTTP Exceptions
some exceptions explain HTTP error codes from the server. to generate error need to use the abort helper. In Laravel error (404) means "page not found" or error (401) means "unauthorized error"
In resources/views/home.blade.php
- Home page beside login and registration page by default have been created when I installed Laravel/ui package. I will build up this page as user dashboard and will use HTML code of this page as a template for other blade pages.
- @extends('layouts.app') - This way extends the app template, I defined a child view
- The body content will be place within @section and @endsection directives
- @if (session('status')) – It is used to display message for several purposes including redirecting, forgot password, reset password, email verification, success message for reset password. However, here in admin-home page, the @if (session('status')) has been used to displaying message for redirecting and deleting record.
- @foreach () - Blade provides this directive for looping purpose. I have used this directive to iterate the data and display them in table.
@can () - I have used this blade directive to the gate called facade.
- route () - Instead of URL helper method, I have used Named routes to generate url.
- @php($count=1) - Defined $count variable and looped it within for each loop to auto increment the table row in user dashboard.

Gates - It is a primary way of authorizing actions that provides a simple and closure-based approach. I have define the gate within the boot method of the app -> Provider - > AuthServiceProvider class. Then through blade directive (@can), I have applied it in home.blade.php to authorize each user can only access their own contact list.

In resources/views/admin-home.blade.php
I have copied HTML code of home.blade.php and pasted into admin-home with a few changes.
- @extends('layouts.app') // This way extends the app template, defines a child view
- The body content was placed within @section and @endsection directives
- @if (session('status')) – It is used to display message for several purposes including redirecting, forgot password, reset password, email verification, success message for reset password. However, here in admin-home page, the @if (session('status')) has been used to display message for redirecting.
- @foreach () - Blade provide this directive for looping purpose. I have used this directive to iterate the data and display them in table.

Short Demo of Admin and User Login




Comments