Dheeraj
Dheeraj

Reputation: 59

AuthToken in Django Rest FrameWork: non_field_errors

I m trying to integrate token based authentication in DRF(Django version 1.10) but when I hit api-token-auth/ using {"username":"test","password":"123456789"} as mentioned in the doc it is required to return me the Token but I m getting

{
    "non_field_errors": [
        "Unable to log in with provided credentials."
    ]
}

I have used rest_framework.authtoken in my installed apps also token is getting generated once the user is registered and save in authtoken_token table .

Also in my urls.py of root I m using

urlpatterns += [
    url(r'^api-token-auth/', authviews.obtain_auth_token),
]

Any help would be appreciated. Also attaching the code

urls.py

urlpatterns += [
    url(r'^api-token-auth/', authviews.obtain_auth_token),
]

settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'users'
]

users/urls.py


from rest_framework.routers import DefaultRouter
from . import views as user_views
from django.conf.urls import url ,include


router = DefaultRouter()
router.register(r'user', user_views.UserViewSet,base_name="user")
urlpatterns = router.urls

Upvotes: 2

Views: 3322

Answers (2)

Dheeraj
Dheeraj

Reputation: 59

Thanks Hassan! The issue is resolved . I have used USERNAME_FIELD = 'email' & was using actual username in the post data. Also one more thing I wanted to clarify if anyone can...I m using make_password to hash my password also I can use user.set_password to hash my password in both the cases I m getting token successfully using api-token-auth. Which hashing algorithm or library does DRF authtoken actually using then? or we can hash using any library available Django will automatically decode it ?

Upvotes: 1

Muhammad Hassan
Muhammad Hassan

Reputation: 14391

You are probably not hashing your password and saving it as it is. In your view, you should save password like this.

user = User.objects.create(usename='test', first_name='first_name', email='[email protected]')
user.set_password('password')
user.save()

user.set_password will hash password.

Upvotes: 2

Related Questions