@xeplr/auth 1.1.0 → 1.1.2

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.
Files changed (2) hide show
  1. package/index.js +81 -3
  2. package/package.json +2 -2
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
@@ -153,6 +200,13 @@ async function boot() {
153
200
 
154
201
  // Configure email (via @xeplr/utils, the engine auth uses) + activation links.
155
202
  require('@xeplr/utils').configureFromEnv();
203
+
204
+ // Whether mail can actually go out — asked, not assumed from a variable
205
+ // being set. Reported in the banner and NEVER fatal: sign-in works without
206
+ // email; only the messages that carry links (activation, invite, password
207
+ // reset) cannot be sent, and the banner says so rather than the first
208
+ // person to register finding out.
209
+ var email = await emailStatus();
156
210
  authService.configureActivation({
157
211
  // Fully qualified, one per link — the token is the only thing appended.
158
212
  // AUTH_ACTIVATION_BASE_URL still works as an origin; see configureActivation.
@@ -172,13 +226,37 @@ async function boot() {
172
226
  }
173
227
  });
174
228
 
175
- banner();
229
+ banner(email);
230
+ if (!email.ok) warnNoEmail(email);
176
231
  return server;
177
232
  }
178
233
 
234
+ async function emailStatus() {
235
+ var utils = require('@xeplr/utils');
236
+ if (typeof utils.checkEmail !== 'function') {
237
+ return { ok: true, unknown: true, detail: 'not checked — @xeplr/utils is too old to check' };
238
+ }
239
+ return utils.checkEmail({ timeoutMs: 5000 });
240
+ }
241
+
242
+ function warnNoEmail(email) {
243
+ console.warn('[xeplr-auth] EMAIL IS NOT WORKING — ' + String(email.detail).replace(/\.$/, '') + '.');
244
+ console.warn(' Sign-in keeps working. Registration, invites and password');
245
+ console.warn(' resets cannot send their links until this is fixed: set');
246
+ console.warn(' EMAIL_PROVIDER (smtp, brevo, aws or azure) and its settings.\n');
247
+ }
248
+
249
+ function describeEmail(email) {
250
+ if (!email) return process.env.EMAIL_PROVIDER || '(not configured)';
251
+ if (email.unknown) return '? ' + email.detail;
252
+ return email.ok
253
+ ? '✓ ' + email.provider + ' — ' + email.detail
254
+ : '✗ NOT WORKING — ' + email.detail;
255
+ }
256
+
179
257
  // Startup summary — the EFFECTIVE config (defaults resolved) so you can see what
180
258
  // the service is actually running with. Secrets are masked, never printed.
181
- function banner() {
259
+ function banner(email) {
182
260
  var set = function (v) { return v ? '✓ set' : '✗ MISSING'; };
183
261
  var rows = [
184
262
  ['port', process.env.AUTH_PORT || '19001'],
@@ -191,7 +269,7 @@ function banner() {
191
269
  ['slide tolerance', (process.env.AUTH_ACCESS_TOKEN_TOLERANCE_SECONDS || '0') + 's'],
192
270
  ['max sessions/user', process.env.AUTH_MAX_SESSIONS_PER_USER || '5'],
193
271
  ['redis', (process.env.REDIS_HOST || 'localhost') + ':' + (process.env.REDIS_PORT || '6379')],
194
- ['email', process.env.EMAIL_PROVIDER || '(not configured)'],
272
+ ['email', describeEmail(email)],
195
273
  // Names the VARIABLE it resolved through, not just "set" — with a shared
196
274
  // default and a per-service override, "which server am I on" is otherwise
197
275
  // a guess.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xeplr/auth",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Pluggable authentication service: register, login, forgot/reset password, RBAC, multi-tenancy",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -44,7 +44,7 @@
44
44
  "multer": "^2.3.0",
45
45
  "@xeplr/base-apis": "^2.0.0",
46
46
  "@xeplr/db": "^1.0.0",
47
- "@xeplr/utils": "^1.0.0"
47
+ "@xeplr/utils": "^1.0.8"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "express": "^4.0.0 || ^5.0.0"