javascriptdjangofetchdj-rest-auth

dj-rest-auth registration fetch


Using dj-rest-auth for user auth and registration on my react app. Got login and logout to work using api endpoints in the docs. Failing to register a user, getting HTTP bad request 400. Reading on the web, explains that there's something wrong with my request but cannot figure what.

EDIT: Tried insomina to simulate POST registration response and got error on a Django's side:

ConnectionRefusedError at /api/dj-rest-auth/registration/
[Errno 61] Connection refused

register.js

fetch('/api/dj-rest-auth/registration/', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        user: 'alalala',
        password1: '1234',
        password2: '1234',
        email: 'ala@gmail.com',
      })
    })
.then(res => {
        res.json()
      })

settings.py

SITE_ID = 1
CORS_ORIGIN_ALLOW_ALL = True
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
    ]
}
REST_AUTH_REGISTER_SERIALIZERS = {
    'REGISTER_SERIALIZER': 'api.serializers.CustomRegisterSerializer'
}
INSTALLED_APPS = [
#local
    'api',
    # 3'rd party apps
    'rest_framework',
    'corsheaders',
    'rest_framework.authtoken',
    'dj_rest_auth', 

    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'dj_rest_auth.registration',

    # added django but it disable admin login logout pages
    'django.contrib.sites',
    
    # django default...
]

Solution

  • The issue was in the settings, in config of drf auth, in this case it would work with:

    ACCOUNT_USERNAME_REQUIRED = True
    ACCOUNT_EMAIL_REQUIRED = False
    ACCOUNT_EMAIL_VERIFICATION = 'none'
    ACCOUNT_AUTHENTICATION_METHOD = 'username'