FOSUserBundle override Login logic - authentication

I have override my Login form, so I want login by mobileNumber, not by username and password.
This is the template:
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<form action="{{ path("fos_user_security_check") }}" method="post">
{% if csrf_token %}
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
{% endif %}
<div class="main-text">Войти</div>
<div class="text-description-mobile-phone">Мобильный телефон</div>
<input type="text" name="mobileNumber" placeholder="+7(999) 123-45-67" class="info-input" required="required" />
<div class="text-description-password">Код из СМС</div>
<input type="text" name="code" placeholder="9876" class="info-input" required="required" />
<div class="line"></div>
<input type="submit" id="_submit" name="_submit" value="{{ 'Войти'|trans }}" class="enter-button"/>
</form>
Where does the login logic take place?
Where can I add checking of my own field?
How can I override user identification?
Can you provide an example?

I found the answer in the Internet. If someone will face the same problem, you can find solution there: https://ourcodeworld.com/articles/read/459/how-to-authenticate-login-manually-an-user-in-a-controller-with-or-without-fosuserbundle-on-symfony-3
Or in russian : http://fkn.ktu10.com/?q=node/9574

Related

Customer Register - Identify Existing Email

I'm trying on Shopify when a user is trying to create an account, to verify if the email that the user picked already exists, the theme I'm using is Debut. So far I've had 0 success with this.
<div class="page-width">
<div class="grid">
<div class="grid__item medium-up--one-half medium-up--push-one-quarter">
<div class="input-form form-vertical">
{% form 'create_customer' %}
<h1 class="text-center">{{ 'customer.register.title' | t }}</h1>
{%- if form.errors -%}
<div class="form-message form-message--error">
{{ form.errors | default_errors }}
</div>
{%- endif -%}
<label for="FirstName">{{ 'customer.register.first_name' | t }}</label>
<input type="text" name="customer[first_name]" id="FirstName" {% if form.first_name %}value="{{ form.first_name }}"{% endif %} autofocus>
<label for="LastName">{{ 'customer.register.last_name' | t }}</label>
<input type="text" name="customer[last_name]" id="LastName" {% if form.last_name %}value="{{ form.last_name }}"{% endif %}>
<label for="Email">{{ 'customer.register.email' | t }}</label>
<input type="email" name="customer[email]" id="Email" class="{% if form.errors contains 'email' %} input--error{% endif %}" {% if form.email %} value="{{ form.email }}"{% endif %} autocorrect="off" autocapitalize="off">
<label for="CreatePassword">{{ 'customer.register.password' | t }}</label>
<input type="password" name="customer[password]" id="CreatePassword" class="{% if form.errors contains 'password' %} input--error{% endif %}">
<label for="CreatePassword2">Repeat Password</label>
<input type="password" name="customer[password]" id="CreatePassword2" class="{% if form.errors contains 'password' %} input--error{% endif %}">
<p class="text-center">
<input type="submit" value="{{ 'customer.register.submit' | t }}" class="btn">
</p>
{% endform %}
</div>
</div>
</div>
</div>
I thought that the part of
{{ form.errors | default_errors }}
Would return an error when the email is duplicate but it does nothing and the user just gets redirected to the main page. Any idea of how this can be accomplished? Thank you

shopify quantity selector increment restriction

I need to setup the quantity selector in such a way that the user cant increment after the quantity exceeds the stock.
here's the code
{% if section.settings.show_quantity_selector %}
<div class="product-form__controls-group">
<div class="product-form__item">
<label for="Quantity-{{ section.id }}">{{ 'products.product.quantity' | t }}</label>
<input type="number" id="Quantity-{{ section.id }}" name="quantity" value="1" min="1" max="current_variant.inventory_quantity == 1" class="product-form__input product-form__input--quantity" data-quantity-input>
</div>
</div>
{% endif %}
You are close, try something like this:
<input type="number" id="Quantity-{{ section.id }}" name="quantity" value="1" min="1" max="{{ current_variant.inventory_quantity }}" class="product-form__input product-form__input--quantity" data-quantity-input>

how can I add products in a cart in Shopify by through a form in which only product name field is taken?

