Not able to login with Facebook using django-allauth - django

Previously I was working on my Django app on a local server and all my settings work with django-allauth (I was using manage.py runserver rather than nginx + gunicorn for staging/production)
Now, I'm implementing django-allauth to my staging website, but I can't make it work on my staging server (Note: In this example I've replaced my domain name to mydomain.com).
Basically after I clicked the link to login with Facebook at http://staging.mydomain.com/accounts/login/, it redirects to https://www.facebook.com/dialog/oauth?response_type=code&state=YXdAxg2WiIBo&redirect_uri=http%3A%2F%2Fstaging.mydomain.com%2Faccounts%2Ffacebook%2Flogin%2Fcallback%2F&client_id=1600059933550804&scope=email&auth_type=reauthenticate and I get the following errors:
Given URL is not allowed by the Application configuration.: One or
more of the given URLs is not allowed by the App's settings. It must
match the Website URL or Canvas URL, or the domain must be a subdomain
of one of the App's domains.
Here are my setting files:
nginx conf file
server {
listen 80;
server_name staging.mydomain.me;
location /static {
alias /home/myusername/sites/staging.mydomain.me/static;
}
location / {
proxy_set_header Host $host;
proxy_pass http://unix:/tmp/staging.mydomain.me.socket;
}
}
settings.py
...
SITE_ID = 1
LOGIN_REDIRECT_URL = '/'
ACCOUNT_EMAIL_REQUIRED = True
SOCIALACCOUNT_PROVIDERS = {
'facebook': {
'SCOPE': ['email'],
'AUTH_PARAMS': {'auth_type': 'reauthenticate'},
'METHOD': 'oauth2',
'VERIFIED_EMAIL': False,
}
}
AUTH_USER_MODEL = 'users.User'
ACCOUNT_ADAPTER = 'users.adapters.AccountAdapter'
ACCOUNT_SIGNUP_FORM_CLASS = 'users.forms.UserApplicationForm'
ACCOUNT_LOGOUT_ON_GET = True
...
Settings in Facebook app
Settings in Sites [http://staging.mydomain.com/admin/sites/site/1/]
Settings in Social application [http://staging.mydomain.com/admin/socialaccount/socialapp/2/]

Try one of theese 2 things:
create a test app for your facebook app, and set the staging.mydomain.com domain for it, configure a brand new social app (allauth) with the new api key and secret (for your new test app), a new django site, and so on...
change your facebook app settings to have staging.mydomain.com as its app domain.
I think both solutions will work, but I prefer the first one, as your staging app perfectly fits in definition of 'test app' and therefore, I think is a more elegant solution.
Hope this helps you.

Related

How to get django allauth google sign in to redirect to home page?

Hello I am using django and django all auth for authentication and am using the google feature so that when a user decides to sign in it authenticates with his google account then it will redirect him to the home page in my case to localhost:8000 in developement. But when I do try it does authenticate but instead of sending to localhost:8000 the home page it sends to this url: http://localhost:8000/accounts/profile/# not sure why.
Here is my settings.py file.
ACCOUNT_LOGOUT_REDIRECT_URL ='/'
ACCOUNT_LOGIN_REDIRECT_URL ='task_list'
ACCOUNT_SIGNUP_REDIRECT_URL = '/'
SOCIALACCOUNT_PROVIDERS = {
'google': {
'SCOPE': [
'profile',
'email',
],
'AUTH_PARAMS': {
'access_type': 'online',
}
}
}
any help I would appreciate Thank you.
From the docs
The default behaviour is to redirect authenticated users to
LOGIN_REDIRECT_URL when they try accessing login/signup pages
So all you need is to set this:
LOGIN_REDIRECT_URL = '/'

How to solve facebook django url social-auth-app-django problem?

Im trying to have social-auth-app-django facebook on my website and im getting this error, on localhost everything works but now when i changed settings to my website it isn't work :(
My settings.py:
LOGIN_URL='Logowanie'
LOGIN_REDIRECT_URL = 'MeetMe!'
SOCIAL_AUTH_FACEBOOK_KEY = '***********' # App ID
SOCIAL_AUTH_FACEBOOK_SECRET = '***********' # App Secret
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/przekierowanie/'
SOCIAL_AUTH_FACEBOOK_SCOPE = [
'email',
]
SECURE_SSL_REDIRECT = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SOCIAL_AUTH_REDIRECT_IS_HTTPS = True
I solve it on my own !
What i did:
added to settings.py
SOCIAL_AUTH_FACEBOOK_API_VERSION = '2.11'
i followed this tutorial
no such table: social_auth_usersocialauth
1.pip install python-social-auth==0.2.21
2.pip install social-auth-app-django
3.Set Django's app list and the backend-related settings according to the settings paragraph of the migration page.
4.Apply migrations: python manage.py migrate
5.Uninstall the old package: pip uninstall python-social-auth
i set on developers facebook status to live
Domain:
domain.pl
Url:
https://domain.pl
Valid OAuth Redirect URIs:
https://domain.pl/oauth/complete/facebook/
First Go To Setting.py And Put This Code End Of All Codes:
AUTHENTICATION_BACKENDS = [
'social_core.backends.facebook.FacebookOAuth2',
]
Second Go To FaceBook Developer In This Address :
https://developers.facebook.com
And Make One Account There.
After go To My Apps And Then Click On Create App And Then Put Your Website Name ((Attention...If You Use Your Local Host You Need To Put One Domain Name For Your Local Ip)) And Your Email And Click On Create App ID And In Your Dashboard Looking For Facebook Login And Click On Set Up .
And Then In First Step In Web Window Put Your WebSite Name For Local Host For Example Put mysite.com:8000/ And Click On Save And Other Options Just Cross .
Now In Your Dashboard On The Left Side Click On Setting And Then Basic If You See Your APP ID And Your APP SECRET Put This Two In Your Settings.py After That Last Code .
SOCIAL_AUTH_FACEBOOK_KEY = 'Put Your App Id Code Here'
SOCIAL_AUTH_FACEBOOK_SECRET = 'Put Your App Secret Code Here'
If You Want To Take User's Email Also You Can You This As Well .
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
Now In You Dashboard Go To Settings And After Go To Basic An In Field App Domains Put Your Website Name Also Go To Settings After Advanced And Put Website Name In Domain Manager With CLick On Add a Domain .
Now Again In Dashboard Go In Products+ Part And Click On Facrbook Login After Settings And Check These Option Be Active(Yes) :
• Client OAuth Login
• Web OAuth Login
• Enforce HTTPS
• Embedded Browser OAuth Login
And IN This Form Your Are Now Also Go In This Field Valid OAuth Redirect URIs And Put This URL :
exapmle.com/social-auth/complete/facebook/
And Put Your Button In Your Website Page That Have This Login Auth :
<li>
Sign in with Facebook
</li>

Can't access csrf cookie sent by server via Set-Cookie (Django + Angular2)

I'm experimenting with an app running on Angular 2 in the frontend and Django as API and I can't access the CSRF cookie sent from Django.
As you can see, I am getting back the cookie from the server but I cannot access it from the browser (document.cookie) or Angular 2 (using the ng2-cookies module).
Everything was working while testing on localhost, now client and server are running on different hosts and this might be the reason of the problem.
Client running on https://django-angular2-movies.firebaseapp.com/
Server running on https://glacial-shore-18891.herokuapp.com
This is part of the settings I'm using on Django:
settings.py
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
CSRF_COOKIE_SECURE = False
CSRF_TRUSTED_ORIGINS = ['django-angular2-movies.firebaseapp.com']
ALLOWED_HOSTS = ['*']
Other things that I have tried, but didn't help:
Removing the CSRF_TRUSTED_ORIGINS setting
Adding CSRF_COOKIE_DOMAIN = 'django-angular2-movies.firebaseapp.com'
Using { withCredentials: true } in the request sent from the client
Using the #ensure_csrf_cookie decorator on Django views
What am I doing wrong?
Thanks

django-allauth signin with google redirects to development url even in production

I created a Client ID and Client Secret from the google cloud api console and added a record in the Social apps table for django-allauth
I also added WEB ORIGIN:
mysite.com (prod)
http://localhost:8000 (dev)
and REDIRECT URI
http:mysite.com/accounts/google/login/callback/ (prod)
localhost:8000/accounts/google/login/callback/ (dev)
in the google api console.
Sign in with Google works great in development and redirects to the localhost callback url on successful sign-in. But I'm getting a redirect_uri_mismatch error in prod.
These are the error details from the google error page:
Request Details
cookie_policy_enforce=false
scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email
response_type=code
access_type=online
redirect_uri=http://127.0.0.1:8000/accounts/google/login/callback/
state=SOME_RANDOM_NUMBER
display=page
client_id=MY_CLIENT_ID
The redirect_uri is still set to 127.0.0.1 instead of http:mysite.com/accounts/google/login/callback/
So how do I set the proper redirect_uri?
This is my settings.py pertaining to django-allauth
INSTALLED_APPS = (
#the usual stuff
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
)
import django.conf.global_settings as DEFAULT_SETTINGS
TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
"allauth.account.context_processors.account",
"allauth.socialaccount.context_processors.socialaccount",
)
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
)
ACCOUNT_EMAIL_REQUIRED = True
LOGIN_REDIRECT_URL = "/"
Here's urls.py
urlpatterns = patterns('',
url(r'^accounts/', include('allauth.urls')),
)
I haven't made any other django-allauth settings.
I saw the docs and couldn't find where to make the change.
I found that the issue occurs because the nginx proxy, which sits in front of the python app server, sets the HTTP Host header to localhost.
So when allauth tries to do request.build_absolute_uri, HTTP_HOST is localhost.
So I set the Setting proxy_set_header in the nginx configuration file which fixed the issue.
proxy_set_header Host $http_host;
Also see Facebook oauth authentication is redirecting to localhost instead of to my domain for the same issue in a different avatar.

Does django take SCRIPT_NAME into account when using the default LOGIN_URL

I'm using Django 1.0.2 and trying to figure out how to get the #login_required working correctly.
When I use the built-in server it redirects to the default login_url, or my LOGIN_URL as defined in settings.py as expected.
What is not clear to me is how to deploy to the server where my site is not at the root. In my templates I use the url template tag, and in the views I can access request.META['SCRIPT_NAME'], but for some reason it doesn't seem to apply to the LOGIN_URL used.
What am I missing?
Thanks.
There is a setting called FORCE_SCRIPT_NAME that you should use to set the location of your script, if it's not hosted at the root directory. You can add it to the login/logout URLs:
# This is needed because we're running the app in non-root directory of web server
FORCE_SCRIPT_NAME = '/myapp.cgi/'
LOGIN_URL = FORCE_SCRIPT_NAME + 'accounts/login/'
LOGOUT_URL = FORCE_SCRIPT_NAME + 'accounts/logout/'
Reference: https://docs.djangoproject.com/en/dev/ref/settings/#force-script-name