Limit Simultaneous Access in Django

Asked

Viewed 274 times

1

I wonder if there is any native way of the Djang framework itself to limit the amount of screens per user, I have researched about and can’t find anything to answer that. I want to prevent a user from being logged in to more than 1 screen, as with Netflix and Whatsapp online. I appreciate any response!

  • 1

    Are the sessions saved in a bank? If they are, just check if the user is logged in, if he is already logged in, do not allow the new login. or if he tries to log in delete sessions with his session table id

  • This certainly helped a lot. But I’m still having a hard time getting a session from a specific user. I’ve looked through the comic book to see if I can find any connection, but I can’t find.

  • 1

    Are you being saved to DB? if so: select count(*) from sessoes where user_id = 'id' if the answer is > 0 (You are logged in!), do not let it log in!

2 answers

1

I came back to tell you how I solved my problem. Being new still in Django I had to solve my way and following the tips that were given to me in this forum.

WHAT I DID

Knowing the user’s email I got his id.

user = User.objects.get(username = mail) user_id = user.id Then I searched all the sessions and with the for I looked for the session that had this one, but for that I had to decode before.

`sessoes = Session.objects.all()

        for k in sessoes:
            z = k.get_decoded()  #decodifica e pega a sessao

            if(z['_auth_user_id'] == str(user_id)): # compara sessaoes encontradas com a sessao do usuario que tenta logar`

Then what I did was just delete the session that had that id. NOTE: Also includes in Settings SESSION_EXPIRE_AT_BROWSER_CLOSE = True so that the session expires when the browser is closed.

Thank you all!

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.