-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
72 lines (55 loc) · 1.98 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from flask import Flask, redirect, url_for, render_template, flash
from flask_login import LoginManager, UserMixin, login_user, logout_user, current_user
from modules.oauth import OAuthSignIn
from flask_mongoengine import MongoEngine
from modules.mongo_helpers import get_mongo_db
mongodb = MongoEngine()
app = Flask(__name__)
# app.config['MONGODB_SETTINGS'] = {
# 'db': '<---YOUR_DB_NAME--->',
# 'host': 'mongodb://<---YOUR_DB_FULL URI--->'
# }
app.config['OAUTH_CREDENTIALS'] = config['OAUTH_CREDENTIALS']
db = MongoEngine(app)
app.config['SECRET_KEY'] = '<---YOUR_SECRET_FORM_KEY--->'
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
class User(UserMixin, db.Document):
meta = {'collection': 'user'}
email = db.StringField(max_length=30)
password = db.StringField()
@login_manager.user_loader
def load_user(user_id):
return User.objects(pk=user_id).first()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))
@app.route('/authorize/<provider>')
def oauth_authorize(provider):
if not current_user.is_anonymous:
return redirect(url_for('index'))
oauth = OAuthSignIn.get_provider(provider)
return oauth.authorize()
@app.route('/callback/<provider>')
def oauth_callback(provider):
if not current_user.is_anonymous:
return redirect(url_for('index'))
oauth = OAuthSignIn.get_provider(provider)
social_id, username, email = oauth.callback()
if social_id is None:
flash('Authentication failed.')
return redirect(url_for('index'))
user = User.objects(email=form.email.data).first()
if not user:
user = User(social_id=social_id, nickname=username, email=email)
db.session.add(user)
db.session.commit()
login_user(user, True)
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(host='127.0.0.1', port=4000, debug=True)