|
1 |
| -# SessionMIddleware |
| 1 | +# SessionMiddleware |
| 2 | + |
| 3 | +Session Middleware for Slimane |
| 4 | + |
| 5 | + |
| 6 | +## Usage |
| 7 | +```swift |
| 8 | +import Slimane |
| 9 | +import SessionMiddleware |
| 10 | + |
| 11 | +let app = Slimane() |
| 12 | + |
| 13 | +// SessionConfig |
| 14 | +let sesConf = SessionConfig( |
| 15 | + secret: "my-secret-value", |
| 16 | + expires: 180, |
| 17 | + HTTPOnly: true |
| 18 | +) |
| 19 | + |
| 20 | +// Enable to use session in Slimane |
| 21 | +app.use(SessionMiddleware(conf: sesConf)) |
| 22 | +``` |
| 23 | + |
| 24 | +## Apis |
| 25 | + |
| 26 | +### SessionMiddleware |
| 27 | + |
| 28 | +Middleware for session handling in Slimane |
| 29 | + |
| 30 | +```swift |
| 31 | +SessionMiddleware(conf: SessionConfig) |
| 32 | +``` |
| 33 | + |
| 34 | +### SessionConfig |
| 35 | + |
| 36 | +Session Configuration Struct. |
| 37 | + |
| 38 | +#### Members |
| 39 | +* store: SessionStoreType |
| 40 | + - Default store is the SessionMemoryStore |
| 41 | +* keyName: String |
| 42 | +* secret: String |
| 43 | + - Your application secret value |
| 44 | +* expires: Int? |
| 45 | + - Sec for cookie/stored data ttl |
| 46 | +* HTTPOnly: Bool |
| 47 | +* secure: Bool |
| 48 | +* maxAge: Int? |
| 49 | +* domain: String? |
| 50 | +* path: String? |
| 51 | + |
| 52 | + |
| 53 | +### SessionStoreType |
| 54 | + |
| 55 | +```swift |
| 56 | +public protocol SessionStoreType { |
| 57 | + func destroy(sessionId: String) |
| 58 | + func load(sessionId: String, completion: (SessionResult<[String: AnyObject?]>) -> Void) |
| 59 | + func store(sessionId: String, values: [String: AnyObject?], expires: Int?, completion: () -> Void) |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +### SessionMemoryStore |
| 64 | + |
| 65 | +SessionMemoryStore is default store for SessionMiddleware. |
| 66 | +And this is not recommended to use under production environment. |
| 67 | + |
| 68 | +### SerializerType |
| 69 | + |
| 70 | +Protocol for Serializer |
| 71 | + |
| 72 | +```swift |
| 73 | +public protocol SerializerType { |
| 74 | + func serialize(src: [String: AnyObject]) throws -> String |
| 75 | + func deserialize(src: String) throws -> [String: AnyObject] |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### JsonSerializer |
| 80 | + |
| 81 | +Serialize/deserialize the value with JSON format |
| 82 | + |
| 83 | +```swift |
| 84 | +let serializer = JsonSerializer() |
| 85 | + |
| 86 | +let serialized = serializer.serializer(["foo": "bar"]) |
| 87 | + |
| 88 | +print(serializer.deserialize(serialized)) |
| 89 | +``` |
| 90 | + |
| 91 | +## Create your own SessionStore |
| 92 | + |
| 93 | +Easy to make an own SessionStore with confirming SessionStoreType. Here is an easy FileSessionStore Example. |
| 94 | + |
| 95 | +```swift |
| 96 | +import SessionMiddleware |
| 97 | +import Suv |
| 98 | + |
| 99 | +struct FileSessionStore: SessionStoreType { |
| 100 | + func destroy(sessionId: String) { |
| 101 | + FS.unlink("/path/to/\(sessionId).json") |
| 102 | + } |
| 103 | + |
| 104 | + func load(sessionId: String, completion: (SessionResult<[String: AnyObject?]>) -> Void) { |
| 105 | + let serializer = JsonSerializer() |
| 106 | + FS.readFile("/path/to/\(sessionId).json") { |
| 107 | + if case .Data(let buf) = $0 { |
| 108 | + let dictionary = serializer.deserialize(buf.toString()!) |
| 109 | + completion(.Success(dictionary)) |
| 110 | + } |
| 111 | + |
| 112 | + if case .Error(let error) = $0 { |
| 113 | + completion(.Error(error)) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + func store(sessionId: String, values: [String: AnyObject?], expires: Int?, completion: () -> Void) { |
| 119 | + let serializer = JsonSerializer() |
| 120 | + // Overwrite |
| 121 | + FS.writeFile("/path/to/\(sessionId).json", Buffer(try! serializer(values))) { _ in |
| 122 | + completion() |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +## List of available SessionStores |
| 129 | +* SessionMemoryStore(Including this package) |
| 130 | +* [SessionRedisStore](https://github.com/slimane-swift/SessionRedisStore) |
| 131 | + |
| 132 | + |
| 133 | +## Package.swift |
| 134 | +```swift |
| 135 | +import PackageDescription |
| 136 | + |
| 137 | +let package = Package( |
| 138 | + name: "SlimaneApp", |
| 139 | + dependencies: [ |
| 140 | + .Package(url: "https://github.com/slimane-swift/SessionMiddleware.git", majorVersion: 0, minor: 1) |
| 141 | + ] |
| 142 | +) |
| 143 | +``` |
| 144 | +## Licence |
| 145 | + |
| 146 | +SessionMiddleware is released under the MIT license. See LICENSE for details. |
0 commit comments