@meridianjs/auth 0.1.0 → 0.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.
package/dist/index.d.mts CHANGED
@@ -2,11 +2,13 @@ import * as _meridianjs_types from '@meridianjs/types';
2
2
  import { MeridianContainer } from '@meridianjs/types';
3
3
  import { Response, NextFunction } from 'express';
4
4
 
5
+ type UserRole = "super-admin" | "admin" | "moderator" | "member";
5
6
  interface RegisterInput {
6
7
  email: string;
7
8
  password: string;
8
9
  first_name?: string;
9
10
  last_name?: string;
11
+ role?: UserRole;
10
12
  }
11
13
  interface LoginInput {
12
14
  email: string;
package/dist/index.d.ts CHANGED
@@ -2,11 +2,13 @@ import * as _meridianjs_types from '@meridianjs/types';
2
2
  import { MeridianContainer } from '@meridianjs/types';
3
3
  import { Response, NextFunction } from 'express';
4
4
 
5
+ type UserRole = "super-admin" | "admin" | "moderator" | "member";
5
6
  interface RegisterInput {
6
7
  email: string;
7
8
  password: string;
8
9
  first_name?: string;
9
10
  last_name?: string;
11
+ role?: UserRole;
10
12
  }
11
13
  interface LoginInput {
12
14
  email: string;
package/dist/index.js CHANGED
@@ -61,14 +61,20 @@ var AuthModuleService = class extends (0, import_framework_utils.MeridianService
61
61
  throw Object.assign(new Error("Email already registered"), { status: 409 });
62
62
  }
63
63
  const password_hash = await import_bcrypt.default.hash(input.password, BCRYPT_ROUNDS);
64
+ let role = input.role ?? "member";
65
+ if (!input.role) {
66
+ const userCount = await userService.countUsers();
67
+ if (userCount === 0) role = "super-admin";
68
+ }
64
69
  const user = await userService.createUser({
65
70
  email: input.email.toLowerCase().trim(),
66
71
  password_hash,
67
72
  first_name: input.first_name ?? null,
68
73
  last_name: input.last_name ?? null,
74
+ role,
69
75
  is_active: true
70
76
  });
71
- const token = this.signToken(user.id, null, [], config.projectConfig.jwtSecret);
77
+ const token = this.signToken(user.id, null, [user.role], config.projectConfig.jwtSecret);
72
78
  return {
73
79
  user: {
74
80
  id: user.id,
@@ -96,7 +102,7 @@ var AuthModuleService = class extends (0, import_framework_utils.MeridianService
96
102
  }
97
103
  await userService.recordLogin(user.id).catch(() => {
98
104
  });
99
- const token = this.signToken(user.id, null, [], config.projectConfig.jwtSecret);
105
+ const token = this.signToken(user.id, null, [user.role ?? "member"], config.projectConfig.jwtSecret);
100
106
  return {
101
107
  user: {
102
108
  id: user.id,
@@ -122,11 +128,11 @@ var AuthModuleService = class extends (0, import_framework_utils.MeridianService
122
128
  var import_jsonwebtoken2 = __toESM(require("jsonwebtoken"));
123
129
  function authenticateJWT(req, res, next) {
124
130
  const authHeader = req.headers.authorization;
125
- if (!authHeader?.startsWith("Bearer ")) {
131
+ const token = authHeader?.startsWith("Bearer ") ? authHeader.substring(7) : req.query?.token;
132
+ if (!token) {
126
133
  res.status(401).json({ error: { message: "Unauthorized \u2014 Bearer token required" } });
127
134
  return;
128
135
  }
129
- const token = authHeader.substring(7);
130
136
  let config;
131
137
  try {
132
138
  const scope = req.scope;
package/dist/index.mjs CHANGED
@@ -22,14 +22,20 @@ var AuthModuleService = class extends MeridianService({}) {
22
22
  throw Object.assign(new Error("Email already registered"), { status: 409 });
23
23
  }
24
24
  const password_hash = await bcrypt.hash(input.password, BCRYPT_ROUNDS);
25
+ let role = input.role ?? "member";
26
+ if (!input.role) {
27
+ const userCount = await userService.countUsers();
28
+ if (userCount === 0) role = "super-admin";
29
+ }
25
30
  const user = await userService.createUser({
26
31
  email: input.email.toLowerCase().trim(),
27
32
  password_hash,
28
33
  first_name: input.first_name ?? null,
29
34
  last_name: input.last_name ?? null,
35
+ role,
30
36
  is_active: true
31
37
  });
32
- const token = this.signToken(user.id, null, [], config.projectConfig.jwtSecret);
38
+ const token = this.signToken(user.id, null, [user.role], config.projectConfig.jwtSecret);
33
39
  return {
34
40
  user: {
35
41
  id: user.id,
@@ -57,7 +63,7 @@ var AuthModuleService = class extends MeridianService({}) {
57
63
  }
58
64
  await userService.recordLogin(user.id).catch(() => {
59
65
  });
60
- const token = this.signToken(user.id, null, [], config.projectConfig.jwtSecret);
66
+ const token = this.signToken(user.id, null, [user.role ?? "member"], config.projectConfig.jwtSecret);
61
67
  return {
62
68
  user: {
63
69
  id: user.id,
@@ -83,11 +89,11 @@ var AuthModuleService = class extends MeridianService({}) {
83
89
  import jwt2 from "jsonwebtoken";
84
90
  function authenticateJWT(req, res, next) {
85
91
  const authHeader = req.headers.authorization;
86
- if (!authHeader?.startsWith("Bearer ")) {
92
+ const token = authHeader?.startsWith("Bearer ") ? authHeader.substring(7) : req.query?.token;
93
+ if (!token) {
87
94
  res.status(401).json({ error: { message: "Unauthorized \u2014 Bearer token required" } });
88
95
  return;
89
96
  }
90
- const token = authHeader.substring(7);
91
97
  let config;
92
98
  try {
93
99
  const scope = req.scope;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meridianjs/auth",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Meridian auth module — JWT authentication and middleware",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",