Sunday, February 28, 2021

Re: Log out all sessions for current user logged in

New code for logging out all sessions of logged in users.

```
# Logout all devices in account security page.
def logoff_all(request):
try:
# Filtering all session objects.
session_query = Session.objects.all()

# Iterating session_query and fetching session key.
for s in session_query:
# Decoding session key and getting id from that.
var_id = s.get_decoded().get('id')
# Converting string to int data type.
var_id_user = int(var_id)
# Id from decode and logged in session id is same then
# delete that session key from Session table from database.
if var_id_user == request.session.get('id'):
s.flush()
messages.success(request, "All sessions are logged off successfully.")
return HttpResponseRedirect("/home")
except Exception as e:
logging.error(e)
print(e)
return HttpResponseRedirect("/home")
```
**Error**
```
'Session' object has no attribute 'flush'
```

On Sat, Feb 27, 2021 at 7:45 PM Ryan Nowakowski <tubaman@fattuba.com> wrote:
Please reply with your new code and copy and paste the exact error message with full traceback.

On February 26, 2021 6:40:28 PM CST, Salima Begum <salima.b@rohteksolutions.com> wrote:
Yes. I have tried that no it is not working and giving error like "Query object doesn't contain flush()"

On Sat, 27 Feb 2021, 12:35 am Ryan Nowakowski, <tubaman@fattuba.com> wrote:
Have your tried my suggestion below? Did it work?

On February 25, 2021 9:42:55 PM CST, Salima Begum <salima.b@rohteksolutions.com> wrote:
Logout all sessions belong to logged in user Can you please how can I achieve this?

Thanks
~salima

On Fri, Feb 26, 2021 at 4:56 AM Ryan Nowakowski <tubaman@fattuba.com> wrote:
On Wed, Feb 17, 2021 at 08:23:08AM +0530, Salima Begum wrote:
> Logout all sessions based on current user id and clear django sessions
> table based on user id. It is logging off all sessions.
>
> ```
> # Logout all devices in account security page.
> def logoff_all(request):
>     try:
>
>         # Filtering all session objects.
>         session_query = Session.objects.all()
>
>         # Iterating session_query and fetching session key.
>         for s in session_query:
>             # Decoding session key and getting id from that.
>             var_id = s.get_decoded().get('id')
>             # Converting string to int data type.
>             var_id_user = int(var_id)
>             # Id from decode and logged in session id is same then
>             # delete that session key from Session table from database.
>             if var_id_user == request.session.get('id'):
>                 s.delete()
>         messages.success(request, "All sessions are logged off
> successfully.")
>         return HttpResponseRedirect("/home")
>     except Exception as e:
>         logging.error(e)
>         print(e)
>     return HttpResponseRedirect("/home")
> ```

I would call s.flush() instead of s.delete().  That's what Django's
normal logout function does[1].

[1] https://github.com/django/django/blob/a948d9df394aafded78d72b1daa785a0abfeab48/django/contrib/auth/__init__.py#L149

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20210225232524.GG19963%40fattuba.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/A440B5D5-3000-4004-9093-0F685D23BAD2%40fattuba.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4EFB4A20-91CC-4CE0-B810-90C0694BF497%40fattuba.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMSz6bmaD7%3DxBKHHUY%2BJ_HfxrzEVAvg0Jeo%2BZxUscbkZmSgKvA%40mail.gmail.com.

No comments:

Post a Comment