0

I know there is documentation for this question here https://docs.djangoproject.com/en/1.9/topics/auth/customizing/, however I need some clarification on my use case.

I basically have an api that I created. What this api does is submit user form data to an external url, and once submitted, the data is saved to that external urls database.

I can then get a specific user through another api call. The user is returned as an object. Now I am trying to log that user in on my end using the django.contrib.auth through my system but what I have isn't working.

Here is my process:

A. I added a custom authentication backend to my settings.py file

AUTHENTICATION_BACKENDS =(
    'new_app.backends.SettingsBackend',
    'django.contrib.auth.backends.ModelBackend',
)

B. In my new_app's backend file, I have the following code:

class SettingsBackend(object):
    def authenticate(self, username=None, password=None):
            if username and password:
                user = api.get_user()
                #add pk to user object to satisfy django error warning
                user.pk = user.unique_field
                return user
            return None

    def get_user(self, user_id):
        try:
            if self.user.pk == user_id:
                return self.user
        except:
            return None

In a signin view, I am trying to log the user in using this:

def login_user(request, username, password):
    from django.contrib import auth 
    if not username and password:
        raise ValueError("Invalid username and password")

    auth_user = auth.authenticate(username=username, password=password)

    if auth_user is None:
        raise ValueError("Invalid username and password")

    auth.login(request, auth_user)
    return auth_user

I get the error saying:

'api.get_user()' object has no attribute 'save'

The error is occuring on the auth.login(request, auth_user) line.

I think the reason is because the django.contrib.auth thinks my user object is a django model. So, the question is how should I tackle this to make it work given my scenario? Thanks for the time.

7
  • have you stated the changed user model in settings? Commented Mar 18, 2016 at 19:08
  • @RA123 thanks for the response. I didn't because this is not a typical django user model so I thought that wasn't neccessary. I feel like I am close but need to perhaps not use django.contrib.auth and make my own auth method but that's where I need some guidance. Commented Mar 18, 2016 at 19:55
  • Please first set AUTH_USER_MODEL = 'user_custom.MyUser' if you aren't using django's user model Commented Mar 18, 2016 at 19:57
  • @RA123 thanks but that doesn't work. This is a user object that is gotten from an api call, there is no custom user model that i have in my app models file. I'm not even using a models file since everything is gotten from an external database. I hope it makes sense Commented Mar 18, 2016 at 21:28
  • 1
    You cannot use login method, as it will look for the user id in whatever user database is defined (default if not) to create/continue sessions. Therefore it needs a user model class. Commented Mar 18, 2016 at 21:38

1 Answer 1

0

I'm not an expert at django subtleties (as it's been a while I left django for microframeworks).

That said, if I were you, what I'd do is to make sure that whatever api.get_user() is returning, implement whatever API django is expecting from that object, which means starting with an empty save() method on it.

But I'd also do standard django stuff, like @RA123 suggests in a comment, by setting AUTH_USER_MODEL up.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the input! Looking into that now. Will post answer when I have it working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.