create-nexwire 4.2.1 → 4.2.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.
Files changed (54) hide show
  1. package/express/src/database/migrations/sqlite/meta/0000_snapshot.json +1 -1
  2. package/express/src/database/migrations/sqlite/meta/_journal.json +1 -1
  3. package/express/src/framework/database/app-schema.ts +37 -0
  4. package/express/src/framework/database/connection.ts +198 -196
  5. package/express/src/framework/database/seed.ts +35 -22
  6. package/express/src/framework/facade.ts +24 -5
  7. package/express/src/framework/kernel.ts +54 -52
  8. package/express/src/framework/maker-cli/index.mjs +2 -0
  9. package/express/src/framework/maker-cli/module/core.mjs +30 -0
  10. package/express/src/framework/maker-cli/module/index.mjs +10 -1
  11. package/express/src/framework/maker-cli/provider/core.mjs +49 -0
  12. package/express/src/framework/maker-cli/provider/index.mjs +12 -0
  13. package/express/src/framework/maker-cli/stubs/notification/controller.ts.stub +77 -77
  14. package/express/src/framework/maker-cli/stubs/provider/module-barrel.ts.stub +19 -0
  15. package/express/src/framework/maker-cli/stubs/provider/module.ts.stub +21 -0
  16. package/express/src/framework/maker-cli/stubs/provider/name.ts.stub +21 -0
  17. package/express/src/framework/maker-cli/utils/help.mjs +6 -1
  18. package/express/src/framework/maker-cli/utils/project-commands.mjs +73 -12
  19. package/express/src/framework/notification/index.ts +63 -7
  20. package/express/src/framework/providers/adapter.ts +125 -0
  21. package/express/src/framework/providers/contracts.ts +56 -0
  22. package/express/src/framework/providers/loader.ts +105 -0
  23. package/express/src/framework/providers/registry.ts +62 -0
  24. package/express/src/middlewares/auth-middleware.ts +65 -38
  25. package/express/src/providers/README.md +107 -0
  26. package/express/src/storage/database/nexwire.sqlite +0 -0
  27. package/hono/src/database/migrations/sqlite/meta/0000_snapshot.json +1 -1
  28. package/hono/src/database/migrations/sqlite/meta/_journal.json +1 -1
  29. package/hono/src/framework/database/app-schema.ts +37 -0
  30. package/hono/src/framework/database/connection.ts +198 -196
  31. package/hono/src/framework/database/seed.ts +35 -22
  32. package/hono/src/framework/facade.ts +48 -29
  33. package/hono/src/framework/kernel.ts +50 -48
  34. package/hono/src/framework/maker-cli/index.mjs +2 -0
  35. package/hono/src/framework/maker-cli/module/core.mjs +30 -0
  36. package/hono/src/framework/maker-cli/module/index.mjs +10 -1
  37. package/hono/src/framework/maker-cli/provider/core.mjs +49 -0
  38. package/hono/src/framework/maker-cli/provider/index.mjs +12 -0
  39. package/hono/src/framework/maker-cli/stubs/notification/controller.ts.stub +85 -85
  40. package/hono/src/framework/maker-cli/stubs/provider/module-barrel.ts.stub +19 -0
  41. package/hono/src/framework/maker-cli/stubs/provider/module.ts.stub +21 -0
  42. package/hono/src/framework/maker-cli/stubs/provider/name.ts.stub +21 -0
  43. package/hono/src/framework/maker-cli/utils/help.mjs +6 -1
  44. package/hono/src/framework/maker-cli/utils/project-commands.mjs +73 -12
  45. package/hono/src/framework/notification/index.ts +63 -7
  46. package/hono/src/framework/providers/adapter.ts +125 -0
  47. package/hono/src/framework/providers/contracts.ts +56 -0
  48. package/hono/src/framework/providers/loader.ts +105 -0
  49. package/hono/src/framework/providers/registry.ts +62 -0
  50. package/hono/src/middlewares/auth-middleware.ts +65 -38
  51. package/hono/src/providers/README.md +107 -0
  52. package/hono/src/storage/database/nexwire.sqlite +0 -0
  53. package/package.json +2 -2
  54. package/src/index.js +14 -6
@@ -1,10 +1,27 @@
1
1
  import { eq } from "drizzle-orm";
2
2
  import { db } from "@/framework/database/connection.js";
3
3
  import { dispatchEvent } from "@/framework/events/dispatcher.js";
4
+ import { resolveAuthProvider } from "@/framework/providers/adapter.js";
4
5
  import { redisClientIfReady } from "@/framework/redis/client.js";
5
6
  import { mail } from "@/framework/support/mail.js";
6
- import { notifications } from "@/modules/auth/database/models/notification.js";
7
- import { users } from "@/modules/auth/database/models/user.js";
7
+
8
+ let notifications: any = null;
9
+ let modelsLoaded = false;
10
+ let modelsLoadAttempted = false;
11
+
12
+ async function loadNotificationModel() {
13
+ if (modelsLoaded) return true;
14
+ if (modelsLoadAttempted) return false;
15
+ modelsLoadAttempted = true;
16
+ try {
17
+ const notificationMod = await import("@/modules/auth/database/models/notification.js").catch(() => null);
18
+ if (notificationMod?.notifications) notifications = notificationMod.notifications;
19
+ modelsLoaded = !!notifications;
20
+ return modelsLoaded;
21
+ } catch {
22
+ return false;
23
+ }
24
+ }
8
25
 
9
26
  /** Extra JSON metadata attached to the notification (e.g. entity IDs, links). */
10
27
  export type NotificationOptions = {
@@ -22,6 +39,8 @@ export type NotificationOptions = {
22
39
  broadcast?: boolean;
23
40
  /** Email delivery options */
24
41
  mail?: {
42
+ /** Explicit recipient. Skips the auth provider contact lookup. */
43
+ to?: string;
25
44
  subject?: string;
26
45
  html?: string;
27
46
  };
@@ -29,6 +48,13 @@ export type NotificationOptions = {
29
48
 
30
49
  /** Create a notification and persist it to the database. Optionally broadcast via Socket.IO and/or send by email. */
31
50
  export async function notify(userId: number, options: NotificationOptions) {
51
+ const ok = await loadNotificationModel();
52
+ if (!ok || !notifications) {
53
+ console.warn(
54
+ "[notification] skipped: notification model not available (@/modules/auth/database/models/notification.js). Delete or replace auth/notification without breaking framework."
55
+ );
56
+ return null;
57
+ }
32
58
  const insertResult = await db.insert(notifications).values({
33
59
  userId,
34
60
  type: options.type || "info",
@@ -63,9 +89,39 @@ async function sendMailChannel(
63
89
  notification: { id: number; title: string; body: string | null },
64
90
  options: NotificationOptions
65
91
  ) {
66
- const [user] = await db.select({ email: users.email, name: users.name }).from(users).where(eq(users.id, userId));
92
+ let to: string | null = (options.mail as any)?.to || null;
93
+ let name: string | null = null;
94
+
95
+ if (!to) {
96
+ try {
97
+ const provider = await resolveAuthProvider();
98
+ if (provider?.getUserContact) {
99
+ const contact = await provider.getUserContact(userId);
100
+ if (contact?.email) {
101
+ to = contact.email as string;
102
+ name = (contact.name as string) || null;
103
+ }
104
+ }
105
+ } catch {}
106
+ }
107
+
108
+ if (!to) {
109
+ try {
110
+ const userMod = await import("@/modules/auth/database/models/user.js").catch(() => null);
111
+ if (userMod?.users) {
112
+ const [user] = await db
113
+ .select({ email: userMod.users.email, name: (userMod.users as any).name })
114
+ .from(userMod.users)
115
+ .where(eq(userMod.users.id, userId));
116
+ if (user?.email) {
117
+ to = user.email as string;
118
+ name = (user as any).name || null;
119
+ }
120
+ }
121
+ } catch {}
122
+ }
67
123
 
68
- if (!user?.email) return;
124
+ if (!to) return;
69
125
 
70
126
  const subject = options.mail?.subject || notification.title;
71
127
  const html = options.mail?.html || defaultMailHtml(notification.title, notification.body);
@@ -74,14 +130,14 @@ async function sendMailChannel(
74
130
  await dispatchEvent(
75
131
  "notification:mail",
76
132
  {
77
- to: user.email,
133
+ to,
78
134
  subject,
79
135
  html
80
136
  },
81
137
  { queue: "mail" }
82
138
  );
83
139
  } else {
84
- await mail.sendMail({ to: user.email, subject, html });
140
+ await mail.sendMail({ to, subject, html });
85
141
  }
86
142
  }
87
143
 
@@ -95,7 +151,7 @@ function escapeHtml(str: string) {
95
151
  }
96
152
 
97
153
  /** Convert a DB row into the normalized response shape, parsing the `data` JSON column. */
98
- function normalize(row: typeof notifications.$inferSelect) {
154
+ function normalize(row: any) {
99
155
  let parsed: unknown = null;
100
156
  if (row.data) {
101
157
  try {
@@ -0,0 +1,125 @@
1
+ import { eq } from "drizzle-orm";
2
+ import { db } from "@/framework/database/connection.js";
3
+ import type { AuthProvider } from "./contracts.js";
4
+ import { loadProviders } from "./loader.js";
5
+ import { getAuthProvider } from "./registry.js";
6
+
7
+ let defaultProvider: AuthProvider | null = null;
8
+ let defaultLoadAttempted = false;
9
+
10
+ /**
11
+ * Why: Builds the built-in auth provider from the default auth module.
12
+ * When: No custom provider was registered by the application.
13
+ * Where: Auth provider adapter.
14
+ * How: Lazily imports the default user/refresh-token models. When the default
15
+ * auth module was deleted or replaced, no provider is produced.
16
+ */
17
+ async function loadDefaultProvider(): Promise<AuthProvider | null> {
18
+ if (defaultProvider) return defaultProvider;
19
+ if (defaultLoadAttempted) return null;
20
+ defaultLoadAttempted = true;
21
+ try {
22
+ const userMod = await import("@/modules/auth/database/models/user.js").catch(() => null);
23
+ if (!userMod?.users || !userMod?.refreshTokens) return null;
24
+
25
+ const users = userMod.users;
26
+ const refreshTokens = userMod.refreshTokens;
27
+
28
+ defaultProvider = {
29
+ async getUserById(id: number) {
30
+ try {
31
+ const user = await (db.query.users.findFirst as any)?.({
32
+ where: eq(users.id, id),
33
+ with: { role: true }
34
+ });
35
+ if (user) return user;
36
+ } catch {}
37
+ try {
38
+ const rows = await db.select().from(users).where(eq(users.id, id));
39
+ return rows?.[0] ?? null;
40
+ } catch {
41
+ return null;
42
+ }
43
+ },
44
+ async findRefreshToken(jti: string) {
45
+ try {
46
+ const token = await (db.query.refreshTokens.findFirst as any)?.({
47
+ where: eq(refreshTokens.jti, jti)
48
+ });
49
+ if (token) return token;
50
+ } catch {}
51
+ try {
52
+ const rows = await db.select().from(refreshTokens).where(eq(refreshTokens.jti, jti));
53
+ return rows?.[0] ?? null;
54
+ } catch {
55
+ return null;
56
+ }
57
+ },
58
+ async deleteRefreshToken(tokenId: number) {
59
+ try {
60
+ await db.delete(refreshTokens).where(eq(refreshTokens.id, tokenId));
61
+ } catch {}
62
+ },
63
+ async revokeRefreshToken(tokenId: number) {
64
+ try {
65
+ await db.delete(refreshTokens).where(eq(refreshTokens.id, tokenId));
66
+ } catch {}
67
+ },
68
+ async getAuthContext(user: any, payload?: any) {
69
+ try {
70
+ if (user?.role && typeof user.role === "object") {
71
+ return {
72
+ ...(payload || {}),
73
+ id: user.id,
74
+ email: user.email,
75
+ roleId: user.role?.id ?? null,
76
+ role: user.role?.name ?? null,
77
+ ...user
78
+ };
79
+ }
80
+ } catch {}
81
+ return {
82
+ ...(payload || {}),
83
+ id: user.id,
84
+ email: user.email,
85
+ roleId: user.roleId ?? null,
86
+ role: user.role ?? null,
87
+ ...user
88
+ };
89
+ },
90
+ async getUserContact(userId: number) {
91
+ try {
92
+ const user = await this.getUserById(userId);
93
+ if (!user) return null;
94
+ return {
95
+ email: user.email ?? null,
96
+ name: user.name ?? user.fullName ?? user.username ?? null,
97
+ phone: user.phone ?? user.phoneNumber ?? null
98
+ };
99
+ } catch {
100
+ return null;
101
+ }
102
+ }
103
+ };
104
+ return defaultProvider;
105
+ } catch {
106
+ return null;
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Why: Resolves the auth provider the framework should use.
112
+ * When: The auth middleware or the notification mailer needs user data.
113
+ * Where: Auth provider adapter.
114
+ * How: Application providers are loaded first, so a file in `src/providers/`
115
+ * wins. Otherwise the built-in provider from the default auth module is
116
+ * used. When neither exists the framework degrades gracefully.
117
+ */
118
+ export async function resolveAuthProvider(): Promise<AuthProvider | null> {
119
+ await loadProviders();
120
+ const registered = getAuthProvider();
121
+ if (registered) return registered;
122
+ const def = await loadDefaultProvider();
123
+ if (def) return def;
124
+ return null;
125
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Why: Defines the shapes a pluggable provider has to satisfy.
3
+ * When: A developer replaces a framework default (auth today, payments later).
4
+ * Where: Imported through the framework facade, never from framework internals.
5
+ * How: Interfaces stay deliberately loose so custom user shapes pass through.
6
+ */
7
+
8
+ /** Registry key of the built-in auth provider contract. */
9
+ export const AUTH_PROVIDER_KEY = "auth";
10
+
11
+ /**
12
+ * Registry key for any provider implementation.
13
+ *
14
+ * `string & {}` keeps the literal keys above discoverable by editors while
15
+ * still accepting any custom key, so a project can register its own contracts
16
+ * (payments, otp, storage, ...) without the framework having to know them.
17
+ */
18
+ export type ProviderKey = typeof AUTH_PROVIDER_KEY | (string & {});
19
+
20
+ export interface AuthProviderUser {
21
+ id: number;
22
+ email?: string | null;
23
+ [key: string]: any;
24
+ }
25
+
26
+ export interface AuthProviderRefreshToken {
27
+ id: number;
28
+ jti: string;
29
+ revoked?: boolean | null;
30
+ expiresAt?: Date | string | null;
31
+ userId?: number | null;
32
+ [key: string]: any;
33
+ }
34
+
35
+ export interface AuthUserContext {
36
+ id: number;
37
+ email?: string | null;
38
+ roleId?: number | null;
39
+ role?: string | null;
40
+ [key: string]: any;
41
+ }
42
+
43
+ export interface AuthUserContact {
44
+ email?: string | null;
45
+ name?: string | null;
46
+ phone?: string | null;
47
+ }
48
+
49
+ export interface AuthProvider {
50
+ getUserById(id: number): Promise<AuthProviderUser | null>;
51
+ findRefreshToken(jti: string): Promise<AuthProviderRefreshToken | null>;
52
+ revokeRefreshToken?(tokenId: number): Promise<void>;
53
+ deleteRefreshToken?(tokenId: number): Promise<void>;
54
+ getAuthContext?(user: AuthProviderUser, payload?: any): Promise<AuthUserContext | any>;
55
+ getUserContact?(userId: number): Promise<AuthUserContact | null>;
56
+ }
@@ -0,0 +1,105 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { pathToFileURL } from "node:url";
4
+ import { glob } from "glob";
5
+ import { discoverModuleFiles } from "@/framework/modules/discover.js";
6
+ import { logger } from "@/framework/support/logger.js";
7
+
8
+ /**
9
+ * Why: Finds the app-level provider folder for the running code.
10
+ * When: Providers are loaded at boot.
11
+ * Where: Provider loader.
12
+ * How: Mirrors the module discovery rule — `src/providers` in development,
13
+ * `dist/src/providers` when the compiled server is the entrypoint.
14
+ */
15
+ function resolveProvidersPath() {
16
+ const cwd = process.cwd();
17
+ const srcProviders = path.resolve(cwd, "src/providers");
18
+ const distProviders = path.resolve(cwd, "dist/src/providers");
19
+ const entry = String(process.argv[1] || "");
20
+ const runningDist = /[\\/]dist[\\/]/.test(entry);
21
+
22
+ if (runningDist && fs.existsSync(distProviders)) return distProviders;
23
+ return srcProviders;
24
+ }
25
+
26
+ export const providersPath = resolveProvidersPath();
27
+
28
+ let loadPromise: Promise<number> | null = null;
29
+ let loading = false;
30
+
31
+ /**
32
+ * Why: Collects every provider file that should run at boot.
33
+ * When: The loader runs.
34
+ * Where: Provider loader.
35
+ * How: Globs `src/providers/**` for app-wide providers, and
36
+ * `src/modules/**\/providers.ts` plus `src/modules/**\/providers/*.ts` for
37
+ * module-scoped ones. Tests and declaration files are ignored.
38
+ */
39
+ async function collectProviderFiles(): Promise<string[]> {
40
+ const files: string[] = [];
41
+
42
+ if (fs.existsSync(providersPath)) {
43
+ const appFiles = await glob("**/*.{ts,js,mjs}", {
44
+ cwd: providersPath,
45
+ absolute: true,
46
+ nodir: true,
47
+ ignore: ["**/__tests__/**", "**/*.d.ts", "**/*.test.*", "**/*.spec.*"],
48
+ windowsPathsNoEscape: true
49
+ });
50
+ files.push(...appFiles);
51
+ }
52
+
53
+ try {
54
+ // A module plugs its own implementation in either from a single file
55
+ // (providers.ts) or from a folder (providers/*.ts). Both are imported.
56
+ const moduleFiles = await discoverModuleFiles("**/providers.{ts,js}");
57
+ const moduleProviderFiles = await discoverModuleFiles("**/providers/*.{ts,js}");
58
+ files.push(...moduleFiles, ...moduleProviderFiles);
59
+ } catch {}
60
+
61
+ return [...new Set(files)].sort();
62
+ }
63
+
64
+ /**
65
+ * Why: Imports each discovered provider file so its registration runs.
66
+ * When: The loader runs.
67
+ * Where: Provider loader.
68
+ * How: Imports in sorted order; a broken file is logged and skipped instead of
69
+ * taking the whole application down.
70
+ */
71
+ async function loadProviderFiles(): Promise<number> {
72
+ const files = await collectProviderFiles();
73
+
74
+ for (const file of files) {
75
+ try {
76
+ await import(pathToFileURL(file).href);
77
+ } catch (error) {
78
+ logger.warn("Provider file failed to load", {
79
+ file,
80
+ error: error instanceof Error ? error.message : String(error)
81
+ });
82
+ }
83
+ }
84
+
85
+ return files.length;
86
+ }
87
+
88
+ /**
89
+ * Why: Runs the application's provider files exactly once.
90
+ * When: The kernel boots, and on the first provider lookup.
91
+ * Where: Kernel startup and the auth provider adapter.
92
+ * How: Caches the promise so repeated lookups cost nothing. A provider file
93
+ * that itself asks for a provider during load gets an immediate answer
94
+ * instead of deadlocking on its own load.
95
+ */
96
+ export async function loadProviders(): Promise<number> {
97
+ if (loading) return 0;
98
+ if (!loadPromise) {
99
+ loading = true;
100
+ loadPromise = loadProviderFiles().finally(() => {
101
+ loading = false;
102
+ });
103
+ }
104
+ return loadPromise;
105
+ }
@@ -0,0 +1,62 @@
1
+ import { AUTH_PROVIDER_KEY, type AuthProvider, type ProviderKey } from "./contracts.js";
2
+
3
+ /**
4
+ * Why: Holds the app-wide provider implementations in one place.
5
+ * When: A provider file in `src/providers/` (or a module's `providers.ts`)
6
+ * registers its implementation at boot.
7
+ * Where: Read by the framework through `resolve*Provider()` helpers.
8
+ * How: A plain key/value map. No container, no decorators, no auto-wiring.
9
+ */
10
+ const registry = new Map<string, unknown>();
11
+
12
+ /**
13
+ * Why: Registers a provider implementation under a key.
14
+ * When: A provider file runs at boot and claims a contract.
15
+ * Where: Application code, through the framework facade.
16
+ * How: Stores the implementation and returns it so it can be inlined.
17
+ */
18
+ export function registerProvider<T>(key: ProviderKey, implementation: T): T {
19
+ registry.set(key, implementation);
20
+ return implementation;
21
+ }
22
+
23
+ /** Returns the registered implementation for a key, or null when absent. */
24
+ export function getProvider<T = unknown>(key: ProviderKey): T | null {
25
+ const found = registry.get(key);
26
+ return (found as T) ?? null;
27
+ }
28
+
29
+ /** Reports whether a provider is registered for a key. */
30
+ export function hasProvider(key: ProviderKey): boolean {
31
+ return registry.has(key);
32
+ }
33
+
34
+ /** Removes a registered provider. Mainly useful in tests. */
35
+ export function clearProvider(key: ProviderKey): void {
36
+ registry.delete(key);
37
+ }
38
+
39
+ /** Lists every registered provider key, sorted for stable output. */
40
+ export function listProviders(): string[] {
41
+ return [...registry.keys()].sort();
42
+ }
43
+
44
+ /** Registers the auth provider that the framework's auth middleware uses. */
45
+ export function registerAuthProvider(provider: AuthProvider): AuthProvider {
46
+ return registerProvider(AUTH_PROVIDER_KEY, provider);
47
+ }
48
+
49
+ /** Returns the registered auth provider, or null when none was registered. */
50
+ export function getAuthProvider(): AuthProvider | null {
51
+ return getProvider<AuthProvider>(AUTH_PROVIDER_KEY);
52
+ }
53
+
54
+ /** Reports whether a custom auth provider is registered. */
55
+ export function hasAuthProvider(): boolean {
56
+ return hasProvider(AUTH_PROVIDER_KEY);
57
+ }
58
+
59
+ /** Removes the registered auth provider. Mainly useful in tests. */
60
+ export function clearAuthProvider(): void {
61
+ clearProvider(AUTH_PROVIDER_KEY);
62
+ }
@@ -1,19 +1,23 @@
1
- import { eq } from "drizzle-orm";
2
1
  import { type NextFunction, type Request, type Response } from "express";
3
- import { cookie, db, HttpStatusCodes, jwt } from "@/framework/facade.js";
4
- import { refreshTokens, users } from "@/modules/auth/database/models/user.js";
2
+ import { cookie, HttpStatusCodes, jwt } from "@/framework/facade.js";
3
+ import { resolveAuthProvider } from "@/framework/providers/adapter.js";
5
4
 
6
5
  export async function authMiddleware(req: Request, res: Response, next: NextFunction) {
6
+ const provider = await resolveAuthProvider();
7
+ if (!provider) {
8
+ console.warn(
9
+ "[auth-middleware] auth provider not available; skipping auth. Register one in src/providers/ or restore src/modules/auth."
10
+ );
11
+ return next();
12
+ }
13
+
7
14
  const accessToken = await cookie.getAuth(req);
8
15
 
9
16
  if (accessToken) {
10
- const accessPayload = await jwt.verifyToken(accessToken, "access");
17
+ const accessPayload: any = await jwt.verifyToken(accessToken, "access");
11
18
 
12
19
  if (accessPayload) {
13
- const user = await db.query.users.findFirst({
14
- where: eq(users.id, accessPayload.id as number),
15
- with: { role: true }
16
- });
20
+ const user = await provider.getUserById((accessPayload as any)?.id);
17
21
 
18
22
  if (!user) {
19
23
  cookie.deleteAuth(res);
@@ -21,13 +25,20 @@ export async function authMiddleware(req: Request, res: Response, next: NextFunc
21
25
  return res.status(HttpStatusCodes.UNAUTHORIZED).json({ message: "Unauthorized" });
22
26
  }
23
27
 
24
- res.locals.auth = {
25
- ...accessPayload,
26
- id: user.id,
27
- email: user.email,
28
- roleId: user.role?.id ?? null,
29
- role: user.role?.name ?? null
30
- };
28
+ let authCtx: any;
29
+ if (provider.getAuthContext) {
30
+ authCtx = await provider.getAuthContext(user, accessPayload);
31
+ } else {
32
+ authCtx = {
33
+ ...(accessPayload || {}),
34
+ id: (user as any).id,
35
+ email: (user as any).email,
36
+ roleId: (user as any).role?.id ?? (user as any).roleId ?? null,
37
+ role: (user as any).role?.name ?? (user as any).role ?? null,
38
+ ...(user as any)
39
+ };
40
+ }
41
+ res.locals.auth = authCtx;
31
42
  return next();
32
43
  }
33
44
  }
@@ -40,7 +51,7 @@ export async function authMiddleware(req: Request, res: Response, next: NextFunc
40
51
  return res.status(HttpStatusCodes.UNAUTHORIZED).json({ message: "Unauthorized" });
41
52
  }
42
53
 
43
- const refreshPayload = await jwt.verifyToken(refreshToken, "refresh");
54
+ const refreshPayload: any = await jwt.verifyToken(refreshToken, "refresh");
44
55
 
45
56
  if (!refreshPayload?.jti) {
46
57
  cookie.deleteAuth(res);
@@ -48,27 +59,27 @@ export async function authMiddleware(req: Request, res: Response, next: NextFunc
48
59
  return res.status(HttpStatusCodes.UNAUTHORIZED).json({ message: "Invalid token" });
49
60
  }
50
61
 
51
- const storedToken = await db.query.refreshTokens.findFirst({
52
- where: eq(refreshTokens.jti, refreshPayload.jti as string)
53
- });
62
+ const storedToken = await provider.findRefreshToken((refreshPayload as any)?.jti);
54
63
 
55
- if (!storedToken || storedToken.revoked) {
64
+ if (!storedToken || (storedToken as any).revoked) {
56
65
  cookie.deleteAuth(res);
57
66
  cookie.deleteRefresh(res);
58
67
  return res.status(HttpStatusCodes.UNAUTHORIZED).json({ message: "Invalid token" });
59
68
  }
60
69
 
61
- if (storedToken.expiresAt.getTime() < Date.now()) {
62
- await db.delete(refreshTokens).where(eq(refreshTokens.id, storedToken.id));
63
- cookie.deleteAuth(res);
64
- cookie.deleteRefresh(res);
65
- return res.status(HttpStatusCodes.UNAUTHORIZED).json({ message: "Invalid token" });
70
+ const expiresAt = (storedToken as any).expiresAt;
71
+ if (expiresAt) {
72
+ const exp = expiresAt instanceof Date ? expiresAt.getTime() : new Date(expiresAt).getTime();
73
+ if (!Number.isNaN(exp) && exp < Date.now()) {
74
+ if (provider.deleteRefreshToken) await provider.deleteRefreshToken((storedToken as any).id);
75
+ else if (provider.revokeRefreshToken) await provider.revokeRefreshToken((storedToken as any).id);
76
+ cookie.deleteAuth(res);
77
+ cookie.deleteRefresh(res);
78
+ return res.status(HttpStatusCodes.UNAUTHORIZED).json({ message: "Invalid token" });
79
+ }
66
80
  }
67
81
 
68
- const user = await db.query.users.findFirst({
69
- where: eq(users.id, refreshPayload.id as number),
70
- with: { role: true }
71
- });
82
+ const user = await provider.getUserById((refreshPayload as any)?.id);
72
83
 
73
84
  if (!user) {
74
85
  cookie.deleteAuth(res);
@@ -76,23 +87,39 @@ export async function authMiddleware(req: Request, res: Response, next: NextFunc
76
87
  return res.status(HttpStatusCodes.UNAUTHORIZED).json({ message: "Unauthorized" });
77
88
  }
78
89
 
90
+ let baseCtx: any;
91
+ if (provider.getAuthContext) {
92
+ baseCtx = await provider.getAuthContext(user, refreshPayload);
93
+ } else {
94
+ baseCtx = {
95
+ ...(refreshPayload || {}),
96
+ id: (user as any).id,
97
+ email: (user as any).email,
98
+ roleId: (user as any).role?.id ?? (user as any).roleId ?? null,
99
+ role: (user as any).role?.name ?? (user as any).role ?? null,
100
+ ...(user as any)
101
+ };
102
+ }
103
+
79
104
  const newAccessToken = await jwt.generateToken(
80
105
  {
81
- id: user.id,
82
- email: user.email,
83
- roleId: user.role?.id ?? null,
84
- role: user.role?.name ?? null
106
+ id: (user as any).id,
107
+ email: (user as any).email,
108
+ roleId: baseCtx?.roleId ?? (user as any).role?.id ?? (user as any).roleId ?? null,
109
+ role: baseCtx?.role ?? (user as any).role?.name ?? (user as any).role ?? null
85
110
  },
86
111
  "access"
87
112
  );
88
113
 
89
114
  await cookie.setAuth(res, newAccessToken.token);
90
115
  res.locals.auth = {
91
- id: user.id,
92
- email: user.email,
93
- roleId: user.role?.id ?? null,
94
- role: user.role?.name ?? null,
95
- type: "access"
116
+ ...(baseCtx || {}),
117
+ id: (user as any).id,
118
+ email: (user as any).email,
119
+ roleId: baseCtx?.roleId ?? (user as any).role?.id ?? (user as any).roleId ?? null,
120
+ role: baseCtx?.role ?? (user as any).role?.name ?? (user as any).role ?? null,
121
+ type: "access",
122
+ ...(user as any)
96
123
  };
97
124
  return next();
98
125
  }