-
-
Notifications
You must be signed in to change notification settings - Fork 893
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added flask-login to sessions example
- Loading branch information
1 parent
ece1623
commit a51a3ba
Showing
3 changed files
with
114 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
Flask==0.12.2 | ||
Flask-Login==0.4.0 | ||
Flask-Session==0.3.1 | ||
Flask_SocketIO | ||
itsdangerous==0.24 | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,70 @@ | ||
from flask import Flask, render_template, session, request, jsonify | ||
from flask_login import LoginManager, UserMixin, current_user, login_user, \ | ||
logout_user | ||
from flask_session import Session | ||
from flask_socketio import SocketIO, emit | ||
|
||
app = Flask(__name__) | ||
app.config['SECRET_KEY'] = 'top-secret!' | ||
app.config['SESSION_TYPE'] = 'filesystem' | ||
login = LoginManager(app) | ||
Session(app) | ||
socketio = SocketIO(app, manage_session=False) | ||
|
||
|
||
class User(UserMixin, object): | ||
def __init__(self, id=None): | ||
self.id = id | ||
|
||
|
||
@login.user_loader | ||
def load_user(id): | ||
return User(id) | ||
|
||
|
||
@app.route('/') | ||
def index(): | ||
session['value'] = '' | ||
return render_template('sessions.html') | ||
|
||
|
||
@app.route('/session', methods=['GET', 'POST']) | ||
def session_access(): | ||
if request.method == 'GET': | ||
return jsonify({'session': session['value']}) | ||
session['value'] = request.get_json().get('session') | ||
return jsonify({ | ||
'session': session.get('value', ''), | ||
'user': current_user.id | ||
if current_user.is_authenticated else 'anonymous' | ||
}) | ||
data = request.get_json() | ||
if 'session' in data: | ||
session['value'] = data['session'] | ||
elif 'user' in data: | ||
if data['user']: | ||
login_user(User(data['user'])) | ||
else: | ||
logout_user() | ||
return '', 204 | ||
|
||
|
||
@socketio.on('get-session') | ||
def get_session(): | ||
emit('refresh-session', session['value']) | ||
emit('refresh-session', { | ||
'session': session.get('value', ''), | ||
'user': current_user.id | ||
if current_user.is_authenticated else 'anonymous' | ||
}) | ||
|
||
|
||
@socketio.on('set-session') | ||
def set_session(value): | ||
session['value'] = value | ||
def set_session(data): | ||
if 'session' in data: | ||
session['value'] = data['session'] | ||
elif 'user' in data: | ||
if data['user'] is not None: | ||
login_user(User(data['user'])) | ||
else: | ||
logout_user() | ||
|
||
|
||
if __name__ == '__main__': | ||
socketio.run(app) |
This file contains 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