Masonite Documentation
v4.0
v4.0
  • Introduction and Installation
  • Prologue
    • Creating A Blog Tutorial
    • Release Cycle
    • Contributing Guide
    • How To Contribute
  • The Basics
    • Routing
    • Controllers
    • Middleware
    • Response
    • Request
    • Static Files
    • Views
    • Environments
    • Configuration
    • Error Handling
  • Features
    • API Development
    • Authentication
    • Authorization
    • Broadcasting
    • Caching
    • Compiling Assets
    • Commands
    • CSRF Protection
    • Events
    • Facades
    • Filesystem and Uploading
    • Hash ID's
    • Helpers
    • Mail
    • Notifications
    • Package Development
    • Queues and Jobs
    • Rate Limiting
    • Sessions
    • Task Scheduling
    • Tinker Shell (REPL)
    • Validation
  • Architecture
    • Service Providers
    • Service Container
  • Security
    • CORS
    • Hashing
  • Masonite ORM
    • To Masonite ORM Docs
  • Testing
    • Getting Started
    • HTTP Tests
    • Database Tests
    • Commands Tests
    • Console Tests
    • Mocking
    • Extending
  • Official Packages
    • Masonite Debugbar
  • How-to Guides
    • Handling AJAX requests with expired authentication
    • Build Email Verification from Scratch With Masonite Framework and JSON Web Tokens
    • Deploying a Masonite Application to Heroku
    • How To Deploy Masonite to PythonAnywhere
    • How-To: Use RabbitMQ with Masonite 2.0 queues
    • How To Use The Repository Pattern with Masonite
    • Making Masonite and Laravel Mix work together
  • What's New
    • Masonite 1.3
    • Masonite 1.4
    • Masonite 1.5
    • Masonite 1.6
    • Masonite 2.0
    • Masonite 2.1
    • Masonite 2.2
    • Masonite 2.3
    • Masonite 3.0
  • Upgrade Guide
    • Masonite 1.3 to 1.4
    • Masonite 1.4 to 1.5
    • Masonite 1.5 to 1.6
    • Masonite 1.6 to 2.0
    • Masonite 2.0 to 2.1
    • Masonite 2.1 to 2.2
    • Masonite 2.2 to 2.3
    • Masonite 2.3 to 3.0
    • Masonite 3.0 to 4.0
Powered by GitBook
On this page
  • Configuration
  • Cookie
  • Redis
  • Saving Session Data
  • Flashing Data
  • Retrieving Session Data
  • Checking For Existence
  • Deleting Session Data
  • Resetting the Session
Edit on GitHub
Export as PDF
  1. Features

Sessions

PreviousRate LimitingNextTask Scheduling

Last updated 2 years ago

Masonite comes with a simple way to store sessions. Masonite supports the following session drivers: and .

Configuration

Session configuration is located at config/session.py file. In this file, you can configure which driver to use.

Masonite is configured to use the Cookie session driver by default, named cookie.

config/session.py
DRIVERS = {
    "default": "cookie",
    "cookie": {},
    "redis": {
        "host": "127.0.0.1",
        "port": 6379,
        "password": "",
        "options": {"db": 1},  # redis module driver specific options
        "timeout": 60 * 60,
        "namespace": "masonite4",
    },
}

Cookie

Cookie driver will store all session data in the users cookies. It can be used as is.

Redis

Redis driver is requiring the redis python package, that you can install with:

pip install redis

Then you should define Redis as default driver and configure it with your Redis server parameters:

DRIVERS = {
    "default": "redis",
    "redis": {
        "host": "127.0.0.1",
        "port": 6379,
        "password": "",
        "options": {"db": 1},  # redis module driver specific options
        "timeout": 60 * 60,
        "namespace": "masonite4",
    },
}

Finally ensure that the Redis server is running and you're ready to start using sessions.

Saving Session Data

To save session data you can simply "set" data into the session:

from masonite.sessions import Session

def store(self, session: Session):
  data = session.set('key', 'value')

Flashing Data

Flash data is data specific to the next request. This is useful for data such as error messages or alerts:

from masonite.sessions import Session

def store(self, session: Session):
  data = session.flash('key', 'value')

Retrieving Session Data

To get back the session data you set you can simply using the "get" method:

from masonite.sessions import Session

def store(self, session: Session):
  data = session.get('key')

Checking For Existence

You can check if a session has a specific key:

from masonite.sessions import Session

def store(self, session: Session):
  if session.has('key'):
    pass

Deleting Session Data

You can also delete a key from the session

from masonite.sessions import Session

def store(self, session: Session):
  session.delete('key')

Resetting the Session

You can reset all data in a session:

session.flush()
Cookie
Redis