Skip to content

Commit 09b19b9

Browse files
Session store knex (directus#5432)
* Rotate JPG image on upload directus#4206 * fixes directus#3949 width/height generated for gif and tif * API hooks for event added for auth.login directus#4079 * updated doc for api hooks for new auth.login event * Style tweaks * Update docs * Tweak docs some more * Spelling error * Allow non-required flags and pass to hook * SDK - Persistent login refresh fixes directus#4113 * Fixed directus#4145 SDK, Token Expired error * Spell check * Docs Spell check * Docs Spell check * Docs Spell check * update docs for sdk-js * To delete all expired session from db on login * corrected the condition for the delete * changed the from Date.now to new date . * Move it inline * Added Knex option for Session store Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
1 parent 7817f8f commit 09b19b9

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

api/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
"@keyv/redis": "^2.1.2",
131131
"connect-memcached": "^1.0.0",
132132
"connect-redis": "^5.2.0",
133+
"connect-session-knex": "^2.0.0",
133134
"ioredis": "^4.19.2",
134135
"keyv-memcache": "^1.0.1",
135136
"memcached": "^2.2.2",
@@ -169,4 +170,4 @@
169170
"ts-node-dev": "^1.0.0",
170171
"typescript": "^4.0.5"
171172
}
172-
}
173+
}

api/src/middleware/session.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import expressSession, { Store } from 'express-session';
22
import env from '../env';
33
import { getConfigFromEnv } from '../utils/get-config-from-env';
4-
4+
import database from '../database';
55
let store: Store | undefined = undefined;
66

77
if (env.SESSION_STORE === 'redis') {
@@ -17,4 +17,12 @@ if (env.SESSION_STORE === 'memcache') {
1717
store = new MemcachedStore(getConfigFromEnv('SESSION_MEMCACHE_'));
1818
}
1919

20+
if (env.SESSION_STORE === 'database') {
21+
const KnexSessionStore = require('connect-session-knex')(expressSession);
22+
store = new KnexSessionStore({
23+
knex: database,
24+
tablename: 'oauth_sessions', // optional. Defaults to 'sessions'
25+
});
26+
}
27+
2028
export const session = expressSession({ store, secret: env.SECRET as string, saveUninitialized: false, resave: false });

docs/reference/environment-variables.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ Alternatively, you can provide the individual connection parameters:
164164

165165
Sessions are only used in the oAuth authentication flow.
166166

167-
| Variable | Description | Default Value |
168-
| --------------- | ------------------------------------------------------------------------- | ------------- |
169-
| `SESSION_STORE` | Where to store the session data. Either `memory`, `redis`, or `memcache`. | `memory` |
167+
| Variable | Description | Default Value |
168+
| --------------- | ------------------------------------------------------------------------------------ | ------------- |
169+
| `SESSION_STORE` | Where to store the session data. Either `memory`, `redis`, `memcache` or `database`. | `memory` |
170170

171171
Based on the `SESSION_STORE` used, you must also provide the following configurations:
172172

@@ -194,6 +194,10 @@ Alternatively, you can provide the individual connection parameters:
194194
| ------------------ | ---------------------------------- | ------------- |
195195
| `SESSION_MEMCACHE` | Location of your memcache instance | --- |
196196

197+
### Database
198+
199+
No additional configuration required.
200+
197201
## File Storage
198202

199203
| Variable | Description | Default Value |

0 commit comments

Comments
 (0)