AZU Framework

Integrate Session with the AZU web frameworkarrow-up-right for type-safe session management in your Crystal applications.

Overview

spinner

Installation

Add both dependencies to your shard.yml:

dependencies:
  azu:
    github: azutoolkit/azu
  session:
    github: azutoolkit/session

Quick Start

1. Define Your Session Data

# src/sessions/user_session.cr
struct UserSession
  include Session::SessionData

  property user_id : Int64?
  property username : String?
  property email : String?
  property roles : Array(String) = [] of String
  property logged_in_at : Time?

  def authenticated? : Bool
    !user_id.nil?
  end

  def admin? : Bool
    roles.includes?("admin")
  end
end

2. Configure Session

3. Create the Session Handler

4. Register the Handler

5. Use Sessions in Endpoints

Helper Module

Create a helper module to simplify session access in endpoints:

Use the helper in your endpoints:

Logout Endpoint

Flash Messages in Templates

Access flash messages in your views:

Production Configuration

With Redis and Clustering

For serverless or edge deployments:

Testing

Test Helper

Endpoint Tests

Graceful Shutdown

Handle server shutdown to properly close cluster connections:

Complete Example

See the azu-session-examplearrow-up-right repository for a complete working example with:

  • User authentication (login/logout)

  • Protected routes

  • Flash messages

  • Admin authorization

  • Redis session storage

  • Test coverage

Last updated

Was this helpful?