Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Flask Social Login with Github - Free sample included
Sm0ke
Sm0ke

Posted on • Edited on • Originally published atblog.appseed.us

     

Flask Social Login with Github - Free sample included

Hello Coders!

This article presents an open-source sample that implements GithubSocial Login on top of Flask, a popular web framework written in Python. The project source code can be downloaded and used from Github (MIT Licence) and also configured to work with other well-known OAuth2 providers like Google, Twitter, or Facebook.

Thanks for reading! - Content provided byApp Generator.


Flask Social Login - Source Code (Github)


The code usesFlask-Dance to bridge the authentication flow to the Github OAuth interface. In order to make the app more appealing a few simple pages styled with Bootstrap 5 are also provided.


image


The authentication flow provided byFlask-Dance library requires only a few lines of code to implement a Social Authentication via Github (and not only).

The first thing we need to do is to access Github and create a new OAuth application that will provideOAUTH_CLIENT_ID andOAUTH_SECRET_KEY required byFlask-dance library.


Create OAuth APP on Github

In order to do this, we need to accessGithub ->Settings (top right corner) ->Developer Settings from the left menu.


Flask Social Login - Create OAuth App on Github


Access theOAuth Apps item from the left menu and click to create a newAPP:


Flask Social Login - OAuth APPS Menu


Once the new page is loaded we can create a sample APP. The most important settings are listed bellow:

  • Client ID - (readonly) generated by Github - to be saved in.env file
  • Client Secrets - we need to generate one and use it in.env file
  • Application Name - important for public services
  • Homepage URL:https://localhost:5000
  • Authorization callback URL:https://localhost:5000/login/github/authorized

Please note that all URLs useHTTPS schema, something that we need to take care in our project. Once the project is saved, we can go back to our project, save the information provided by Github and start it.


Flask Social Login - Compile project

As mentioned before, source code is published on Github and we can start and test this simple project with a few lines type in the terminal. Please note that the project requires aminimal programming kit already installed in the workstation:

  • Python3 - a modern script language used for many types of projects
  • GIT - a command-line tool used to download sources from Github
  • Amodern code editor -VSCode orAtom

Once all tools are installed and accessible in the terminal we can proceed further and compile the project by following the instruction provided by theREADME file.


Step #1 - Clone sources (public repository)

$# Clone the sources$git clone https://github.com/app-generator/flask-social-login.git$cdflask-social-login
Enter fullscreen modeExit fullscreen mode

Step #2 - Create a virtual environment

$# Virtualenv modules installation (Unix based systems)$virtualenvenv$source env/bin/activate$$# Virtualenv modules installation (Windows based systems)$# virtualenv env$# .\env\Scripts\activate
Enter fullscreen modeExit fullscreen mode

Step #3 - Install dependencies

$# Install requirements$pip3install-r requirements.txt
Enter fullscreen modeExit fullscreen mode

Step #4 - Set Up Environment

$# Set the FLASK_APP environment variable$(Unix/Mac)exportFLASK_APP=run.py$(Windows)setFLASK_APP=run.py$(Powershell)$env:FLASK_APP=".\run.py"
Enter fullscreen modeExit fullscreen mode

Step #5 -Creating an OAuth App (on Github)

  • SignIN to Github
  • AccessSettings ->Developer Settings ->OAuth Apps
  • Edit your OAuth App
    • App Name
    • App Description
    • (mandatory)HomePage:https://localhost:5000
    • (mandatory)Authorization callback URL:https://localhost:5000/login/github/authorized
    • Generate a newsecret key

Step #6 - Rename.env.sample to.env and edit the file

  • GITHUB_OAUTH_CLIENT_ID - value provided by Github (step #5)
  • GITHUB_OAUTH_CLIENT_SECRET - value provided by Github (step #5)

Step #7 - (optional) Enable DEBUG Environment (local development)

$# Set up the DEBUG environment$(Unix/Mac)exportFLASK_ENV=development$(Windows)setFLASK_ENV=development$(Powershell)$env:FLASK_ENV="development"
Enter fullscreen modeExit fullscreen mode

Step #8 - Start the project (HTTPS)

$flask run--cert=adhoc$$# Access the app in browser: HTTPS://127.0.0.1:5000/
Enter fullscreen modeExit fullscreen mode

At this point, the app should be up & running in the browser ready to successfully authenticate users via Github.


Flask Social Login - HOMEpage


Thanks for reading! For more resources, please access:

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
uithemes profile image
ui-themes
Modern UI Kits & Themes that deserve at least one tweet/
  • Location
    Hack Land
  • Joined

Nice feature! Thanks for sharing.

CollapseExpand
 
sm0ke profile image
Sm0ke
#Automation, my favorite programming language
  • Location
    Constanta, Romania
  • Education
    Hack Land
  • Work
    Founder @ AppSeed.us
  • Joined

🚀🚀

CollapseExpand
 
zoltanszogyenyi profile image
Zoltán Szőgyényi
Creator of Flowbite. Web dev. Open-source contributor. Community builder.
  • Location
    Timisoara, Romania
  • Education
    University
  • Work
    Web developer
  • Joined

Thanks for writing this :)

CollapseExpand
 
sm0ke profile image
Sm0ke
#Automation, my favorite programming language
  • Location
    Constanta, Romania
  • Education
    Hack Land
  • Work
    Founder @ AppSeed.us
  • Joined

🚀🚀

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

#Automation, my favorite programming language
  • Location
    Constanta, Romania
  • Education
    Hack Land
  • Work
    Founder @ AppSeed.us
  • Joined

More fromSm0ke

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp