Skip to content

Commit

Permalink
added flask-login to sessions example
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jun 26, 2017
1 parent ece1623 commit a51a3ba
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 15 deletions.
1 change: 1 addition & 0 deletions example/requirements.txt
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
Expand Down
49 changes: 43 additions & 6 deletions example/sessions.py
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)
79 changes: 70 additions & 9 deletions example/templates/sessions.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,48 @@ <h1>Flask-SocketIO Sessions example</h1>

<div class="pure-form">
<h2>HTTP</h2>
<p>Current session: <b><span id="http-session"></span></b></p>
<p>Set session to: <input type=text id='http-session-set'> <button class="pure-button pure-button-primary" id="submit-http">Submit</button></p>

<div class="pure-g">
<div class="pure-u-1-6"><h3>Session</h3></div>
<div class="pure-u-5-6">
<p>Current session: <b><span id="http-session"></span></b></p>
<p>
<input type=text id='http-session-set' placeholder="Set session to...">
<button class="pure-button pure-button-primary" id="submit-http">Submit</button>
</p>
</div>
<div class="pure-u-1-6"><h3>User</h3></div>
<div class="pure-u-5-6">
<p>Current user: <b><span id="http-user"></span></b></p>
<p>
<input type=text id='http-user-set' placeholder="User to login...">
<button class="pure-button pure-button-primary" id="login-http">Login</button>
<button class="pure-button pure-button-primary" id="logout-http">Logout</button>
</p>
</div>
</div>
<hr>
<h2>Socket.IO</h2>
<p><button class="pure-button pure-button-primary" id="connect">Connect</button></p>
<div id="socketio" style="display: none">
<p>Current session: <b><span id="socketio-session"></span></b></p>
<p>Set session to: <input type=text id='socketio-session-set'> <button class="pure-button pure-button-primary" id="submit-socketio">Submit</button></p>
<div class="pure-g">
<div class="pure-u-1-6"><h3>Session</h3></div>
<div class="pure-u-5-6">
<p>Current session: <b><span id="socketio-session"></span></b></p>
<p>
<input type=text id='socketio-session-set' placeholder="Set session to...">
<button class="pure-button pure-button-primary" id="submit-socketio">Submit</button>
</p>
</div>
<div class="pure-u-1-6"><h3>User</h3></div>
<div class="pure-u-5-6">
<p>Current user: <b><span id="socketio-user"></span></b></p>
<p>
<input type=text id='socketio-user-set' placeholder="User to login...">
<button class="pure-button pure-button-primary" id="login-socketio">Login</button>
<button class="pure-button pure-button-primary" id="logout-socketio">Logout</button>
</p>
</div>
</div>
</div>
</div>
</div>
Expand All @@ -35,6 +69,7 @@ <h2>Socket.IO</h2>
url: '/session',
success: function(data) {
$('#http-session').text(data['session']);
$('#http-user').text(data['user']);
}
});
}, 1000);
Expand All @@ -47,15 +82,33 @@ <h2>Socket.IO</h2>
contentType: 'application/json'
});
});
$('#login-http').click(function() {
$.ajax({
type: 'POST',
url: '/session',
data: JSON.stringify({user: $('#http-user-set').val()}),
contentType: 'application/json'
});
});
$('#logout-http').click(function() {
$.ajax({
type: 'POST',
url: '/session',
data: JSON.stringify({user: null}),
contentType: 'application/json'
});
});


// Socket.IO handlers
var socket = null;

$('#connect').click(function() {
if (!socket) {
socket = io.connect(location.href);
socket.on('refresh-session', function(value) {
$('#socketio-session').text(value);
socket.on('refresh-session', function(data) {
$('#socketio-session').text(data['session']);
$('#socketio-user').text(data['user']);
});

$('#connect').text('Disconnect');
Expand All @@ -64,8 +117,10 @@ <h2>Socket.IO</h2>
else {
socket.disconnect();
socket = null;
$('#connect').text('Connect');
$('#socketio').hide();
$('#connect').text('Connect');
$('#socketio-session').text('');
$('#socketio-user').text('');
}
});

Expand All @@ -75,7 +130,13 @@ <h2>Socket.IO</h2>
}, 1000);

$('#submit-socketio').click(function() {
socket.emit('set-session', $('#socketio-session-set').val());
socket.emit('set-session', {session: $('#socketio-session-set').val()});
});
$('#login-socketio').click(function() {
socket.emit('set-session', {user: $('#socketio-user-set').val()});
});
$('#logout-socketio').click(function() {
socket.emit('set-session', {user: null});
});
</script>
</body>
Expand Down

0 comments on commit a51a3ba

Please sign in to comment.