I have made a form page in Shopify in which following fields I have taken:-
<form action="/cart" method="post">
{% comment %}
Successful message
{% endcomment %}
{% if form.posted_successfully? %}
<p class="note form-success">
{{ 'contact.form.post_success' | t }}
</p>
{% endif %}
<div class="selection-wrapper">
{{ form.errors | default_errors }}
</div>
<div class="selection-wrapper">
<div class="grid">
<label for="Name" class="hidden-label">Name*</label>
<div class="grid__item medium-up--one-half">
<input type="text" style="width:100%;" name="first_name" id="FirstName" {% if form.first_name %}value="{{ form.first_name }}"{% endif %} required>
<label for="FirstName">{{ 'customer.register.first_name' | t }}</label>
</div>
<div class="grid__item medium-up--one-half">
<input type="text" style="width:100%;" name="last_name" id="LastName" {% if form.last_name %}value="{{ form.last_name }}"{% endif %}>
<label for="LastName">{{ 'customer.register.last_name' | t }}</label>
</div>
</div>
<br/>
<div class="grid">
<label for="Email" class="hidden-label">Email Address *</label>
<input type="email" style="width:100%;" id="Email" name="email" autocapitalize="off" value="{% if form.email %}{{ form.email }}{% elsif customer %}{{ customer.email }}{% endif %}">
</div>
<br/>
<div class="grid">
<label for="ContactFormPhone" class="hidden-label">Key Code Card Number*</label>
<input type="text" id="key_cord_card_no" style="width:100%;" name="keyCordCardNo" value="{{ form.keyCordCardNo }}" required>
</div>
<br/>
<div class="grid">
<label for="Product Name" class="hidden-label">Product Name*</label>
<input type="text" id="productName" style="width:100%;" name="product_name" value="{{ form.product_name }}" required>
the </div>
<div class="grid">
<p class="submit">
<input type="submit" class="button solid" value="submit">
</p>
</div>
</div>
</form>
I have to add product to cart by its name how can i do it?
How can I add products in a cart in Shopify by through a form in which only product name field is taken?
The short answer is - you can't add a product only by his name.
A product MUST have a variant ID in order for you to submit it to the cart.
The long answer is:
The only way to add a product by it's name is if you make an AJAX request to the product page and get the variant ID from there and submit that.
You always buy a variant in Shopify, even if a product doesn't have any variants it always have a defaut variant which is bought.
So if you plan to allow the customer to enter the product name in a text field you will need to make an AJAX request to the search page ( since I assume the customer won't be able to enter the exact name of the product in order for you to make a direct AJAX request ) and get the first result ( it will be best if you add the variant ID in the result item as a data attribute for example or some other way so that you don't need to make a second AJAX call to the product page itself ) and submit that to the cart.
Since the information given is so little I can't provide you any code or further instructions how to proceed.

the pk=request i have some errors in it. i cant seems to link my reserve to another page because of this error

def reserve(request):
if request.method=="POST":
user=User.objects.get(pk=request.POST['user'])
**book = Book.objects.get(pk=request.POST['book'])**
book.isAvailable=True
book.save()
reservation=Reserve(dateLoaned=datetime.now(),book=book, user=user)
reservation.save()
reservations=Reserve.objects.all()
return redirect('/library/reservations/')
else:
user=User.objects.all()
book=Book.objects.all()
return render(request, 'reserve.html',{"users":user, "book":book})
There is some error in my book = Book.objects.get(pk=request.post[book'])
Im doing a project that needs to be submitted on Monday. PLS HELP!
reserve.html
{% extends 'base.html' %}
{% load static %}
{% block title %}
Reserve
{% endblock %}
{% block maincontent %}
<form action="../reserve/" method="post">
{% csrf_token %}
<br>
<div class="table">
<input value="{{ book.title }}" class="form-control"
id="exampleFormControlInput1" enabled>
</div>
<div class="form-group">
<td>
<img src = "{{ book.cover }}" height="150" width="100"/>
</td>
</div>
<div class="form-group">
<br>
<label for="FormControlSelect1">Select Users</label>
<select class="form-control" id="FormControlSelect1" required
name="user">
{% for user in users %}
<option value="{{ user.pk }}">{{ user }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success btn-block">Reserve</button><br>
</div>
<input type="hidden" value="{{ book.pk }}" name="book" />
</form>
{% endblock %}
My reserve html might have some error. it should link my reserve to another page. but it didnt.
Error in the above image MultiValueDictKeyError. You have to replace the below code
reservation = Reserve.objects.get(pk=request.POST['reservation'])
to
reservation = Reserve.objects.get(pk=request.POST.get('reservation'))

Using Shopify how can I capture the customer address on registration and have it saved as the default address for billing and shipping?

I am creating a shopify website and I am trying to customize it so when a user / customer registers they can put their address information in at the time and have it saved as their default address. Shopify currently has it so you register then it takes you to an account page where you have the option of adding the address in.
I have tried making form elements on the register page which works as in they show up and get data.
But I don't know how to make the form submit the address data to the default address data locations.
In other words I don't know how to make it store the address as the default address.
I have looked at the code for the actual address page and tried plugin in the id's names and form values in the registration section with no luck. I have tried using id's and names associated with the api customer reference with no luck as well. I can't really find documentation on the subject. There is a section on capturing additional information at registration but its capture as "Notes" not as an address.
Any help is greatly appreciated thank you.
I created a second account page (customers/account.new-address.liquid) with the address registration form:
{% assign formID = "" %}
{% if formInfo.id %}
{% assign formID = formInfo.id | prepend: "_"%}
{% endif %}
{% form 'customer_address', formInfo %}
<div class="input-wrapper">
<label for="customer_addresses_first_name{{ formID }}">{{ 'customer.addresses.first_name' | t }}</label>
<input type="text" name="address[first_name]" value="{{form.first_name}}" id="customer_addresses_first_name{{ formID }}" />
</div>
<div class="input-wrapper">
<label for="customer_addresses_last_name{{ formID }}">{{ 'customer.addresses.last_name' | t }}</label>
<input type="text" name="address[last_name]" value="{{form.last_name}}" id="customer_addresses_last_name{{ formID }}" />
</div>
<div class="input-wrapper">
<label for="customer_addresses_company{{ formID }}">{{ 'customer.addresses.company' | t }}</label>
<input type="text" name="address[company]" value="{{form.company}}" id="customer_addresses_company{{ formID }}" />
</div>
<div class="input-wrapper">
<label for="customer_addresses_address1{{ formID }}">{{ 'Street Address' }}</label>
<input type="text" name="address[address1]" value="{{form.address1}}" id="customer_addresses_address1{{ formID }}" />
</div>
<div class="input-wrapper">
<label for="customer_addresses_address2{{ formID }}">{{ 'Suburb' }}</label>
<input type="text" name="address[address2]" value="{{form.address2}}" id="customer_addresses_address2{{ formID }}" />
</div>
<div class="input-wrapper">
<label for="customer_addresses_city{{ formID }}">{{ 'customer.addresses.city' | t }}</label>
<input type="text" name="address[city]" value="{{form.city}}" id="customer_addresses_city{{ formID }}" />
</div>
{% if isNew %}
<div class="input-wrapper">
<label for="address-country">{{ 'customer.addresses.country' | t }}</label>
<div class="select-wrapper">
<div class="selected-text"></div>
<select id="address-country" name="address[country]" data-default="{{form.country}}">{{ country_option_tags }}</select>
</div>
</div>
<div class="input-wrapper" id="address-province-container" style="display:none">
<label for="address-province">{{ 'customer.addresses.province' | t }}</label>
<div class="select-wrapper">
<div class="selected-text"></div>
<select id="address-province" class="new-address-province" name="address[province]" data-default="{{form.province}}"></select>
</div>
</div>
{% else %}
<div class="input-wrapper">
<label for="address-country-{{form.id}}">{{ 'customer.addresses.country' | t }}</label>
<div class="select-wrapper">
<div class="selected-text"></div>
<select id="address-country-{{form.id}}" name="address[country]" data-default="{{form.country}}">{{ country_option_tags }}</select>
</div>
</div>
<div class="input-wrapper" id="address-province-container-{{ address.id }}" style="display:none">
<label for="address-province-{{ address.id }}">{{ 'customer.addresses.province' | t }}</label>
<div class="select-wrapper">
<div class="selected-text"></div>
<select id="address-province-{{ address.id }}" name="address[province]" data-default="{{form.province}}"></select>
</div>
</div>
{% endif %}
<div class="input-wrapper">
<label for="customer_addresses_zip{{ formID }}">{{ 'customer.addresses.zip' | t }}</label>
<input type="text" name="address[zip]" value="{{form.zip}}" id="customer_addresses_zip{{ formID }}" />
</div>
<div class="input-wrapper">
<label for="customer_addresses_phone{{ formID }}">{{ 'customer.addresses.phone' | t }}</label>
<input type="text" name="address[phone]" value="{{form.phone}}" id="customer_addresses_phone{{ formID }}" />
</div>
<div class="inline-input-wrapper">
{% capture defaultAddressID %}
{% if isNew %}
address_default_address_new
{% else %}
address_default_address{{ formID }}
{% endif %}
{% endcapture %}
<label for="{{ defaultAddressID | strip_newlines | strip}}">
{{ form.set_as_default_checkbox }}
<span class="inline-label">
{{ 'customer.addresses.set_as_default' | t }}
</span>
</label>
</div>
<div class="input-wrapper cta-container">
<input class="button" type="submit" id="submit{{ formID }}" value="{{ 'general.general.submit' | t }}">
{% unless isNew %}
<button class="cancel-edit button secondary">{{ 'customer.general.cancel' | t }}</button>
{% endunless %}
{% if customer.addresses.size > 0 and isNew %}
<button class="toggle-new-address button secondary">{{ 'customer.general.cancel' | t }}</button>
{% endif %}
</div>
{% endform %}
and then added the following code to the top of the account page (customers/account.liquid) so when a customer accesses the account page after registering and they have 0 addresses on their profile they are redirected to add an address:
{% if customer.addresses.size == 0 %}
<meta content="0; url=/account?view=new-address" http-equiv="refresh" />
{% endif %}
The ?view=new-address is the link to the alternative account page we created above.
This can't be done on the registration form without the use of a third-party app. The solution provided by Muaaz above is the only workaround I'm aware of, and it's a good solution if you're okay with using 2 forms.
You can follow Shopify's tutorial to collect address fields (or other custom fields), but these fields are saved only to the customer note. Address fields will not save as the default address.
The Customer Fields app allows you to collect address fields and save that data as the customer's default address. This is available on the Lite plan, which allows you to collect any standard Shopify fields during registration.