najm-auth 2.0.12 → 2.0.13

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/README.md CHANGED
@@ -746,6 +746,12 @@ const options = {
746
746
  // Verify the password without minting access/refresh tokens.
747
747
  const user = await authService.verifyCredentials({ identifier, password });
748
748
 
749
+ // Or narrowly accept only an unverified pending account with one exact role.
750
+ const pendingSponsor = await authService.verifyPendingCredentials(
751
+ { identifier, password },
752
+ 'sponsor',
753
+ );
754
+
749
755
  if (await appRequiresPasswordSetup(user.id)) {
750
756
  // Revokes normal sessions and writes only an HttpOnly, SameSite=Strict,
751
757
  // browser-session cookie. The database stores only its SHA-256 hash.
package/dist/index.d.ts CHANGED
@@ -1272,6 +1272,13 @@ declare class AuthService {
1272
1272
  * this before issuing a purpose-bound CredentialSetupService session.
1273
1273
  */
1274
1274
  verifyCredentials(body: LoginDto): Promise<SanitizedUser>;
1275
+ /**
1276
+ * Verify a pending, unverified account for one exact application role.
1277
+ * This deliberately does not establish a normal auth session. Applications
1278
+ * should exchange the result for a short-lived, purpose-bound setup session.
1279
+ */
1280
+ verifyPendingCredentials(body: LoginDto, expectedRole: string): Promise<SanitizedUser>;
1281
+ private verifyCredentialsForPolicy;
1275
1282
  /** Establish a complete normal auth session for an already verified user. */
1276
1283
  establishSession(user: SanitizedUser): Promise<TokenPair & {
1277
1284
  user: SanitizedUser;
package/dist/index.js CHANGED
@@ -2323,6 +2323,23 @@ var AuthService = class AuthService2 {
2323
2323
  * this before issuing a purpose-bound CredentialSetupService session.
2324
2324
  */
2325
2325
  async verifyCredentials(body) {
2326
+ return this.verifyCredentialsForPolicy(body, { kind: "active" });
2327
+ }
2328
+ /**
2329
+ * Verify a pending, unverified account for one exact application role.
2330
+ * This deliberately does not establish a normal auth session. Applications
2331
+ * should exchange the result for a short-lived, purpose-bound setup session.
2332
+ */
2333
+ async verifyPendingCredentials(body, expectedRole) {
2334
+ if (!expectedRole.trim()) {
2335
+ Err8(this.t("errors.invalidCredentials"), 401);
2336
+ }
2337
+ return this.verifyCredentialsForPolicy(body, {
2338
+ kind: "pending",
2339
+ expectedRole: expectedRole.trim().toLowerCase()
2340
+ });
2341
+ }
2342
+ async verifyCredentialsForPolicy(body, policy) {
2326
2343
  const password = body.password;
2327
2344
  const rawIdentifier = "identifier" in body ? body.identifier : body.email;
2328
2345
  const identifier = normalizeAuthIdentifier(rawIdentifier);
@@ -2353,11 +2370,18 @@ var AuthService = class AuthService2 {
2353
2370
  }
2354
2371
  Err8(this.t("errors.invalidCredentials"), 401);
2355
2372
  }
2356
- if (user.status !== "active") {
2357
- Err8(this.t("errors.accountInactive"), 403);
2358
- }
2359
- if (this.config.requireVerifiedEmail && !user.emailVerified) {
2360
- Err8(this.t("errors.emailNotVerified"), 403);
2373
+ if (policy.kind === "pending") {
2374
+ const role = typeof user.role === "string" ? user.role.toLowerCase() : "";
2375
+ if (user.status !== "pending" || user.emailVerified || role !== policy.expectedRole) {
2376
+ Err8(this.t("errors.invalidCredentials"), 401);
2377
+ }
2378
+ } else {
2379
+ if (user.status !== "active") {
2380
+ Err8(this.t("errors.accountInactive"), 403);
2381
+ }
2382
+ if (this.config.requireVerifiedEmail && !user.emailVerified) {
2383
+ Err8(this.t("errors.emailNotVerified"), 403);
2384
+ }
2361
2385
  }
2362
2386
  if ((user.failedLoginAttempts ?? 0) > 0 || user.lockoutUntil) {
2363
2387
  await this.userService.resetFailedAttempts(user.id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "najm-auth",
3
- "version": "2.0.12",
3
+ "version": "2.0.13",
4
4
  "description": "Authentication and authorization library for najm framework",
5
5
  "type": "module",
6
6
  "files": [