Giter VIP home page Giter VIP logo

flask-multi-session's Introduction

Flask-Multi-Session build status build status

Flask-Multi-Session provides Mongo session storage for Flask apps. This module allows you to manage user sessions from different devices, thus you can logout user from all devices. Flask-Multi-Session supports python>=3.5.

Installation

Install Flask-Multi-Session from pip.

pip3 install flask-multi-session

Usage

from flask import Flask, session, redirect, url_for
import random

app = Flask(__name__)

@app.route('/')
def index():
  user_id = session.get('user_id')
  if user_id is not None:
    return """
      Hello. This is index page. Your login is %s.<\br>
      <a href="/logout">Logout</a> <a href="/logout_all_devices">Logout from all devices</a>
    """ % user_id
  else:
    return 'Hello. This is index page. Please <a href="/login">login</a>.'

@app.route('/login')
def login():
  session.login(random.randint(1, 10000))
  return redirect(url_for('index'))

@app.route('/logout')
def logout():
  session.logout()
  return redirect(url_for('index'))

@app.route('/logout_all_devices')
def logout_all_devices():
  session.logout_all_devices()
  return redirect(url_for('index'))

from flask_multisession import MongoSessionInterface
app.session_interface = MongoSessionInterface()


if __name__ == '__main__':
  app.run()

Important! Use "user_id" as a key for unique user id and add one to session after successful login.

Important! Added new method clear_user_sessions to Session class.

Example: session.clear_user_sessions() delete all sessions for current user, but you can specify user_id: session.clear_user_sessions(user_id=23) delete all sessions for user with id=23.

Set Mongo

If you have a remote Mongo you may specify one in MongoSessionInterface constructor.


mongo_config = dict(
  host='192.168.0.1',
  port=27017
)

from flask.ext.multisession import MongoSessionInterface
app.session_interface = MongoSessionInterface(**mongo_config)

flask-multi-session's People

Contributors

thinktalentnext avatar nbob avatar movermeyer avatar

Watchers

Ranjan Kumar Patel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.