Create customers, manage authentication and authorization.

Customer account management

In order for a customer to place an order, he/she must create an account first. this allow customers to track their orders, and let the store admin had enough information to deliver the order.

Tradenity API offers the Customer resource which provides all the necessary infrastructure to create and manage user account, login and logout, safely store sensitive information such as password in encrypted format.

In this section we will learn how to integrate Tradenity Customer resource and related services within your application to allow your customers to create and manage their accounts.

To create a new customer

app/Http/Controllers/AccountsController.php

<?php

public function register()
{
    $data = array('customer' => new Customer());
    return view('account/register', $data);
}

The registration form looks like this:

``


<h2>Registration</h2>
<div class="registration-grids">
    <div class="reg-form">
        <div class="reg">
            <p>Welcome, please enter the following details to continue.</p>
            <p>If you have previously registered with us, <a href="/login">click here to login</a></p>
            <form action="/register" method="post" >
                {{ csrf_field() }}
                <ul>
                    <li class="text-info">First Name: </li>
                    <li><input type="text" value="" name="firstName"/></li>
                </ul>
                <ul>
                    <li class="text-info">Last Name: </li>
                    <li><input type="text" value="" name="lastName"/></li>
                </ul>
                <ul>
                    <li class="text-info">Email: </li>
                    <li><input type="text" value="" name="email"/></li>
                </ul>
                <ul>
                    <li class="text-info">Username: </li>
                    <li><input type="text" value="" name="username"/></li>
                </ul>
                <ul>
                    <li class="text-info">Password: </li>
                    <li><input type="password" value="" name="password"/></li>
                </ul>
                <ul>
                    <li class="text-info">Re-enter Password:</li>
                    <li><input type="password" value="" name="confirmPassword"/></li>
                </ul>

                <input type="submit" value="REGISTER NOW"/>
                <p class="click">By clicking this button, you are agree to my  <a href="#">Policy Terms and Conditions.</a></p>
            </form>
        </div>
    </div>
</div>

To create a new Customer instance, we simply populate the Customer instance with data, then call Customer#create method.

app/Http/Controllers/AccountsController.php

<?php

public function create(Request $request)
{
    $customer = Customer::fromArray($request->all());
    $customer->create();
    return redirect('/login');
}

Now, the customer created successfully. let’s implement the login functionality. We try to get a customer with the specified ID using Customer#findByUsername method. If it returns valid customer, we check the password.

Please note that the password stored as encrypted text using bcrypt algorithm, so to check for its validity, either use the provided Customer#is_valid_password or implement your own bcrypt matching. plain text comparison will not work.

app/Http/Controllers/SessionController.php

<?php

public function create(Request $request)
{
    $customer = Customer::findByUsername($request->input('username'));
    $user = new User($customer);


    if ( (!is_null($customer)) && Hash::check($request->input('password'), $customer->password))
    {
        Auth::login($user);
        return redirect("/");
    }
    else
    {
        return redirect('/login');
    }
}


You can log out by POST to the delete action:

app/Http/Controllers/SessionController.php

<?php

public function delete(Request $request)
{
    Auth::logout($request->user());
    return redirect("/");
}