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.
New customer registration
To create a new customer
src/AppBundle/Controller/AccountsController.php
<?php
/**
* @Route("/register", name="new_account")
*/
public function newAction(Request $request)
{
// replace this example code with whatever you need
return $this->render('accounts/register.html.twig', [
'customer' => new Customer()
]);
}
The registration form looks like this:
app/Resources/views/accounts/register.html.twig
<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="/accounts/create" method="post">
<ul>
<li class="text-info">First Name: </li>
<li><input type="text" value="{{ customer.firstName }}" name="firstName"/></li>
</ul>
<ul>
<li class="text-info">Last Name: </li>
<li><input type="text" value="{{ customer.lastName }}" name="lastName"/></li>
</ul>
<ul>
<li class="text-info">Email: </li>
<li><input type="text" value="{{ customer.email }}" name="email"/></li>
</ul>
<ul>
<li class="text-info">Username: </li>
<li><input type="text" value="{{ customer.username }}" 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 class="reg-right">
</div>
<div class="clearfix"></div>
</div>
To create a new Customer
instance, we simply populate the Customer
instance with data, then call Customer#create
method.
src/AppBundle/Controller/AccountsController.php
<?php
/**
* @Route("/accounts/create", name="create_account")
* @Method({"POST"})
*/
public function createAction(Request $request)
{
$customer = new Customer();
$customer->firstName = $request->request->get("firstName");
$customer->lastName = $request->request->get("lastName");
$customer->email = $request->request->get("email");
$customer->username = $request->request->get("username");
$customer->password = $request->request->get("password");
$confirmPassword = $request->request->get("confirmPassword");
if ($confirmPassword === $customer->password && $customer->isValid()) {
$customer->create();
return $this->redirect('/login');
}else{
return $this->render('accounts/register.html.twig', [
'customer' => $customer
]);
}
}
Customer login
Now, the customer created successfully. let’s implement the login functionality.
We try to get a customer with the specified ID using Customer#find_by_username
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.
src/AppBundle/Controller/SessionsController.php
<?php
/**
* @Route("/login", name="login")
*/
public function newAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render(
'sessions/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]
);
}