0

As mentioned above that I want to use django "authenticate" method with one parameter as email because I use mastodon api for authenticating user and I already got access token from mastodon. After this I just want to check that whether that email is present in django's user table or not with the help of any django method. I used authenticate method but it fails in the use case of password reset. Is there any method in django which can authenticate user with only email. I mention my code below if any modification in the code make that code runs would be appreciable.

Django Code:

        mastodon_var = mastodon.Mastodon(

            client_id='gnowsys_ndf/ndf/NROER-client_cred.secret',
            api_base_url='https://member.metastudio.org'
        )

        access_token = None
        ####GET ACCESS FROM MASTODON HERE#######
        try:
            access_token  = mastodon_var.log_in(
            Username,
            Password,
            to_file='gnowsys_ndf/ndf/NROER-access_token.secret',

            )
        except Exception as e:
            print e
            pass        

        name = Username
        email = Username 
        password = Password
        # userna = mastodon_var.u"username"
        # print userna


        #######IF USER GETS ACCESS TOKEN THEN FOLLOWING CODE WILL BE EXECUTED##########
        if access_token:
            if User.objects.filter(username=name).exists():
                ####SECOND AND BY-DEFAULT LAYER FOR AUTHENTICATION
                user = authenticate(username=name,password=password)
                if user is not None:
                    if user.is_active:
                        login(request, user)
                        return HttpResponseRedirect( reverse('landing_page') )
                    else:
                        HttpResponse("Error1")
                return HttpResponseRedirect( reverse('landing_page') )
            else:
                member = User.objects.create_user(name,email,password)
                member.save()


                ####SECOND AND BY-DEFAULT LAYER FOR AUTHENTICATION
                user = authenticate(username=name, password=password)
                if user is not None:
                    if user.is_active:
                        login(request, user)
                        return HttpResponseRedirect( reverse('landing_page') )
                    else:
                        HttpResponse("Error2")
            return HttpResponseRedirect( reverse('landing_page') )   

        else:
            #t = TemplateResponse(request, 'login_template', {})
            #return t.render()
            return HttpResponse("OOps!!!!! You entered wrong credentials")
        #return HttpResponseRedirect( reverse('landing_page') )
    else:

        return HttpResponse("Invalid Credentials.")
Chintan Joshi
  • 149
  • 1
  • 14
  • Possible duplicate of [Django - Login with Email](https://stackoverflow.com/questions/37332190/django-login-with-email) – ramganesh Mar 28 '18 at 07:28
  • Actually I don't want password field for authentication.I just want email as a parameter for authentication because my authentication is already done by api. –  Mar 28 '18 at 09:21
  • Okay, How have you created user objects? – ramganesh Mar 28 '18 at 10:19
  • So here is the thing. I get username and password from user then I pass it to mastodon api and it will give me access token if user is authenticated. Then I check that whether this user is present in django user table if user is present then I will give access to landing page otherwise I create auth object of the user and then I add user in the table. Now the thing is if user change password on mastodon then i get access token but now the password is changed user is unable to land on landing page because password is mismatch on mastodon and django website.I want solution for that –  Mar 28 '18 at 10:26
  • "Now the thing is if user change password on mastodon then i get access token but now the password is changed user is unable to land on landing page because password is mismatch on mastodon and django website." at the time of changing a password in the mastodon you have to update user object with the same password. – ramganesh Mar 28 '18 at 10:30
  • After I get access token from api I want user to directly land on landing page. How can I achieve that? –  Mar 28 '18 at 10:30
  • You can override authenticate method and skip user.check_password condition. But "authenticate(username=name, password=password)" is not meaningful without password check. Ref this: https://stackoverflow.com/a/37332393/4628154 – ramganesh Mar 28 '18 at 10:36
  • Instead of using authenticate method is there any way around to directly land on landing page? –  Mar 28 '18 at 11:03
  • Yes, after member.save() statement you have to put redirect statement. In this way, Django User authentication will not work. – ramganesh Mar 28 '18 at 11:33

0 Answers0