Laravel CMS | N-09 | Admin Registration

Video Tutorial

Overview

So it is time for us in order to get into a very important part of our CMS which is about Admin registration and Authentication, so in this tutorial, we are going to implement the admin dashboard where it will allow you to add new admins and delete them.

It will allow us in the next tutorial, to cover the Admin Authentication (Login) and Routes Restriction, where laravel provides a very brief authentication system from the ground up, but still, we need to make it ours by wrapping around our content management system main auth system.

Admin Page

First, we need to create the main admin page it is going to be composed of two main parts (Table for View the registered admins) and (a Form for adding new admins to the database).

So the Admins Table comes first

<!-- admin.blade.php --!>
@extends("layouts.app");

@section("main-row")

    <!-- Side Area BAR (Navifation) -->
    @include('inc.sideArea')
    <div class="col-md-8" id="main-admin">
        <div id="admin-container">
            <div id="admin-table" class="table-responsive">
                @if(count($admins) > 0)
                <table class="table">
                    <tr>
                            <th>NO</th>
                            <th>Username</th>
                            <th>Email</th>
                            <th>Date Added</th>
                            <th>Hashed Password</th>
                            <th>Edit</th>
                            <th>Delete</th>
                    </tr>
                    @for($i = 0; $i < count($admins); $i++)
                        <tr>
                                <?php $admin = $admins[$i]; ?>
                                <td>{{ $i + 1 }}</td>
                                <td>{{ $admin->username }}</td>
                                <td>{{ $admin->email }}</td>
                                <td>{{ $admin->created_at }}</td>
                                <td>{{ $admin->hash_password }}</td>
                                <td>edit button</td>
                                <td>remove button</td>
                        </tr>
                    @endfor
                </table>
                @else
                 <!-- Empty! then we --> 
                <h3>No Admins Are Available</h3>
                @endif
            </div>
         </div>
     </div>
@endsection

Then we add the Registration Form (Since you were already familiar with laravel forms)

<div class="divider"></div>
<div class="admin-form">
    <h4>Add New CMS Admin</h4>

    {!! Form::open(["route" => "admin.store", "method" => "POST"])!!}

        <div class="form-group">
            {!! Form::label("username", "Name(UserName)") !!}
            {!! Form::text("username",
            "", ["class" => "form-control", "placeholder" => "UserName"]) !!}
        </div>
        <div class="form-group">
            {!! Form::label("email", "Email") !!}
            {!! Form::text("email", "", ["class" => "form-control", "placeholder" => "Email"]) !!}
        </div>
        <div class="form-group">
            {!! Form::label("password", "Password") !!}
            {!! Form::password("password", ["class" => "form-control", "placeholder" => "Password"]) !!}
        </div>
        <div class="form-group">
            {!! Form::label("confirmPassword", "Confirm Password") !!}
            {!! Form::password("confirmPassword", ["class" => "form-control", "placeholder" => "Confirm"]) !!}
        </div>
        <div class="form-group">
            <small class="text-muted"><span class="glyphicon glyphicon-info-sign"></span> Make Sure you Information are Correct before Submitting.</small>
            {!! Form::submit("Register", ["class" => "btn btn-success btn-block"]) !!}
        </div>
    {!! Form::close() !!}
</div>

And this all we need to include on the Admin page, to make this work we have to add the right routes to take us to the admin page whenever we submit the /admin path, so under the web.php routes file add

//Web.php
/* ADMIN Routes */
Route::get("/admin", "AdminController@index")->name("admin.index");
//Registing new Adming 
Route::post("/admin/register", "AdminController@store")->name("admin.store");

Since we need to store the Admins data in the database so we need a model for that.

#Create Admin Model
php artisan make:model Admin

Change the Admins table to (Admin).

Admin Controller

When we submit the form we are going to hit the /admin/register route and that should take us to the admin controller store method, so let’s create that as well.

#Create Admin Controller using Artisan
php artisan make:controller AdminController --resource

First, let’s create the index method since it is responsible for finding already registered admins in the database and returns them alongside the admin page view

//Use Admin Model 
use App\Admin
class AdminController extends Controller {
...
    public function index() {
        //Get Admins if any!
        $admins = Admin::orderBy("id")->get();
        return view("pages.admin")->with("admins", $admins);
    }
...

if you tried to access /admin you should get the admin page with an empty table and the registration form.

Now for storing new admins in the database, we do the same thing as we did with the posts and categories.

...
    public function store(Request $request) {
        //Validate
        $this->validate($request, ["username" => "required", "email" => "required|email", "password" 
         => "required", "confirmPassword" => "required"]);
        //Check if Password and Confirm Passwords Match
        if($request->password != $request->confirmPassword) {
            //Flash Validation Error
            session()->flash("Error", "Password Does Not Match!");
            return back()->withInput(["username" => $request->username, "email" => $request- 
             >email]);
        }
        //New Admin
        $admin = new Admin();
        $admin->username = $request->username;
        $admin->email = $request->email;
        $admin->password = Hash::make($request->password);

        //Save
        $admin->save();
        //Success Session
        session()->flash("Success", "Admin " . $request->username . " Registered Successfully!");
        //Success
        return back();
    }
...

We validate the admin form using inputs name that we have defined under the blade template and since we have a password and a confirm password fields for admin registration we need to make that the passwords match by checking both of them and return a session error if they don’t with the username and email inputs for saving the user a bit of time of retyping the whole thing.

Truth Time, try to access the admin page and add new admins thought the form after submit you should new user showed in the admin’s table.

What’s Next

So that was only the start of the CMS Authentication system that we are going to implement in the next tutorial since now we have the admins database and dashboard that we can rely on upon Admin Login.

For the Login, we will use builtin laravel functions and classes that are going to helps us a lot in terms of achieving a fast and flexible auth system.

No Comments Yet