question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to login users with session authentication

See original GitHub issue

I’ve written the following mutation for logging users in. It’s been adapted to work with channels, before I was using Django’s default login function (which worked fine with graphene):

# ...
from asgiref.sync import async_to_sync
from channels.auth import login
# ...


class Login(graphene.Mutation):
    user = graphene.Field(UserNode)
    success = graphene.Boolean(required=True)
    errors = graphene.List(FormFieldError)

    class Arguments:
        username = graphene.String(required=True)
        password = graphene.String(required=True)

    def mutate(self, info, username, password):
        success = False
        errors = []
        user = authenticate(info.context, username=username, password=password)
        context = vars(info.context) # by default this is a namespace, `login` expects a dictionary

        if user is not None:
            async_to_sync(login)(context, user)
            success = True
            context['session'].save()
        else:
            errors.append(FormFieldError(
                field='password', messages=['Authentication failed.'],
            ))

        return Login(success=success, user=user, errors=errors)

This successfully logs the user in. I’ve inserted a breakpoint after the user is logged in, and info.context.user.is_authenticated is True.

The issue is this doesn’t persist. If you make another request, info.context.user.is_authenticated is False. It doesn’t persist the user’s session.

I followed the channels authentication documentation as recommended in this project’s README.

It seems like the issue might be that the user isn’t logged in within a consumer’s receive method, which is described in the channels docs. But this isn’t possible because I’m using graphene mutations to login the user (therefore the login code can’t be in the consumer).

How can I get this working? Thanks in advance.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dspacejscommented, Sep 11, 2019

@prokher sorry for the late response. I never managed to find a solution for this. Thanks for the fix!

1reaction
prokhercommented, May 6, 2019

Thanks for reporting. We will take a look.

Read more comments on GitHub >

github_iconTop Results From Across the Web

User can't authenticate or must authenticate twice
Troubleshooting an issue in which user can't authenticate or must authenticate twice when starting a remote desktop connection.
Read more >
Unable To Authenticate Session Error When Login to Different ...
Symptoms · 1. Open an IE session · 2. Log into E-Business Suite instance 1 using the URL http://<instance1>.<domain>:<port>/OA_HTML/AppsLocalLogin ...
Read more >
Unable to login user: invalid session / login information supplied
The login fails when a UNIX or Linux RWP server set up to authenticate using Samba that is configured to verify credentials from...
Read more >
Unable to login using the Use Windows Session Credentials ...
For this issue, see Logging in to vSphere Web Client fails when using the Use Windows session authentication option (2054277).
Read more >
How to troubleshoot user cannot login ? Authentication fails ...
If you can not list the account, it maybe missing the User Principal Name (UPN) or a Unix attribute. UPN must be filled...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found