VaporDynamoDBSessions

1.1.0

A DynamoDB integration for Vapor's Sessions
brokenhandsio/vapor-dynamodb-sessions

What's New

1.1.0

2021-08-26T10:01:14Z

This release adds support for expiring sessions and fixes the session ID generation

Vapor DynamoDB Sessions

Language Language Build Status Code Coverage MIT License

A simple library to use DynamoDB with Soto to back Vapor's sessions.

Installation

Add the library in your dependencies array in Package.swift:

dependencies: [
    // ...,
    package(name: "VaporDynamoDBSessions", url: "https://github.com/brokenhandsio/vapor-dynamodb-sessions.git", from: "1.0.0"),
],

Also ensure you add it as a dependency to your target:

targets: [
    .target(name: "App", dependencies: [
        .product(name: "Vapor", package: "vapor"), 
        // ..., 
        .product(name: "VaporDynamoDBSessions", package: "VaporDynamoDBSessions")
    ]),
    // ...
]

Usage

To start, you must configure the DynamoDBSessionsProvider with the AWSClient and a table name. In configure.swift set the provider on the application:

app.dynamoDBSessions.provider = DynamoDBSessionsProvider(client: app.aws.client, tableName: tableName)

The DynamoDBSessionsProvider also takes an optional AWS region and endpoint if you need to configure these. To learn how to configure the AWSClient see the Soto Documentation.

Next, tell Vapor to use DynamoDB for sessions:

app.sessions.use(.dynamodb)
app.middleware.use(app.sessions.middleware)

Note: You must set DynamoDB as the SessionDriver before adding the SessionsMiddleware.

Database Requirements

VaporDynamoDBSessions will work with its own table or as part of an application using a single-table design. The only requirements for the library to work is that the table must have a partition key named pk and a sort key named sk.

Session Expiry

VaporDynamoDBSessions supports adding an expiry date to sessions. Any session that has expired will be discarded by the driver. To configure this, pass a session duration when configuring the provider:

// 30 days
let sessionLength: TimeInterval = 60 * 60 * 24 * 30
app.dynamoDBSessions.provider = DynamoDBSessionsProvider(client: app.aws.client, tableName: tableName, region: .useast1, endpoint: dynamoDBEndpoint, sessionLength: sessionLength)

This will add a field to the session �record with the key expiryDate as a number in epoch time. This allows you to use DynamoDB's TTL feature to auto-delete expired session data and clean up the database.

Description

  • Swift Tools 5.2.0
View More Packages from this Author

Dependencies

Last updated: Mon Oct 21 2024 05:25:41 GMT-0900 (Hawaii-Aleutian Daylight Time)