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.
We are trying to use the standard Django authentication system
from accounts.forms import LoginForm, RegistrationForm
from django.contrib.auth import login as django_login, authenticate, logout as django_logout
from tradenity.sdk.entities import Customer
To create a new customer
camerastore/accounts/views.py
def register(request):
if request.method == 'POST':
form = RegistrationForm(data=request.POST)
if form.is_valid():
cd = form.cleaned_data
customer = Customer(firstName=cd['first_name'], lastName=cd['last_name'], email=cd['email'],
username=cd['username'], password=cd['password'])
customer.create()
return redirect('/')
else:
form = RegistrationForm()
return render(request, 'accounts/register.html', {'form': form})
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="/account/create" method="post" >
<ul>
<li class="text-info">First Name: </li>
<li>{{ form.firstName() }}</li>
</ul>
<ul>
<li class="text-info">Last Name: </li>
<li>{{ form.lastName() }}</li>
</ul>
<ul>
<li class="text-info">Email: </li>
<li>{{ form.email() }}</li>
</ul>
<ul>
<li class="text-info">Username: </li>
<li>{{ form.username() }}</li>
</ul>
<ul>
<li class="text-info">Password: </li>
<li>{{ form.password() }}</li>
</ul>
<ul>
<li class="text-info">Re-enter Password:</li>
<li>{{ form.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.
camerastore/accounts/views.py
def register(request):
if request.method == 'POST':
form = RegistrationForm(data=request.POST)
if form.is_valid():
cd = form.cleaned_data
customer = Customer(firstName=cd['first_name'], lastName=cd['last_name'], email=cd['email'],
username=cd['username'], password=cd['password'])
customer.create()
return redirect('/')
else:
form = RegistrationForm()
return render(request, 'accounts/register.html', {'form': form})
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.
camerastore/accounts/views.py
def login(request):
if request.method == 'POST':
form = LoginForm(data=request.POST)
if form.is_valid():
user = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password'])
if user is not None and user.is_active:
django_login(request, user)
return redirect('/')
else:
form = LoginForm()
return render(request, 'accounts/login.html', {'form': form})
camerastore/accounts/views.py
def logout(request):
django_logout(request)
return redirect('/')