Swiftpack.co -  brokenhandsio/vapor-dynamodb-sessions as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
brokenhandsio/vapor-dynamodb-sessions
A DynamoDB integration for Vapor's Sessions
.package(url: "https://github.com/brokenhandsio/vapor-dynamodb-sessions.git", from: "1.1.0")

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.

GitHub

link
Stars: 1
Last commit: 3 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

1.1.0
3 weeks ago

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

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics