Protocols
The following protocols are available globally.
-
A
TypeSafeMiddleware
for managing user sessions. The user defines a final class with the fields they wish to use within the session. This class can then save or destroy itself from a staticStore
, which is keyed by asessionId
. The sessionId can be extracted from the session cookie to initialise an instance of the user’s class with the session data. If no store is defined, the session will default to an in-memory store.Usage Example:
In this example, a class conforming to the TypeSafeSession protocol is defined containing an optional “name” field. Then a route on “/session” is set up that stores a received name into the session.
final class MySession: TypeSafeSession { var name: String? let sessionId: String init(sessionId: String) { self.sessionId = sessionId } static var store: Store? static let sessionCookie = SessionCookie(name: "session-cookie", secret: "abc123") } router.post("/session") { (session: MySession, name: String, respondWith: (String?, RequestError?) -> Void) in session.name = name try? session.save() respondWith(session.name, nil) }
Note: When using multiple TypeSafeSession classes together, if the cookie names are the same, the cookie secret must also be the same. Otherwise the sessions will conflict and overwrite each others cookies. (Different cookie names can use different secrets).
See moreDeclaration
Swift
public protocol TypeSafeSession : TypeSafeMiddleware, Decodable, Encodable