Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logout other devices while logging in on a new device #279

Open
SyedAhkam opened this issue Sep 5, 2022 · 3 comments
Open

Logout other devices while logging in on a new device #279

SyedAhkam opened this issue Sep 5, 2022 · 3 comments

Comments

@SyedAhkam
Copy link

I have a question to ask. I cannot figure out how to force previously logged in sessions to logout while attempting to login on a new device for which a session does not exist.

This is specifically helpful when paired with TOKEN_LIMIT_PER_USER=1, Without implementing this behaviour, a user would be forced to login on their previously logged in device before logging into a new one (matters are worse when its about a mobile app)

How can I achieve this behavior? Is anything wrong with that idea?

@SyedAhkam
Copy link
Author

I got it to work. Requires overriding the post method of KnoxLoginView.

class LoginView(KnoxLoginView):
    authentication_classes = (BasicAuthentication,)

    def post(self, request, format=None):
        if request.user.is_authenticated:
            print(f"User already authenticated but a new token was requested: {request.user.id}")

            # Delete all existing tokens for that user
            request.user.auth_token_set.all().delete()

        return super().post(request, format=format)

Perhaps exposing a pre_login signal could be a better solution?

@SyedAhkam SyedAhkam reopened this Sep 13, 2022
@SyedAhkam
Copy link
Author

Keeping it open for others to share ideas.

@SyedAhkam SyedAhkam reopened this Sep 13, 2022
@mathbouq
Copy link

Where should this code be implemented?
I inserted it in users/views.py, but it is never called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants