rtgdk / sshearts

Facebook Authorising App for Social Sweethearts

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Facebook Authorising App for Social Sweethearts

Explanation of Features

  1. Used social-auth-app-django to implement facebook login. This module is helpful to extend the login to github, google and other oauth providers.
  2. The access token provided by social-auth-app-django is long lived and is stored in the User social auths table under extras field
  3. When user connects the facebook account, the pipeline function - load_user_and_profile_pic (app/pipeline.py) - to save the name, profile picture and profile url in AppFbUser table.
  4. When user disconnects the app, the pipeline function - disconnect_user (app/pipeline.py) - makes user.is_active=false in the database and the user is logged out.

Installation Instructions

  1. Clone or download the repository.

  2. Create a new virtual environment for the project.

    virtualenv -p python3 venv
    source venv/bin/activate
  3. Go into the repo and install required python libraries giving in the requirements.txt file.

    cd sshearts
    pip install -r requirements.txt
  4. Run Django migrate.

    python manage.py migrate
  5. Fill in the SOCIAL_AUTH_FACEBOOK_KEY and SOCIAL_AUTH_FACEBOOK_SECRET environment variable in your machine. (See Instruction for creating FB App)

    export SOCIAL_AUTH_FACEBOOK_KEY=<your-fb-app-id>
    export SOCIAL_AUTH_FACEBOOK_SECRET=<<your-fb-app-secret-key>
  6. Start the application.

    python manage.py runserver

Instruction for creating FB App

  1. Go to developers.facebook.com/ click on My Apps and then Add a New App.
  2. Fill in the app name.
  3. From Settings > Basic, copy your app id and secret key to SOCIAL_AUTH_FACEBOOK_KEY and SOCIAL_AUTH_FACEBOOK_SECRET
  4. Click on Add a Platform and select Website
  5. For site url put http://localhost:8000 , if running locally else your app web url.
  6. And then in App Domains put localhost if running locally else put your website domain.
  7. Now go to Products > Facebook Login > Settings and put http://localhost:8000/app/deauthorize/ in the Deauthorize Callback URL. Or '/app/deauthorize/' if not running locally.

Notes

  1. I didn't know whether the profile picture should be saved in the database and should be picked up from facebook graph api. So I implemented both the scenario. After logging in, you would see 2 profile images - the left one is from graph api and the right one is from saved media picture.

  2. I didn't upload my app id and secret key to the repo since it's not safe to upload them on github. So I stored them on machine's environment variables and used them in the code(import os). Hope you can set up app id and secret in your environment variables too for successfully running the code)

  3. Run with DEBUG=False in production, to redirect AuthCanceled error (when user cancels the facebook app request) to the home page.

  4. I could have used class based view to make it more easily extendable and reusable but the amount of http request to be implemented is too less so I used function based view instead.

About

Facebook Authorising App for Social Sweethearts


Languages

Language:Python 79.6%Language:HTML 19.7%Language:CSS 0.8%