@xeplr/auth 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +47 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -126,10 +126,57 @@ async function start(config = {}) {
|
|
|
126
126
|
// connection AUTH_DB_CONNECTION_INFO_ENCRYPTED · db AUTH_DB_NAME · AUTH_JWT_SECRET
|
|
127
127
|
// AUTH_PORT · AUTH_ACTIVATION_BASE_URL · AUTH_ACCESS_TOKEN_TTL_MINUTES
|
|
128
128
|
// XEPLR_AUTH_MIGRATIONS · email vars · REDIS_*
|
|
129
|
+
/**
|
|
130
|
+
* REDIS HOLDS THE SESSIONS, so this service cannot authenticate anybody
|
|
131
|
+
* without it. Checked here, out loud, because the way this fails otherwise is
|
|
132
|
+
* genuinely cruel:
|
|
133
|
+
*
|
|
134
|
+
* • login SUCCEEDS — minting a token does not need Redis to work
|
|
135
|
+
* • the session write fails silently
|
|
136
|
+
* • every later request finds no live session and answers
|
|
137
|
+
* "Invalid or expired token"
|
|
138
|
+
*
|
|
139
|
+
* So a brand-new token is rejected as expired, and the message sends whoever
|
|
140
|
+
* is debugging it towards clocks, token lifetimes and JWT secrets — anywhere
|
|
141
|
+
* but the service that is not running. It cost two people an afternoon before
|
|
142
|
+
* this check existed.
|
|
143
|
+
*
|
|
144
|
+
* Refuses rather than warns. An auth service that cannot keep a session is not
|
|
145
|
+
* degraded, it is unable to do the one thing it is for, and starting anyway
|
|
146
|
+
* only moves the failure somewhere less obvious.
|
|
147
|
+
*/
|
|
148
|
+
async function checkSessionStore() {
|
|
149
|
+
var host = process.env.REDIS_HOST || 'localhost';
|
|
150
|
+
var port = process.env.REDIS_PORT || '6379';
|
|
151
|
+
var probe = '__xeplr_auth_startup_probe__';
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
var { cache } = require('@xeplr/utils');
|
|
155
|
+
await cache.set(probe, { ok: true }, 5);
|
|
156
|
+
var back = await cache.get(probe);
|
|
157
|
+
await cache.del(probe);
|
|
158
|
+
if (!back || !back.ok) throw new Error('the session store did not return what was written to it');
|
|
159
|
+
} catch (err) {
|
|
160
|
+
console.error('\n[xeplr-auth] Cannot reach the session store (Redis) at ' + host + ':' + port + '.');
|
|
161
|
+
console.error(' ' + (err && err.message ? err.message : err) + '\n');
|
|
162
|
+
console.error(' Sign-in sessions are kept there, so without it every login');
|
|
163
|
+
console.error(' succeeds and then every request is refused as "Invalid or');
|
|
164
|
+
console.error(' expired token" — which looks like a token problem and is not.\n');
|
|
165
|
+
console.error(' Start Redis, or point REDIS_HOST / REDIS_PORT at one:\n');
|
|
166
|
+
console.error(' macOS brew services start redis');
|
|
167
|
+
console.error(' Linux sudo systemctl start redis');
|
|
168
|
+
console.error(' Windows docker run -d -p 6379:6379 redis');
|
|
169
|
+
console.error(' (Redis has no native Windows build — Docker or WSL)\n');
|
|
170
|
+
process.exit(1);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
129
174
|
async function boot() {
|
|
130
175
|
var connName = 'auth';
|
|
131
176
|
var database = process.env.AUTH_DB_NAME || 'auth';
|
|
132
177
|
|
|
178
|
+
await checkSessionStore();
|
|
179
|
+
|
|
133
180
|
await resolveConfig(connName, authConnection());
|
|
134
181
|
|
|
135
182
|
// Self-migrate: auth's own .sql migrations (schema + base data), then the
|