Skip to main content

Django Facebook Login

django-facebook-login provides an authentication backend and a GraphQL mutation that takes a Facebook user-access-token and the user's email and then does one of the following:

  • Sign-up new user
  • Connect existing Django user with their Facebook account
  • Login existing, already connected Django user

In all cases, the user will be authenticated afterwards. This means, unlike most other custom authentication backends, this backend will create a new user if the given credentials (Facebook email + Facebook user access token) are not known, yet.

Make sure you read the Noteworthy Things below before you decide to use this library.

Quick start

  1. Add "facebook-login" to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
        ...
        'facebook-login',
    ]
    
  2. Add the FacebookAuthBackend to your AUTHENTICATION_BACKENDS setting:

    AUTHENTICATION_BACKENDS = (
        ...,
        "facebook_login.auth_backends.FacebookAuthBackend",
    )
    
  3. Hook up the mutation in your GraphQL schema:

    # in your main `schema.py`:
    import graphene
    from facebook_login import schema as fb_login
    
    class Mutation(
        ...
        fb_login.Mutation,
        graphene.ObjectType,
    ):
        pass
    
    class Queries(...):
        pass
    
    schema = graphene.Schema(query=Queries, mutation=Mutation)
    
  4. Run python manage.py migrate to create the FacebookAccount table.

  5. Configure the app in your local_settings.py:

    # Get these values from https://developers.facebook.com/apps/
    FB_LOGIN_APP_ID = 'YOUR APP ID'
    FB_LOGIN_APP_SECRET = 'YOUR APP SECRET'
    

Noteworthy things

This library does not include frontend code

You still need extra code on your frontend that retrieves the user access token from Facebook. Usually you would hook up the official Facebook login button that triggers the official Facebook login popup and then write some code that sends the token that was returned by Facebook to our mutation.

This library forces the user to grant access to their Facebook email

During the official Facebook login popup, the user can decide to revoke access to the email address. Other libraries, like django-allauth will have some extra views where the user is then asked to enter an email anyways, after the Facebook login. We do not care about this. Instead, we will ask the user to press the login button again and this time please grant access to the email address.

This library does not return a JWT token or anything like it

Please note that we don't use JWT in our projects. We use Django's default session based authentication. Therefore, our mutation does not return anything.

Our mutation does call Django's login() function, which will save the new login-state into the user's session. When the mutation returns, it will instruct the browser to save the new session key in the cookie. Our frontend will then trigger a window.location = /new/url/, since this is a new request (including the new session key), the server-rendered response will realize that this is a now logged-in user.

If you would like to disable this behavior, you may provide a custom function for the FB_LOGIN_SUCCESS_HANDLER setting (see below).

Configuration

This app uses the following settings:

FB_LOGIN_APP_ID (mandatory)

This should be your Facebook app-id.

FB_LOGIN_APP_SECRET (mandatory)

This should be your Facebook app secret.

FB_LOGIN_SUCCESS_HANDLER (optional)

Default: facebook_login.utils.success_handler_default

Set this to your own function in case you need to do additional things when a user logs in. You can find our original implementation in utils.success_handler_default().

Your custom function may return anything that can be passed into json.dumps(). If you do return something (i.e. a JWT token), then the mutation will return it to the frontend as the extra key.

FB_LOGIN_API_BASE_URL (optional)

Default: 'https://graph.facebook.com/v3.1'

Allows to override the base API URL, just in case. Of course, we are not sure, if a future API would be backwards compatible, so just changing this to a higher API version number might cause issues with this library.

Troubleshooting

KeyError: 'password'

If this happens, chances are that you are using django-allauth. Their authentication backend crashes when Django's authenticate() function is called without a username and password keyword-argument. As a workaround, you can just make sure that facebook_login.auth_backends.FacebookAuthBackend appears before other authentication backends.

Contributing

  • Clone this repo
  • mkvirtualenv --python=python3.6 django-facebook-login
  • pip install -r requirements.txt
  • pip install -r test_requirements.txt
  • fab test
  • open htmlcov/index.html
  • ./manage.py migrate # This creates a sqlite3 DB
  • ./manage.py createsuperuser
  • ./manage.py runserver

Unfortunately, running the local devserver only gives you access to the Django admin. There is no demo-frontend code that would actually call this library's backend code, yet.

Acknowledgements

This library was built with love at The Artling

Release files for django-facebook-login 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-facebook-login 1.0.3
File Size Uploaded
django-facebook-login-1.0.3.tar.gz 12.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-facebook-login 1.0.3
File Interpreter ABI Platform
django_facebook_login-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 26.7 kB

Release files / django-facebook-login-1.0.3.tar.gz

Download URL django-facebook-login-1.0.3.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e050f10ccf5d227b9f52c493c6ea93c44a1362685f660a43a62f0ae4dea97d13
BLAKE2b-256 checksum
How to use checksums
7ab13e74f9562c6c2916157a7e01d8b73ac80ec1afd5de3846d078d67bb9b800
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

Release files / django_facebook_login-1.0.3-py3-none-any.whl

Download URL django_facebook_login-1.0.3-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6418a337f1094a919a66367f8258827a8ee52380a2bf687830af5b71c02d9dee
BLAKE2b-256 checksum
How to use checksums
7825091980061da4764bca64dff11a77d8791330c682bef30622b023508512ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

Release history Release notifications | RSS feed

5.1.2

2 release files

4.1.2

1 release file

4.1.1

1 release file

4.1.0

2 release files

4.0.0

2 release files

3.0.0

2 release files

2.0.0

2 release files

1.0.4

2 release files

This release

1.0.3 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page