Last active
June 2, 2022 13:13
-
-
Save twyle/d7103293391033c47c020f2f45ef20e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""This module contains initialization code for the api package.""" | |
from flask import Flask, redirect, url_for | |
from flask_dance.contrib.github import github | |
from api.helpers import set_flask_environment | |
from .blueprints.auth.views import auth | |
from .blueprints.default.views import default | |
from .blueprints.extensions import db | |
from .blueprints.auth.models import User | |
from .extensions import login_manager | |
app = Flask(__name__) | |
set_flask_environment(app=app) | |
app.register_blueprint(default) | |
app.register_blueprint(auth, url_prefix='/login') | |
db.init_app(app) | |
login_manager.init_app(app) | |
with app.app_context(): | |
db.create_all() | |
@login_manager.user_loader | |
def load_user(user_id: int) -> User: | |
"""Load the user with the given id.""" | |
return User.query.get(user_id) | |
@app.route('/github') | |
def login(): | |
"""Log in a registered or authenticated user.""" | |
if not github.authorized: | |
return redirect(url_for('github.login')) | |
res = github.get('/user') | |
return f"You are logged in as {res.json()['login']} on GitHub." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment