Login Required Redirect:

to understand where the problem is:

  • uncomment line 33 in /twittme-web/src/lookup/components.js
  • uncomment line 48 in /tweets/api/views.py
  • “console.log(“error”, e);” on line 42 in /twittme-web/src/lookup/components.js

image.png

reactjs redirect:

to test, npm start and runserver, access localhost:30 (reactjs)
image.png
for CORS, we don’t need “HTTP_X_REQUESTED_WITH” anymore, so:

  • don’t add that header in /twittme-web/src/lookup/components.js
    1. //...
    2. if (csrftoken) {
    3. //xhr.setRequestHeader("HTTP_X_REQUESTED_WITH", "XMLHttpRequest");
    4. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    5. xhr.setRequestHeader("X-CSRFToken", csrftoken);
    6. }
    7. //...

another error is if we like, unlike or retweet, we got a 403 error:
image.png

  • add a 403 redirect step in /twittme-web/src/lookup/components.js ```javascript xhr.onload = function () {

    1. //new

    if (xhr.status === 403) {

    1. const detail = xhr.response.detail;
    2. if(detail === "Authentication credentials were not provided."){
    3. window.location.href = "/login?showLoginRequired=true"
    4. }

    }

    callback(xhr.response, xhr.status); };

  1. access localhost:30 again: <br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/1243266/1595451245683-0b8bf3ba-944e-4bfc-8ca5-9d13b52c232e.png#align=left&display=inline&height=412&margin=%5Bobject%20Object%5D&name=image.png&originHeight=824&originWidth=1008&size=35312&status=done&style=none&width=504)<br />becuase everytime we get a 403, our url is redirected (but cannot handle it due to no corresponding page)
  2. <a name="280cY"></a>
  3. #### django redirect:
  4. - replace files in /static/js with files in /twittme-web/build/static/js
  5. - change script src in /templates/react/js.html to new filenames in /static/js
  6. ```html
  7. <!--
  8. <script src="/static/js/2.08ec51a1.chunk.js"></script>
  9. <script src="/static/js/main.b5a8ce6c.chunk.js"></script>
  10. -->
  11. <script src="/static/js/2.08ec51a1.chunk.js"></script>
  12. <script src="/static/js/main.3b3942bc.chunk.js"></script>

After that, we test by using incognito window, localhost:80(django) :
if we like, unlike or retweet, we get 404 with “/login?showLoginRequired=true”
image.png
if we tweet, we get 403 and then 404
image.png
image.png
so that django applied this login required redirect, either: