# my request.user is anonymous

**URL:** <https://forum.djangoproject.com/t/my-request-user-is-anonymous/12904>\
**Category:** Forms & APIs\
**Created:** [March 30, 2022, 2:22am UTC](https://forum.djangoproject.com/t/my-request-user-is-anonymous/12904 "2022-03-30T02:22:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![yutianyang1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/yutianyang1/32/7971_2.png) [@yutianyang1](https://forum.djangoproject.com/u/yutianyang1)\
**Post date:** [March 30, 2022, 2:22am UTC](https://forum.djangoproject.com/t/my-request-user-is-anonymous/12904/1 "2022-03-30T02:22:12Z")

</div>

when I use firefox or edge,after I use `user=authenticate(request,username=username,password=password)`  
`login(request,user)`,  
and then I reload my front-end page, the request.user is still anonymous,but when I use chrome, it login successfully,what’s wrong?

---

<div class="post-metadata">

**Author:** ![zakari-abdoul](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zakari-abdoul/32/7939_2.png) [@zakari-abdoul](https://forum.djangoproject.com/u/zakari-abdoul)\
**Post date:** [March 30, 2022, 11:43am UTC](https://forum.djangoproject.com/t/my-request-user-is-anonymous/12904/2 "2022-03-30T11:43:50Z")

</div>

I don’t really know exactly what happened but you can check these hypotheses:

- Clear your browsers cache or just navigate with private window.

- insert the `@login_required` decorator on the view

- you can also verifie if the [request.user.is\_anonymous](https://docs.djangoproject.com/en/4.0/ref/contrib/auth/#django.contrib.auth.models.User.is_anonymous) returns True.

---

<div class="post-metadata">

**Author:** ![yutianyang1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/yutianyang1/32/7971_2.png) [@yutianyang1](https://forum.djangoproject.com/u/yutianyang1)\
**Post date:** [March 30, 2022, 12:20pm UTC](https://forum.djangoproject.com/t/my-request-user-is-anonymous/12904/3 "2022-03-30T12:20:10Z")

</div>

thank you,I insert the @login\_required and the front-end page console said ```www.ytysite.ltd/:1

````
   Access to XMLHttpRequest at 'https://ytysite.ltd/accounts/login/?next=/settings/login/%3Fusername%3Dadmin%26password%3DyangT521.' (redirected from 'https://ytysite.ltd/settings/login?username=admin&password=yangT521.') from origin 'https://www.ytysite.ltd' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. ``` on edge,and the chrome still login successfully
````

---

<div class="post-metadata">

**Author:** ![zakari-abdoul](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zakari-abdoul/32/7939_2.png) [@zakari-abdoul](https://forum.djangoproject.com/u/zakari-abdoul)\
**Post date:** [March 30, 2022, 12:26pm UTC](https://forum.djangoproject.com/t/my-request-user-is-anonymous/12904/4 "2022-03-30T12:26:57Z")

</div>

you obviously using Django Rest Framework ?  
So you need to include the CORS management systems in your settings file.  
It allow your front to be able to communicate with the back.

---

<div class="post-metadata">

**Author:** ![zakari-abdoul](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zakari-abdoul/32/7939_2.png) [@zakari-abdoul](https://forum.djangoproject.com/u/zakari-abdoul)\
**Post date:** [March 30, 2022, 12:35pm UTC](https://forum.djangoproject.com/t/my-request-user-is-anonymous/12904/5 "2022-03-30T12:35:17Z")

</div>

You need these:

- `python -m pip install django-cors-headers `

In your installed app, insert this

- 

```nohighlight

 'corsheaders',

```

)

In your MIDDLEWARE, insert this

- 

```nohighlight

  'corsheaders.middleware.CorsMiddleware',
  'django.middleware.common.CommonMiddleware',
 

```

]``

After find somewhere at bottom to insert this

- `CORS_ALLOWED_ORIGINS = [*]`

this is the link for further informations [Cors](https://github.com/adamchainz/django-cors-headers#configuration)

---

<div class="post-metadata">

**Author:** ![yutianyang1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/yutianyang1/32/7971_2.png) [@yutianyang1](https://forum.djangoproject.com/u/yutianyang1)\
**Post date:** [March 30, 2022, 1:14pm UTC](https://forum.djangoproject.com/t/my-request-user-is-anonymous/12904/6 "2022-03-30T13:14:29Z")

</div>

thank you so much! but my console error changes to www.ytysite.ltd/:1

```
   Access to XMLHttpRequest at 'https://ytysite.ltd/settings/getinfo/?platform=web' from origin 'https://www.ytysite.ltd' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values 'https://www.ytysite.ltd, https://www.ytysite.ltd', but only one is allowed.
```

---

<div class="post-metadata">

**Author:** ![zakari-abdoul](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zakari-abdoul/32/7939_2.png) [@zakari-abdoul](https://forum.djangoproject.com/u/zakari-abdoul)\
**Post date:** [March 30, 2022, 1:28pm UTC](https://forum.djangoproject.com/t/my-request-user-is-anonymous/12904/7 "2022-03-30T13:28:52Z")

</div>

Try to put ‘\*’ which means “all” in ` CORS_ALLOWED_ORIGINS = ['*']`  
and add `CORS_ORIGIN_ALLOW_ALL = True`

Don’t forget the link in my third point

---

<div class="post-metadata">

**Author:** ![yutianyang1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/yutianyang1/32/7971_2.png) [@yutianyang1](https://forum.djangoproject.com/u/yutianyang1)\
**Post date:** [March 30, 2022, 2:10pm UTC](https://forum.djangoproject.com/t/my-request-user-is-anonymous/12904/8 "2022-03-30T14:10:00Z")

</div>

Is it related to my nginx? there is a `add_header 'Access-Control-Allow-Origin' '*' ` in my nginx.conf

---

<div class="post-metadata">

**Author:** ![zakari-abdoul](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zakari-abdoul/32/7939_2.png) [@zakari-abdoul](https://forum.djangoproject.com/u/zakari-abdoul)\
**Post date:** [March 30, 2022, 2:39pm UTC](https://forum.djangoproject.com/t/my-request-user-is-anonymous/12904/9 "2022-03-30T14:39:48Z")

</div>

I don’t think so, try to have access without the NGINX server to see the behavior .

---

<div class="post-metadata">

**Author:** ![yutianyang1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/yutianyang1/32/7971_2.png) [@yutianyang1](https://forum.djangoproject.com/u/yutianyang1)\
**Post date:** [March 31, 2022, 2:53am UTC](https://forum.djangoproject.com/t/my-request-user-is-anonymous/12904/10 "2022-03-31T02:53:34Z")

</div>

I delete the add\_header in my nginx.conf and the console error changed to  
`GET https://ytysite.ltd/accounts/login/?next=/settings/login/%3Fusername%3Dyty%26password%3DyangT521. 404 (Not Found)`  
now when I login
