@meridianjs/user 0.1.2 → 0.1.3
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 +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +68 -12
- package/dist/index.mjs +59 -3
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -13,6 +13,14 @@ declare class UserModuleService extends UserModuleService_base {
|
|
|
13
13
|
deactivateUser(userId: string): Promise<any>;
|
|
14
14
|
/** Return the total number of registered users. */
|
|
15
15
|
countUsers(): Promise<number>;
|
|
16
|
+
/** Store a new session record when a token is issued. */
|
|
17
|
+
createSession(jti: string, userId: string, expiresAt: Date): Promise<void>;
|
|
18
|
+
/** Returns true if the session is valid (exists, not revoked, not expired). */
|
|
19
|
+
isSessionValid(jti: string): Promise<boolean>;
|
|
20
|
+
/** Revoke a single session by jti (logout). */
|
|
21
|
+
revokeSession(jti: string): Promise<void>;
|
|
22
|
+
/** Revoke all active sessions for a user (admin-initiated kick). */
|
|
23
|
+
revokeAllUserSessions(userId: string): Promise<void>;
|
|
16
24
|
}
|
|
17
25
|
|
|
18
26
|
declare const USER_MODULE = "userModuleService";
|
package/dist/index.d.ts
CHANGED
|
@@ -13,6 +13,14 @@ declare class UserModuleService extends UserModuleService_base {
|
|
|
13
13
|
deactivateUser(userId: string): Promise<any>;
|
|
14
14
|
/** Return the total number of registered users. */
|
|
15
15
|
countUsers(): Promise<number>;
|
|
16
|
+
/** Store a new session record when a token is issued. */
|
|
17
|
+
createSession(jti: string, userId: string, expiresAt: Date): Promise<void>;
|
|
18
|
+
/** Returns true if the session is valid (exists, not revoked, not expired). */
|
|
19
|
+
isSessionValid(jti: string): Promise<boolean>;
|
|
20
|
+
/** Revoke a single session by jti (logout). */
|
|
21
|
+
revokeSession(jti: string): Promise<void>;
|
|
22
|
+
/** Revoke all active sessions for a user (admin-initiated kick). */
|
|
23
|
+
revokeAllUserSessions(userId: string): Promise<void>;
|
|
16
24
|
}
|
|
17
25
|
|
|
18
26
|
declare const USER_MODULE = "userModuleService";
|
package/dist/index.js
CHANGED
|
@@ -25,10 +25,10 @@ __export(index_exports, {
|
|
|
25
25
|
default: () => index_default
|
|
26
26
|
});
|
|
27
27
|
module.exports = __toCommonJS(index_exports);
|
|
28
|
-
var
|
|
28
|
+
var import_framework_utils6 = require("@meridianjs/framework-utils");
|
|
29
29
|
|
|
30
30
|
// src/service.ts
|
|
31
|
-
var
|
|
31
|
+
var import_framework_utils4 = require("@meridianjs/framework-utils");
|
|
32
32
|
|
|
33
33
|
// src/models/user.ts
|
|
34
34
|
var import_framework_utils = require("@meridianjs/framework-utils");
|
|
@@ -42,6 +42,7 @@ var User = import_framework_utils.model.define("user", {
|
|
|
42
42
|
role: import_framework_utils.model.enum(["super-admin", "admin", "moderator", "member"]).default("member"),
|
|
43
43
|
is_active: import_framework_utils.model.boolean().default(true),
|
|
44
44
|
last_login_at: import_framework_utils.model.date().nullable(),
|
|
45
|
+
app_role_id: import_framework_utils.model.text().nullable(),
|
|
45
46
|
metadata: import_framework_utils.model.json().nullable()
|
|
46
47
|
}, [
|
|
47
48
|
{ columns: ["email"], unique: true }
|
|
@@ -59,8 +60,22 @@ var Team = import_framework_utils2.model.define("team", {
|
|
|
59
60
|
});
|
|
60
61
|
var team_default = Team;
|
|
61
62
|
|
|
63
|
+
// src/models/user-session.ts
|
|
64
|
+
var import_framework_utils3 = require("@meridianjs/framework-utils");
|
|
65
|
+
var UserSession = import_framework_utils3.model.define("user_session", {
|
|
66
|
+
id: import_framework_utils3.model.id().primaryKey(),
|
|
67
|
+
user_id: import_framework_utils3.model.text(),
|
|
68
|
+
jti: import_framework_utils3.model.text(),
|
|
69
|
+
expires_at: import_framework_utils3.model.dateTime(),
|
|
70
|
+
revoked_at: import_framework_utils3.model.dateTime().nullable()
|
|
71
|
+
}, [
|
|
72
|
+
{ columns: ["jti"], unique: true },
|
|
73
|
+
{ columns: ["user_id"] }
|
|
74
|
+
]);
|
|
75
|
+
var user_session_default = UserSession;
|
|
76
|
+
|
|
62
77
|
// src/service.ts
|
|
63
|
-
var UserModuleService = class extends (0,
|
|
78
|
+
var UserModuleService = class extends (0, import_framework_utils4.MeridianService)({ User: user_default, Team: team_default, UserSession: user_session_default }) {
|
|
64
79
|
// Subclasses store their own container reference for custom methods
|
|
65
80
|
container;
|
|
66
81
|
constructor(container) {
|
|
@@ -90,30 +105,71 @@ var UserModuleService = class extends (0, import_framework_utils3.MeridianServic
|
|
|
90
105
|
const [, count] = await userRepository.findAndCount({});
|
|
91
106
|
return count;
|
|
92
107
|
}
|
|
108
|
+
// ── Session management (stateful JWT) ───────────────────────────────────────
|
|
109
|
+
/** Store a new session record when a token is issued. */
|
|
110
|
+
async createSession(jti, userId, expiresAt) {
|
|
111
|
+
const repo = this.container.resolve("userSessionRepository");
|
|
112
|
+
const session = repo.create({ jti, user_id: userId, expires_at: expiresAt });
|
|
113
|
+
await repo.persistAndFlush(session);
|
|
114
|
+
}
|
|
115
|
+
/** Returns true if the session is valid (exists, not revoked, not expired). */
|
|
116
|
+
async isSessionValid(jti) {
|
|
117
|
+
const repo = this.container.resolve("userSessionRepository");
|
|
118
|
+
try {
|
|
119
|
+
const session = await repo.findOne({ jti });
|
|
120
|
+
if (!session) return false;
|
|
121
|
+
if (session.revoked_at) return false;
|
|
122
|
+
if (new Date(session.expires_at) < /* @__PURE__ */ new Date()) return false;
|
|
123
|
+
return true;
|
|
124
|
+
} catch {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/** Revoke a single session by jti (logout). */
|
|
129
|
+
async revokeSession(jti) {
|
|
130
|
+
const repo = this.container.resolve("userSessionRepository");
|
|
131
|
+
const session = await repo.findOne({ jti });
|
|
132
|
+
if (session) {
|
|
133
|
+
session.revoked_at = /* @__PURE__ */ new Date();
|
|
134
|
+
await repo.flush();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/** Revoke all active sessions for a user (admin-initiated kick). */
|
|
138
|
+
async revokeAllUserSessions(userId) {
|
|
139
|
+
const repo = this.container.resolve("userSessionRepository");
|
|
140
|
+
const sessions = await repo.find({ user_id: userId, revoked_at: null });
|
|
141
|
+
const now = /* @__PURE__ */ new Date();
|
|
142
|
+
for (const s of sessions) {
|
|
143
|
+
s.revoked_at = now;
|
|
144
|
+
}
|
|
145
|
+
if (sessions.length > 0) await repo.flush();
|
|
146
|
+
}
|
|
93
147
|
};
|
|
94
148
|
|
|
95
149
|
// src/loaders/default.ts
|
|
96
|
-
var
|
|
97
|
-
var UserSchema = (0,
|
|
98
|
-
var TeamSchema = (0,
|
|
99
|
-
var
|
|
150
|
+
var import_framework_utils5 = require("@meridianjs/framework-utils");
|
|
151
|
+
var UserSchema = (0, import_framework_utils5.dmlToEntitySchema)(user_default);
|
|
152
|
+
var TeamSchema = (0, import_framework_utils5.dmlToEntitySchema)(team_default);
|
|
153
|
+
var UserSessionSchema = (0, import_framework_utils5.dmlToEntitySchema)(user_session_default);
|
|
154
|
+
var entitySchemas = [UserSchema, TeamSchema, UserSessionSchema];
|
|
100
155
|
async function defaultLoader({ container }) {
|
|
101
156
|
const config = container.resolve("config");
|
|
102
157
|
const { databaseUrl } = config.projectConfig;
|
|
103
|
-
const orm = await (0,
|
|
158
|
+
const orm = await (0, import_framework_utils5.createModuleOrm)(entitySchemas, databaseUrl);
|
|
104
159
|
const em = orm.em.fork();
|
|
105
160
|
container.register({
|
|
106
|
-
userRepository: (0,
|
|
107
|
-
teamRepository: (0,
|
|
161
|
+
userRepository: (0, import_framework_utils5.createRepository)(em, "user"),
|
|
162
|
+
teamRepository: (0, import_framework_utils5.createRepository)(em, "team"),
|
|
163
|
+
userSessionRepository: (0, import_framework_utils5.createRepository)(em, "user_session"),
|
|
108
164
|
userOrm: orm
|
|
109
165
|
});
|
|
110
166
|
}
|
|
111
167
|
|
|
112
168
|
// src/index.ts
|
|
113
169
|
var USER_MODULE = "userModuleService";
|
|
114
|
-
var index_default = (0,
|
|
170
|
+
var index_default = (0, import_framework_utils6.Module)(USER_MODULE, {
|
|
115
171
|
service: UserModuleService,
|
|
116
|
-
models: [user_default, team_default],
|
|
172
|
+
models: [user_default, team_default, user_session_default],
|
|
117
173
|
loaders: [defaultLoader],
|
|
118
174
|
linkable: {
|
|
119
175
|
user: { tableName: "user", primaryKey: "id" },
|
package/dist/index.mjs
CHANGED
|
@@ -16,6 +16,7 @@ var User = model.define("user", {
|
|
|
16
16
|
role: model.enum(["super-admin", "admin", "moderator", "member"]).default("member"),
|
|
17
17
|
is_active: model.boolean().default(true),
|
|
18
18
|
last_login_at: model.date().nullable(),
|
|
19
|
+
app_role_id: model.text().nullable(),
|
|
19
20
|
metadata: model.json().nullable()
|
|
20
21
|
}, [
|
|
21
22
|
{ columns: ["email"], unique: true }
|
|
@@ -33,8 +34,22 @@ var Team = model2.define("team", {
|
|
|
33
34
|
});
|
|
34
35
|
var team_default = Team;
|
|
35
36
|
|
|
37
|
+
// src/models/user-session.ts
|
|
38
|
+
import { model as model3 } from "@meridianjs/framework-utils";
|
|
39
|
+
var UserSession = model3.define("user_session", {
|
|
40
|
+
id: model3.id().primaryKey(),
|
|
41
|
+
user_id: model3.text(),
|
|
42
|
+
jti: model3.text(),
|
|
43
|
+
expires_at: model3.dateTime(),
|
|
44
|
+
revoked_at: model3.dateTime().nullable()
|
|
45
|
+
}, [
|
|
46
|
+
{ columns: ["jti"], unique: true },
|
|
47
|
+
{ columns: ["user_id"] }
|
|
48
|
+
]);
|
|
49
|
+
var user_session_default = UserSession;
|
|
50
|
+
|
|
36
51
|
// src/service.ts
|
|
37
|
-
var UserModuleService = class extends MeridianService({ User: user_default, Team: team_default }) {
|
|
52
|
+
var UserModuleService = class extends MeridianService({ User: user_default, Team: team_default, UserSession: user_session_default }) {
|
|
38
53
|
// Subclasses store their own container reference for custom methods
|
|
39
54
|
container;
|
|
40
55
|
constructor(container) {
|
|
@@ -64,13 +79,53 @@ var UserModuleService = class extends MeridianService({ User: user_default, Team
|
|
|
64
79
|
const [, count] = await userRepository.findAndCount({});
|
|
65
80
|
return count;
|
|
66
81
|
}
|
|
82
|
+
// ── Session management (stateful JWT) ───────────────────────────────────────
|
|
83
|
+
/** Store a new session record when a token is issued. */
|
|
84
|
+
async createSession(jti, userId, expiresAt) {
|
|
85
|
+
const repo = this.container.resolve("userSessionRepository");
|
|
86
|
+
const session = repo.create({ jti, user_id: userId, expires_at: expiresAt });
|
|
87
|
+
await repo.persistAndFlush(session);
|
|
88
|
+
}
|
|
89
|
+
/** Returns true if the session is valid (exists, not revoked, not expired). */
|
|
90
|
+
async isSessionValid(jti) {
|
|
91
|
+
const repo = this.container.resolve("userSessionRepository");
|
|
92
|
+
try {
|
|
93
|
+
const session = await repo.findOne({ jti });
|
|
94
|
+
if (!session) return false;
|
|
95
|
+
if (session.revoked_at) return false;
|
|
96
|
+
if (new Date(session.expires_at) < /* @__PURE__ */ new Date()) return false;
|
|
97
|
+
return true;
|
|
98
|
+
} catch {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** Revoke a single session by jti (logout). */
|
|
103
|
+
async revokeSession(jti) {
|
|
104
|
+
const repo = this.container.resolve("userSessionRepository");
|
|
105
|
+
const session = await repo.findOne({ jti });
|
|
106
|
+
if (session) {
|
|
107
|
+
session.revoked_at = /* @__PURE__ */ new Date();
|
|
108
|
+
await repo.flush();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/** Revoke all active sessions for a user (admin-initiated kick). */
|
|
112
|
+
async revokeAllUserSessions(userId) {
|
|
113
|
+
const repo = this.container.resolve("userSessionRepository");
|
|
114
|
+
const sessions = await repo.find({ user_id: userId, revoked_at: null });
|
|
115
|
+
const now = /* @__PURE__ */ new Date();
|
|
116
|
+
for (const s of sessions) {
|
|
117
|
+
s.revoked_at = now;
|
|
118
|
+
}
|
|
119
|
+
if (sessions.length > 0) await repo.flush();
|
|
120
|
+
}
|
|
67
121
|
};
|
|
68
122
|
|
|
69
123
|
// src/loaders/default.ts
|
|
70
124
|
import { dmlToEntitySchema, createRepository, createModuleOrm } from "@meridianjs/framework-utils";
|
|
71
125
|
var UserSchema = dmlToEntitySchema(user_default);
|
|
72
126
|
var TeamSchema = dmlToEntitySchema(team_default);
|
|
73
|
-
var
|
|
127
|
+
var UserSessionSchema = dmlToEntitySchema(user_session_default);
|
|
128
|
+
var entitySchemas = [UserSchema, TeamSchema, UserSessionSchema];
|
|
74
129
|
async function defaultLoader({ container }) {
|
|
75
130
|
const config = container.resolve("config");
|
|
76
131
|
const { databaseUrl } = config.projectConfig;
|
|
@@ -79,6 +134,7 @@ async function defaultLoader({ container }) {
|
|
|
79
134
|
container.register({
|
|
80
135
|
userRepository: createRepository(em, "user"),
|
|
81
136
|
teamRepository: createRepository(em, "team"),
|
|
137
|
+
userSessionRepository: createRepository(em, "user_session"),
|
|
82
138
|
userOrm: orm
|
|
83
139
|
});
|
|
84
140
|
}
|
|
@@ -87,7 +143,7 @@ async function defaultLoader({ container }) {
|
|
|
87
143
|
var USER_MODULE = "userModuleService";
|
|
88
144
|
var index_default = Module(USER_MODULE, {
|
|
89
145
|
service: UserModuleService,
|
|
90
|
-
models: [user_default, team_default],
|
|
146
|
+
models: [user_default, team_default, user_session_default],
|
|
91
147
|
loaders: [defaultLoader],
|
|
92
148
|
linkable: {
|
|
93
149
|
user: { tableName: "user", primaryKey: "id" },
|