Skip to content

Instantly share code, notes, and snippets.

@CT83
Created February 10, 2018 00:23
Show Gist options
  • Save CT83/7881e27487fbcc31cc6c319d39212de3 to your computer and use it in GitHub Desktop.
Save CT83/7881e27487fbcc31cc6c319d39212de3 to your computer and use it in GitHub Desktop.
import os
from flask import Flask, redirect, url_for
from flask_dance.consumer import oauth_authorized
from flask_dance.consumer.backend.sqla import OAuthConsumerMixin, SQLAlchemyBackend
from flask_dance.contrib.github import make_github_blueprint, github
from flask_dance.contrib.twitter import make_twitter_blueprint, twitter
from flask_login import UserMixin, current_user, LoginManager, login_required, login_user, logout_user
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm.exc import NoResultFound
from CONSTANT import ON_HEROKU
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
if ON_HEROKU:
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
else:
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://postgres:cybertech83@localhost/postgres"
app = Flask(__name__)
app.config['SECRET_KEY'] = 'thisissupposedtobeasecret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////mnt/c/Users/antho/Documents/oauth/login.db'
twitter_blueprint = make_twitter_blueprint(api_key='XXXXXXXXXXXX',
api_secret='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
github_blueprint = make_github_blueprint(client_id='XXXXXXXXXXXX',
client_secret='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
app.register_blueprint(twitter_blueprint, url_prefix='/twitter_login')
app.register_blueprint(github_blueprint, url_prefix='/github_login')
db = SQLAlchemy(app)
login_manager = LoginManager(app)
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(250), unique=True)
class OAuth(OAuthConsumerMixin, db.Model):
user_id = db.Column(db.Integer, db.ForeignKey(User.id))
user = db.relationship(User)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
twitter_blueprint.backend = SQLAlchemyBackend(OAuth, db.session, user=current_user)
@app.route('/twitter')
def twitter_login():
if not twitter.authorized:
return redirect(url_for('twitter.login'))
account_info = twitter.get('account/settings.json')
account_info_json = account_info.json()
return '<h1>Your Twitter name is @{}'.format(account_info_json['screen_name'])
@oauth_authorized.connect_via(twitter_blueprint)
def twitter_logged_in(blueprint, token):
account_info = blueprint.session.get('account/settings.json')
if account_info.ok:
account_info_json = account_info.json()
username = account_info_json['screen_name']
query = User.query.filter_by(username=username)
try:
user = query.one()
except NoResultFound:
user = User(username=username)
db.session.add(user)
db.session.commit()
login_user(user)
@app.route('/github')
def github_login():
if not github.authorized:
return redirect(url_for('github.login'))
account_info = github.get('/user')
if account_info.ok:
account_info_json = account_info.json()
return '<h1>Your Github name is {}'.format(account_info_json['login'])
return '<h1>Request failed!</h1>'
@app.route('/')
@login_required
def index():
return '<h1>You are logged in as {}</h1>'.format(current_user.username)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('index'))
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
# manager.run()
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment