Commit 97e89533 authored by Noe Nieto's avatar Noe Nieto 💬

Experiments failed because CORS

parent ef3ec8b3
Pipeline #5481 failed with stage
...@@ -7,12 +7,12 @@ ...@@ -7,12 +7,12 @@
{% block login_status %} {% block login_status %}
{% if DEBUG_REGISTER_STATUS %} {% if DEBUG_REGISTER_STATUS %}
{{ block.super }} {{ block.psuper }}
{% endif %} {% endif %}
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<form id="registrate" method="post" action="."> <form id="formilo" method="post" action="." data-woo-url="{{ WOO_URL }}">
{% csrf_token %} {% csrf_token %}
{{ form.as_p }} {{ form.as_p }}
<input type="submit" value="{% trans 'Submit' %}" /> <input type="submit" value="{% trans 'Submit' %}" />
...@@ -20,21 +20,50 @@ ...@@ -20,21 +20,50 @@
{% endblock %} {% endblock %}
{% block js_scripts %} {% block js_scripts %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js"></script>
<script> <script>
const READY_STATE_DONE = 4; var formilo = document.getElementById('formilo');
var cartURL = null;
var formilo = document.getElementById('registrate'); var WOO_URL = formilo.getAttribute('data-woo-url');
formilo.onsubmit = function (event){ var kuketol;
var peto = new XMLHttpRequest();
peto.onreadystatechange = function (evt){
if (peto.readyState == READY_STATE_DONE) {
console.log('State:', peto.readyState, '\nRequest:\n', peto);
$(formilo).on('submit', post_form);
function post_form(evt) {
evt.preventDefault();
$.post(
formilo.action,
$(formilo).serialize(),
function (data, status, xhr) {
if (xhr.status == 302) {
confirm_token(xhr);
} else {
$('body').html(data);
}
} }
}; )
peto.open('POST', formilo.action);
peto.send(new FormData(formilo));
return false; return false;
} }
function gimmie_cookie(name) {
var kuketoj = document.cookie.split(';').filter((item) => item.trim().startsWith(name+'='));
if (kuketoj.length) {
return kuketoj.pop().split('=')[1];
}
}
function confirm_token(xhr) {
$.ajax({
type: 'POST',
url: WOO_URL + '/wp-json/jwt-auth/v1/token/validate',
headers: {
'Authorization': 'Bearer: ' + gimmie_cookie('kuketo'),
}
})
.complete(function(xhr, status){
console.log(arguments);
});
}
</script> </script>
{% endblock %} {% endblock %}
...@@ -30,6 +30,7 @@ class CartRegistrationView(RegistrationView): ...@@ -30,6 +30,7 @@ class CartRegistrationView(RegistrationView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['DEBUG_REGISTER_STATUS'] = settings.DEBUG_REGISTER_STATUS context['DEBUG_REGISTER_STATUS'] = settings.DEBUG_REGISTER_STATUS
context['WOO_URL'] = settings.WOO_URL
return context return context
def get(self, *args, **kwargs): def get(self, *args, **kwargs):
...@@ -100,15 +101,10 @@ class CartRegistrationView(RegistrationView): ...@@ -100,15 +101,10 @@ class CartRegistrationView(RegistrationView):
version="jwt-auth/v1", version="jwt-auth/v1",
query_string_auth=settings.WOO_QUERY_STRING_AUTH, query_string_auth=settings.WOO_QUERY_STRING_AUTH,
) )
jwt_response = jwt_wcapi.post("token", {"username": username, "password": password}) jwt_response = jwt_wcapi.post("token", {"username": username, "password": password})
jwt_json = jwt_response.json() jwt_json = jwt_response.json()
jwt_status = jwt_response.status_code
jwt_token = jwt_json.get("token", None) jwt_token = jwt_json.get("token", None)
return jwt_token
if jwt_status == 200 and jwt_token is not None:
return 'Bearer {}' + jwt_token
def form_valid(self, form): def form_valid(self, form):
""" """
...@@ -121,5 +117,5 @@ class CartRegistrationView(RegistrationView): ...@@ -121,5 +117,5 @@ class CartRegistrationView(RegistrationView):
username=form.cleaned_data[User.USERNAME_FIELD], username=form.cleaned_data[User.USERNAME_FIELD],
password=form.cleaned_data['password1'] password=form.cleaned_data['password1']
) )
response['Location'] = '{}?token={}'.format(response['Location'], token) response.set_cookie('kuketo', token)
return response return response
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment