Reputation: 159
Hello guys I finally can ask for help since I believe my issue is holding me back for the third day now googling. am using react in the frontend and Django in the backed and am trying to register user, the login is working like charm! but when I try creating new user I get error
Unauthorized: /rest-auth/registration/
[05/Jun/2019 10:34:45] "POST /rest-auth/registration/ HTTP/1.1" 401 27
I am sure that is the path to register user because when I visit the link in the browser it works fine. the issue is I am sending data from react frontend set like this
export const authSignUP = (username, email, password1, password2) => {
return dispatch => {
dispatch(authStart);
axios.post('http://127.0.0.1:8000/rest-auth/registration/', {
username: username,
email: email,
password1: password1,
password2: password2
}).then(res => {
const token = res.data.key;
const expirationDate = new Date(new Date().getTime() + 3600 * 1000);
localStorage.setItem('token', token);
localStorage.setItem('expirationDate', expirationDate);
dispatch(authSuccess(token));
dispatch(checkAuthTimeOut(3600));
})
.catch(err => {
alert(err)
// dispatch(authFail(err))
})
}
}
and my django settings file is like this
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
#This is required otherwise it asks for email server
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
REST_AUTH_SERIALIZERS = {
'TOKEN_SERIALIZER': 'jobs_home.serializer.TokenSerializer',
}
ROOT_URLCONF = 'jobs_dj.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, '../build')
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
#Following is added to enable registration with email instead of username
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
"django.contrib.auth.backends.ModelBackend",
# `allauth` specific authentication methods, such as login by e-mail
"allauth.account.auth_backends.AuthenticationBackend",
)
REST_USE_JWT = True
WSGI_APPLICATION = 'jobs_dj.wsgi.application'
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
CORS_ORIGIN_ALLW_ALL = True
CORS_ORIGIN_WHITELIST = (
'http://localhost:3000', 'http://127.0.0.1:8000', 'http://127.0.0.1:3000'
)
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
}
I will really use someones help in breaking this wall.. thanks
Upvotes: 0
Views: 3412
Reputation: 507
I just spent half an hour debugging this so maybe my solution will help someone.
My issue was that the browser had a token still and was sending it on registration (due to working on multiple projects on localhost). I would double check your request headers.
Upvotes: 1
Reputation: 159
I think setting the authentication class in my REST_FRAMEWORK settings in the settigs.py file was the issue I changed the settings to be
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
),
}
and it finally worked. this came to me whn even listing database objects needed authentication even after setting permissions to allow any. I knew that by default It was tring to authenticate the users even when I did not need it.
Upvotes: 0