Configure sessions and store them in databases

This commit is contained in:
Elara 2020-11-15 15:43:44 -08:00
parent 5d8cf17e22
commit c32880ee3e
5 changed files with 71 additions and 3 deletions

3
.gitignore vendored
View File

@ -6,4 +6,5 @@ xcuserdata
DerivedData/
.DS_Store
file:*
Resources/config.json
Resources/config.json
Resources/db.sqlite

View File

@ -28,6 +28,33 @@
"version": "4.2.1"
}
},
{
"package": "fluent",
"repositoryURL": "https://github.com/vapor/fluent.git",
"state": {
"branch": null,
"revision": "e681c93df3201a2d8ceef15e8a9a0634578df233",
"version": "4.0.0"
}
},
{
"package": "fluent-kit",
"repositoryURL": "https://github.com/vapor/fluent-kit.git",
"state": {
"branch": null,
"revision": "31d96b547cc1f869f2885d932a8a9a7ae2103fc6",
"version": "1.10.0"
}
},
{
"package": "fluent-sqlite-driver",
"repositoryURL": "https://github.com/vapor/fluent-sqlite-driver.git",
"state": {
"branch": null,
"revision": "6f29f6f182c812075f09c7575c18ac5535c26824",
"version": "4.0.1"
}
},
{
"package": "leaf",
"repositoryURL": "https://github.com/vapor/leaf",
@ -64,6 +91,33 @@
"version": "4.2.0"
}
},
{
"package": "sql-kit",
"repositoryURL": "https://github.com/vapor/sql-kit.git",
"state": {
"branch": null,
"revision": "ea9928b7f4a801b175a00b982034d9c54ecb6167",
"version": "3.7.0"
}
},
{
"package": "sqlite-kit",
"repositoryURL": "https://github.com/vapor/sqlite-kit.git",
"state": {
"branch": null,
"revision": "2ec279b9c845cec254646834b66338551a024561",
"version": "4.0.2"
}
},
{
"package": "sqlite-nio",
"repositoryURL": "https://github.com/vapor/sqlite-nio.git",
"state": {
"branch": null,
"revision": "6481dd0b01112d082dd7eb362782126e81964138",
"version": "1.1.0"
}
},
{
"package": "swift-backtrace",
"repositoryURL": "https://github.com/swift-server/swift-backtrace.git",

View File

@ -14,7 +14,9 @@ let package = Package(
.package(url: "https://github.com/vapor/leaf-kit", .exact("1.0.0-tau.1.1")),
// Leaf Error Middleware for custom error pages
.package(name: "LeafErrorMiddleware", url: "https://github.com/brokenhandsio/leaf-error-middleware.git", from: "2.0.0-beta"),
.package(url: "https://github.com/apple/swift-crypto.git", from: "1.1.2")
.package(url: "https://github.com/apple/swift-crypto.git", from: "1.1.2"),
.package(url: "https://github.com/vapor/fluent.git", from: "4.0.0"),
.package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.0.0")
],
targets: [
.target(
@ -23,6 +25,8 @@ let package = Package(
.product(name: "Vapor", package: "vapor"),
.product(name: "Leaf", package: "leaf"),
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "Fluent", package: "fluent"),
.product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver"),
"LeafErrorMiddleware"
],
swiftSettings: [

BIN
Resources/db.sqlite Normal file

Binary file not shown.

View File

@ -1,6 +1,8 @@
import Fluent
import Vapor
import Leaf
import LeafErrorMiddleware
import FluentSQLiteDriver
// configures your application
public func configure(_ app: Application) throws {
@ -8,8 +10,15 @@ public func configure(_ app: Application) throws {
// Serve files from /Public
app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
app.sessions.use(.fluent)
app.sessions.configuration.cookieName = "statusboard-session"
app.sessions.configuration.cookieFactory = { sessionID in
.init(string: sessionID.string, expires: Date(timeIntervalSinceNow: 60*60*24*365), isSecure: true, isHTTPOnly: true, sameSite: HTTPCookies.SameSitePolicy.none)
}
app.middleware.use(app.sessions.middleware)
app.sessions.configuration.cookieName = "statusboard_session"
app.databases.use(.sqlite(.file("\(app.directory.resourcesDirectory)/db.sqlite")), as: .sqlite)
app.migrations.add(SessionRecord.migration)
// Configure Leaf
LeafOption.caching = app.environment.isRelease ? .default : .bypass