A simple and up-to-date way to implement Facebook login in a Django app

Deadly 提交于 2019-12-06 13:53:36

There are multiple ways to approach the problem, what is the "best" way is really subjective.

Subjectively speaking, you could opt for django-allauth. Here are a few pointers to help you get started:

  • If you want to keep the signup simple, set SOCIALACCOUNT_AUTO_SIGNUP to True in order to achieve a "no questions asked" login. Users simply approve the FB dialog and they end up logged in in your site right away.

  • Adding a login button to your template is merely a matter of:

    <a href="{% provider_login_url "facebook" %}">Sign In</a>

  • The app offers support for the JS SDK login (pro: users are accustomed to the typical FB popup that appears), or you can use your own OAuth flow. Whatever you please.

The fastest way understand FB's Oauth 2.0 flow is to play with FB's Javascript SDK. Once you get the hang of it, the FB's PHP library is similar. Also, other OAuth sites like Google, Twitter or Dropbox have almost identical implementation.

In baby steps:

  1. Learn how to install FB Javascript SDK onto a simple page

  2. Use FB.login to determine login status and obtain the login url.

  3. Lastly, use FB.Event.Subscribe and subscribe to auth.statusChange to detect the login/logout changes.

Also, good to check out https://developers.facebook.com/roadmap/ on the upcoming features or features being removed.

django-social-auth is not just for Facebook, but that doesn't mean you should use all the backends available.

Project documentation is at http://django-social-auth.readthedocs.org/en/latest/index.html and Facebook backend details at http://django-social-auth.readthedocs.org/en/latest/backends/facebook.html.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!