@idevconn/create-icore 0.1.0

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 (241) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +56 -0
  3. package/dist/cli.js +300 -0
  4. package/dist/index.cjs +303 -0
  5. package/dist/index.d.cts +26 -0
  6. package/dist/index.d.ts +26 -0
  7. package/dist/index.js +265 -0
  8. package/package.json +72 -0
  9. package/templates/.husky/pre-commit +56 -0
  10. package/templates/.nvmrc +1 -0
  11. package/templates/.prettierignore +7 -0
  12. package/templates/.prettierrc +7 -0
  13. package/templates/.yarnrc.yml +7 -0
  14. package/templates/apps/api/.env.example +19 -0
  15. package/templates/apps/api/eslint.config.mjs +23 -0
  16. package/templates/apps/api/package.json +20 -0
  17. package/templates/apps/api/project.json +76 -0
  18. package/templates/apps/api/src/app/abilities/__tests__/ability.guard.unit.test.ts +49 -0
  19. package/templates/apps/api/src/app/abilities/abilities.module.ts +10 -0
  20. package/templates/apps/api/src/app/abilities/ability.factory.ts +13 -0
  21. package/templates/apps/api/src/app/abilities/ability.guard.ts +29 -0
  22. package/templates/apps/api/src/app/abilities/check-ability.decorator.ts +12 -0
  23. package/templates/apps/api/src/app/app.module.ts +19 -0
  24. package/templates/apps/api/src/app/auth/__tests__/auth.guard.unit.test.ts +66 -0
  25. package/templates/apps/api/src/app/auth/auth.controller.ts +62 -0
  26. package/templates/apps/api/src/app/auth/auth.guard.ts +42 -0
  27. package/templates/apps/api/src/app/auth/auth.module.ts +17 -0
  28. package/templates/apps/api/src/app/auth/public.decorator.ts +4 -0
  29. package/templates/apps/api/src/app/profile/profile.controller.ts +15 -0
  30. package/templates/apps/api/src/app/profile/profile.module.ts +5 -0
  31. package/templates/apps/api/src/app/storage/__tests__/assert-ownership.unit.test.ts +28 -0
  32. package/templates/apps/api/src/app/storage/assert-ownership.ts +8 -0
  33. package/templates/apps/api/src/app/storage/storage.controller.ts +108 -0
  34. package/templates/apps/api/src/app/storage/storage.module.ts +10 -0
  35. package/templates/apps/api/src/assets/.gitkeep +0 -0
  36. package/templates/apps/api/src/main.ts +43 -0
  37. package/templates/apps/api/tsconfig.app.json +13 -0
  38. package/templates/apps/api/tsconfig.json +16 -0
  39. package/templates/apps/api/tsconfig.spec.json +16 -0
  40. package/templates/apps/api/vitest.config.mts +21 -0
  41. package/templates/apps/api/webpack.config.js +25 -0
  42. package/templates/apps/microservices/auth/.env.example +38 -0
  43. package/templates/apps/microservices/auth/package.json +19 -0
  44. package/templates/apps/microservices/auth/project.json +65 -0
  45. package/templates/apps/microservices/auth/src/app/__tests__/auth.controller.firebase.integration.unit.test.ts +53 -0
  46. package/templates/apps/microservices/auth/src/app/__tests__/auth.controller.supabase.integration.unit.test.ts +47 -0
  47. package/templates/apps/microservices/auth/src/app/__tests__/auth.controller.unit.test.ts +87 -0
  48. package/templates/apps/microservices/auth/src/app/app.module.ts +66 -0
  49. package/templates/apps/microservices/auth/src/app/auth.controller.ts +60 -0
  50. package/templates/apps/microservices/auth/src/assets/.gitkeep +0 -0
  51. package/templates/apps/microservices/auth/src/main.ts +28 -0
  52. package/templates/apps/microservices/auth/tsconfig.app.json +13 -0
  53. package/templates/apps/microservices/auth/tsconfig.json +16 -0
  54. package/templates/apps/microservices/auth/tsconfig.spec.json +16 -0
  55. package/templates/apps/microservices/auth/vitest.config.mts +21 -0
  56. package/templates/apps/microservices/auth/webpack.config.js +25 -0
  57. package/templates/apps/microservices/upload/.env.example +30 -0
  58. package/templates/apps/microservices/upload/package.json +21 -0
  59. package/templates/apps/microservices/upload/project.json +65 -0
  60. package/templates/apps/microservices/upload/src/app/__tests__/storage.controller.unit.test.ts +49 -0
  61. package/templates/apps/microservices/upload/src/app/app.module.ts +117 -0
  62. package/templates/apps/microservices/upload/src/app/storage.controller.ts +51 -0
  63. package/templates/apps/microservices/upload/src/assets/.gitkeep +0 -0
  64. package/templates/apps/microservices/upload/src/main.ts +28 -0
  65. package/templates/apps/microservices/upload/tsconfig.app.json +13 -0
  66. package/templates/apps/microservices/upload/tsconfig.json +16 -0
  67. package/templates/apps/microservices/upload/tsconfig.spec.json +16 -0
  68. package/templates/apps/microservices/upload/vitest.config.mts +22 -0
  69. package/templates/apps/microservices/upload/webpack.config.js +25 -0
  70. package/templates/apps/templates/client-shadcn/.env.example +2 -0
  71. package/templates/apps/templates/client-shadcn/eslint.config.mjs +10 -0
  72. package/templates/apps/templates/client-shadcn/index.html +17 -0
  73. package/templates/apps/templates/client-shadcn/project.json +9 -0
  74. package/templates/apps/templates/client-shadcn/public/favicon.ico +0 -0
  75. package/templates/apps/templates/client-shadcn/src/app/app.module.css +1 -0
  76. package/templates/apps/templates/client-shadcn/src/app/app.spec.tsx +9 -0
  77. package/templates/apps/templates/client-shadcn/src/app/app.tsx +7 -0
  78. package/templates/apps/templates/client-shadcn/src/assets/.gitkeep +0 -0
  79. package/templates/apps/templates/client-shadcn/src/components/AccessDeniedPage.tsx +15 -0
  80. package/templates/apps/templates/client-shadcn/src/components/PageLayout.tsx +55 -0
  81. package/templates/apps/templates/client-shadcn/src/components/layout/LayoutFooter.tsx +8 -0
  82. package/templates/apps/templates/client-shadcn/src/components/layout/LayoutHeader.tsx +57 -0
  83. package/templates/apps/templates/client-shadcn/src/components/layout/LayoutSider.tsx +44 -0
  84. package/templates/apps/templates/client-shadcn/src/components/ui/button.tsx +50 -0
  85. package/templates/apps/templates/client-shadcn/src/components/ui/card.tsx +63 -0
  86. package/templates/apps/templates/client-shadcn/src/components/ui/input.tsx +23 -0
  87. package/templates/apps/templates/client-shadcn/src/components/ui/label.tsx +18 -0
  88. package/templates/apps/templates/client-shadcn/src/globals.css +27 -0
  89. package/templates/apps/templates/client-shadcn/src/layouts/MainLayout.tsx +17 -0
  90. package/templates/apps/templates/client-shadcn/src/lib/notify.ts +15 -0
  91. package/templates/apps/templates/client-shadcn/src/lib/utils.ts +6 -0
  92. package/templates/apps/templates/client-shadcn/src/main.tsx +50 -0
  93. package/templates/apps/templates/client-shadcn/src/routeTree.gen.ts +136 -0
  94. package/templates/apps/templates/client-shadcn/src/routes/__root.tsx +5 -0
  95. package/templates/apps/templates/client-shadcn/src/routes/_dashboard/dashboard.tsx +33 -0
  96. package/templates/apps/templates/client-shadcn/src/routes/_dashboard/profile.tsx +88 -0
  97. package/templates/apps/templates/client-shadcn/src/routes/_dashboard.tsx +16 -0
  98. package/templates/apps/templates/client-shadcn/src/routes/index.tsx +33 -0
  99. package/templates/apps/templates/client-shadcn/src/routes/login.tsx +93 -0
  100. package/templates/apps/templates/client-shadcn/src/styles.css +1 -0
  101. package/templates/apps/templates/client-shadcn/tsconfig.app.json +27 -0
  102. package/templates/apps/templates/client-shadcn/tsconfig.json +21 -0
  103. package/templates/apps/templates/client-shadcn/tsconfig.spec.json +30 -0
  104. package/templates/apps/templates/client-shadcn/vite.config.mts +92 -0
  105. package/templates/apps/templates/client-shadcn-e2e/eslint.config.mjs +12 -0
  106. package/templates/apps/templates/client-shadcn-e2e/playwright.config.ts +69 -0
  107. package/templates/apps/templates/client-shadcn-e2e/project.json +10 -0
  108. package/templates/apps/templates/client-shadcn-e2e/src/icore.spec.ts +27 -0
  109. package/templates/apps/templates/client-shadcn-e2e/tsconfig.json +19 -0
  110. package/templates/eslint.config.mjs +20 -0
  111. package/templates/libs/auth-client/README.md +11 -0
  112. package/templates/libs/auth-client/eslint.config.mjs +22 -0
  113. package/templates/libs/auth-client/package.json +15 -0
  114. package/templates/libs/auth-client/project.json +19 -0
  115. package/templates/libs/auth-client/src/index.ts +2 -0
  116. package/templates/libs/auth-client/src/lib/auth-client.module.ts +25 -0
  117. package/templates/libs/auth-client/src/lib/auth-client.service.ts +30 -0
  118. package/templates/libs/auth-client/tsconfig.json +24 -0
  119. package/templates/libs/auth-client/tsconfig.lib.json +26 -0
  120. package/templates/libs/auth-client/tsconfig.spec.json +22 -0
  121. package/templates/libs/auth-client/vitest.config.mts +22 -0
  122. package/templates/libs/auth-strategies/firebase/README.md +11 -0
  123. package/templates/libs/auth-strategies/firebase/eslint.config.mjs +22 -0
  124. package/templates/libs/auth-strategies/firebase/package.json +15 -0
  125. package/templates/libs/auth-strategies/firebase/project.json +19 -0
  126. package/templates/libs/auth-strategies/firebase/src/index.ts +4 -0
  127. package/templates/libs/auth-strategies/firebase/src/lib/__tests__/firebase-auth.contract.unit.test.ts +13 -0
  128. package/templates/libs/auth-strategies/firebase/src/lib/firebase-auth.strategy.ts +77 -0
  129. package/templates/libs/auth-strategies/firebase/src/lib/identity-toolkit.client.ts +72 -0
  130. package/templates/libs/auth-strategies/firebase/src/lib/testing/mock-admin-auth.ts +41 -0
  131. package/templates/libs/auth-strategies/firebase/src/lib/testing/mock-identity-toolkit.ts +76 -0
  132. package/templates/libs/auth-strategies/firebase/tsconfig.json +24 -0
  133. package/templates/libs/auth-strategies/firebase/tsconfig.lib.json +23 -0
  134. package/templates/libs/auth-strategies/firebase/tsconfig.spec.json +22 -0
  135. package/templates/libs/auth-strategies/firebase/vitest.config.mts +22 -0
  136. package/templates/libs/auth-strategies/supabase/README.md +11 -0
  137. package/templates/libs/auth-strategies/supabase/eslint.config.mjs +22 -0
  138. package/templates/libs/auth-strategies/supabase/package.json +16 -0
  139. package/templates/libs/auth-strategies/supabase/project.json +19 -0
  140. package/templates/libs/auth-strategies/supabase/src/index.ts +2 -0
  141. package/templates/libs/auth-strategies/supabase/src/lib/__tests__/supabase-auth.contract.unit.test.ts +8 -0
  142. package/templates/libs/auth-strategies/supabase/src/lib/supabase-auth.strategy.ts +79 -0
  143. package/templates/libs/auth-strategies/supabase/src/lib/testing/mock-supabase.ts +107 -0
  144. package/templates/libs/auth-strategies/supabase/tsconfig.json +24 -0
  145. package/templates/libs/auth-strategies/supabase/tsconfig.lib.json +23 -0
  146. package/templates/libs/auth-strategies/supabase/tsconfig.spec.json +22 -0
  147. package/templates/libs/auth-strategies/supabase/vitest.config.mts +22 -0
  148. package/templates/libs/shared/README.md +11 -0
  149. package/templates/libs/shared/eslint.config.mjs +24 -0
  150. package/templates/libs/shared/package.json +14 -0
  151. package/templates/libs/shared/project.json +19 -0
  152. package/templates/libs/shared/src/__tests__/transport.unit.test.ts +58 -0
  153. package/templates/libs/shared/src/abilities/__tests__/ability.unit.test.ts +28 -0
  154. package/templates/libs/shared/src/abilities/ability.ts +21 -0
  155. package/templates/libs/shared/src/abilities/index.ts +2 -0
  156. package/templates/libs/shared/src/abilities/subjects.ts +2 -0
  157. package/templates/libs/shared/src/index.ts +3 -0
  158. package/templates/libs/shared/src/strategies/__tests__/fake-auth.contract.unit.test.ts +4 -0
  159. package/templates/libs/shared/src/strategies/__tests__/fake-storage.contract.unit.test.ts +4 -0
  160. package/templates/libs/shared/src/strategies/auth.ts +21 -0
  161. package/templates/libs/shared/src/strategies/contract/auth-contract.ts +66 -0
  162. package/templates/libs/shared/src/strategies/contract/storage-contract.ts +58 -0
  163. package/templates/libs/shared/src/strategies/fakes/fake-auth.ts +73 -0
  164. package/templates/libs/shared/src/strategies/fakes/fake-storage.ts +51 -0
  165. package/templates/libs/shared/src/strategies/fakes/index.ts +2 -0
  166. package/templates/libs/shared/src/strategies/index.ts +5 -0
  167. package/templates/libs/shared/src/strategies/storage.ts +17 -0
  168. package/templates/libs/shared/src/transport.ts +55 -0
  169. package/templates/libs/shared/tsconfig.json +24 -0
  170. package/templates/libs/shared/tsconfig.lib.json +23 -0
  171. package/templates/libs/shared/tsconfig.spec.json +22 -0
  172. package/templates/libs/shared/vitest.config.mts +21 -0
  173. package/templates/libs/storage-strategies/cloudinary/README.md +11 -0
  174. package/templates/libs/storage-strategies/cloudinary/eslint.config.mjs +23 -0
  175. package/templates/libs/storage-strategies/cloudinary/package.json +15 -0
  176. package/templates/libs/storage-strategies/cloudinary/project.json +19 -0
  177. package/templates/libs/storage-strategies/cloudinary/src/index.ts +2 -0
  178. package/templates/libs/storage-strategies/cloudinary/src/lib/__tests__/cloudinary-storage.contract.unit.test.ts +8 -0
  179. package/templates/libs/storage-strategies/cloudinary/src/lib/cloudinary-storage.strategy.ts +75 -0
  180. package/templates/libs/storage-strategies/cloudinary/src/lib/testing/mock-cloudinary.ts +36 -0
  181. package/templates/libs/storage-strategies/cloudinary/tsconfig.json +24 -0
  182. package/templates/libs/storage-strategies/cloudinary/tsconfig.lib.json +23 -0
  183. package/templates/libs/storage-strategies/cloudinary/tsconfig.spec.json +22 -0
  184. package/templates/libs/storage-strategies/cloudinary/vitest.config.mts +22 -0
  185. package/templates/libs/storage-strategies/firebase/README.md +11 -0
  186. package/templates/libs/storage-strategies/firebase/eslint.config.mjs +23 -0
  187. package/templates/libs/storage-strategies/firebase/package.json +15 -0
  188. package/templates/libs/storage-strategies/firebase/project.json +19 -0
  189. package/templates/libs/storage-strategies/firebase/src/index.ts +2 -0
  190. package/templates/libs/storage-strategies/firebase/src/lib/__tests__/firebase-storage.contract.unit.test.ts +8 -0
  191. package/templates/libs/storage-strategies/firebase/src/lib/firebase-storage.strategy.ts +63 -0
  192. package/templates/libs/storage-strategies/firebase/src/lib/testing/mock-firebase-storage.ts +43 -0
  193. package/templates/libs/storage-strategies/firebase/tsconfig.json +24 -0
  194. package/templates/libs/storage-strategies/firebase/tsconfig.lib.json +23 -0
  195. package/templates/libs/storage-strategies/firebase/tsconfig.spec.json +22 -0
  196. package/templates/libs/storage-strategies/firebase/vitest.config.mts +22 -0
  197. package/templates/libs/storage-strategies/supabase/README.md +11 -0
  198. package/templates/libs/storage-strategies/supabase/eslint.config.mjs +22 -0
  199. package/templates/libs/storage-strategies/supabase/package.json +16 -0
  200. package/templates/libs/storage-strategies/supabase/project.json +19 -0
  201. package/templates/libs/storage-strategies/supabase/src/index.ts +2 -0
  202. package/templates/libs/storage-strategies/supabase/src/lib/__tests__/supabase-storage.contract.unit.test.ts +8 -0
  203. package/templates/libs/storage-strategies/supabase/src/lib/supabase-storage.strategy.ts +53 -0
  204. package/templates/libs/storage-strategies/supabase/src/lib/testing/mock-supabase-storage.ts +78 -0
  205. package/templates/libs/storage-strategies/supabase/tsconfig.json +24 -0
  206. package/templates/libs/storage-strategies/supabase/tsconfig.lib.json +23 -0
  207. package/templates/libs/storage-strategies/supabase/tsconfig.spec.json +22 -0
  208. package/templates/libs/storage-strategies/supabase/vitest.config.mts +22 -0
  209. package/templates/libs/template-shared/README.md +11 -0
  210. package/templates/libs/template-shared/eslint.config.mjs +23 -0
  211. package/templates/libs/template-shared/package.json +22 -0
  212. package/templates/libs/template-shared/project.json +19 -0
  213. package/templates/libs/template-shared/src/index.ts +9 -0
  214. package/templates/libs/template-shared/src/lib/abilities/ability-provider.tsx +19 -0
  215. package/templates/libs/template-shared/src/lib/api/create-api.ts +20 -0
  216. package/templates/libs/template-shared/src/lib/draft/index.ts +1 -0
  217. package/templates/libs/template-shared/src/lib/i18n/create-i18n.ts +42 -0
  218. package/templates/libs/template-shared/src/lib/i18n/keys.ts +30 -0
  219. package/templates/libs/template-shared/src/lib/landing/LandingPage.tsx +68 -0
  220. package/templates/libs/template-shared/src/lib/notify/use-notify.ts +26 -0
  221. package/templates/libs/template-shared/src/lib/stores/auth.store.ts +29 -0
  222. package/templates/libs/template-shared/src/lib/stores/loading.store.ts +13 -0
  223. package/templates/libs/template-shared/tsconfig.json +24 -0
  224. package/templates/libs/template-shared/tsconfig.lib.json +25 -0
  225. package/templates/libs/template-shared/tsconfig.spec.json +22 -0
  226. package/templates/libs/template-shared/vitest.config.mts +22 -0
  227. package/templates/libs/upload-client/README.md +11 -0
  228. package/templates/libs/upload-client/eslint.config.mjs +22 -0
  229. package/templates/libs/upload-client/package.json +15 -0
  230. package/templates/libs/upload-client/project.json +19 -0
  231. package/templates/libs/upload-client/src/index.ts +2 -0
  232. package/templates/libs/upload-client/src/lib/upload-client.module.ts +25 -0
  233. package/templates/libs/upload-client/src/lib/upload-client.service.ts +38 -0
  234. package/templates/libs/upload-client/tsconfig.json +24 -0
  235. package/templates/libs/upload-client/tsconfig.lib.json +26 -0
  236. package/templates/libs/upload-client/tsconfig.spec.json +22 -0
  237. package/templates/libs/upload-client/vitest.config.mts +22 -0
  238. package/templates/nx.json +113 -0
  239. package/templates/package.json +24 -0
  240. package/templates/tools/create-icore/_template-shell/package.json +24 -0
  241. package/templates/tsconfig.base.json +29 -0
@@ -0,0 +1,22 @@
1
+ import baseConfig from '../../../eslint.config.mjs';
2
+
3
+ export default [
4
+ ...baseConfig,
5
+ {
6
+ files: ['**/*.json'],
7
+ rules: {
8
+ '@nx/dependency-checks': [
9
+ 'error',
10
+ {
11
+ ignoredFiles: [
12
+ '{projectRoot}/eslint.config.{js,cjs,mjs,ts,cts,mts}',
13
+ '{projectRoot}/vitest.config.{js,ts,mjs,mts}',
14
+ ],
15
+ },
16
+ ],
17
+ },
18
+ languageOptions: {
19
+ parser: await import('jsonc-eslint-parser'),
20
+ },
21
+ },
22
+ ];
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "@icore/auth-supabase",
3
+ "version": "0.0.1",
4
+ "private": true,
5
+ "type": "commonjs",
6
+ "main": "./src/index.js",
7
+ "types": "./src/index.d.ts",
8
+ "dependencies": {
9
+ "@icore/shared": "*",
10
+ "@supabase/supabase-js": "^2.0.0",
11
+ "tslib": "^2.3.0"
12
+ },
13
+ "devDependencies": {
14
+ "vitest": "^4.0.0"
15
+ }
16
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "auth-supabase",
3
+ "$schema": "../../../node_modules/nx/schemas/project-schema.json",
4
+ "sourceRoot": "libs/auth-strategies/supabase/src",
5
+ "projectType": "library",
6
+ "tags": [],
7
+ "targets": {
8
+ "build": {
9
+ "executor": "@nx/js:tsc",
10
+ "outputs": ["{options.outputPath}"],
11
+ "options": {
12
+ "outputPath": "dist/libs/auth-strategies/supabase",
13
+ "main": "libs/auth-strategies/supabase/src/index.ts",
14
+ "tsConfig": "libs/auth-strategies/supabase/tsconfig.lib.json",
15
+ "assets": ["libs/auth-strategies/supabase/*.md"]
16
+ }
17
+ }
18
+ }
19
+ }
@@ -0,0 +1,2 @@
1
+ export * from './lib/supabase-auth.strategy';
2
+ export * from './lib/testing/mock-supabase';
@@ -0,0 +1,8 @@
1
+ import { runAuthContract } from '@icore/shared';
2
+ import { SupabaseAuthStrategy } from '../supabase-auth.strategy';
3
+ import { createMockSupabaseClient } from '../testing/mock-supabase';
4
+
5
+ runAuthContract('SupabaseAuthStrategy', () => {
6
+ const client = createMockSupabaseClient();
7
+ return new SupabaseAuthStrategy({ client });
8
+ });
@@ -0,0 +1,79 @@
1
+ import type { SupabaseClient } from '@supabase/supabase-js';
2
+ import type { AuthSession, AuthStrategy, VerifiedToken } from '@icore/shared';
3
+
4
+ export interface SupabaseAuthStrategyOptions {
5
+ client: SupabaseClient;
6
+ }
7
+
8
+ export class SupabaseAuthStrategy implements AuthStrategy {
9
+ private readonly client: SupabaseClient;
10
+
11
+ constructor(opts: SupabaseAuthStrategyOptions) {
12
+ this.client = opts.client;
13
+ }
14
+
15
+ async signUp(email: string, password: string): Promise<AuthSession> {
16
+ const { data, error } = await this.client.auth.signUp({ email, password });
17
+ if (error || !data.session) {
18
+ throw new Error(error?.message ?? 'signup_failed');
19
+ }
20
+ return this.toSession(data.session);
21
+ }
22
+
23
+ async signIn(email: string, password: string): Promise<AuthSession> {
24
+ const { data, error } = await this.client.auth.signInWithPassword({ email, password });
25
+ if (error || !data.session) {
26
+ throw new Error(error?.message ?? 'invalid_credentials');
27
+ }
28
+ return this.toSession(data.session);
29
+ }
30
+
31
+ async refresh(refreshToken: string): Promise<AuthSession> {
32
+ const { data, error } = await this.client.auth.refreshSession({ refresh_token: refreshToken });
33
+ if (error || !data.session) {
34
+ throw new Error(error?.message ?? 'invalid_refresh_token');
35
+ }
36
+ return this.toSession(data.session);
37
+ }
38
+
39
+ async verifyToken(token: string): Promise<VerifiedToken> {
40
+ const { data, error } = await this.client.auth.getUser(token);
41
+ if (error || !data.user) {
42
+ throw new Error(error?.message ?? 'invalid_token');
43
+ }
44
+ const meta = (data.user as { app_metadata?: { role?: string } }).app_metadata;
45
+ return {
46
+ uid: data.user.id,
47
+ email: data.user.email,
48
+ role: meta?.role,
49
+ };
50
+ }
51
+
52
+ async setRole(uid: string, role: string): Promise<void> {
53
+ const { error } = await this.client.auth.admin.updateUserById(uid, {
54
+ app_metadata: { role },
55
+ });
56
+ if (error) throw new Error(error.message);
57
+ }
58
+
59
+ async getRole(uid: string): Promise<string | null> {
60
+ const { data, error } = await this.client.auth.admin.getUserById(uid);
61
+ if (error || !data.user) throw new Error(error?.message ?? 'user_missing');
62
+ const meta = data.user.app_metadata as { role?: string } | undefined;
63
+ return meta?.role ?? null;
64
+ }
65
+
66
+ private toSession(s: {
67
+ access_token: string;
68
+ refresh_token: string;
69
+ expires_in: number;
70
+ user: { id: string; email?: string | null } | null;
71
+ }): AuthSession {
72
+ return {
73
+ accessToken: s.access_token,
74
+ refreshToken: s.refresh_token,
75
+ expiresIn: s.expires_in,
76
+ user: { id: s.user?.id ?? '', email: s.user?.email ?? '' },
77
+ };
78
+ }
79
+ }
@@ -0,0 +1,107 @@
1
+ import type { SupabaseClient } from '@supabase/supabase-js';
2
+
3
+ interface FakeUser {
4
+ id: string;
5
+ email: string;
6
+ password: string;
7
+ role?: string;
8
+ }
9
+
10
+ interface FakeSession {
11
+ access_token: string;
12
+ refresh_token: string;
13
+ expires_in: number;
14
+ user: { id: string; email: string };
15
+ }
16
+
17
+ export function createMockSupabaseClient(): SupabaseClient {
18
+ const users = new Map<string, FakeUser>();
19
+ const accessToUid = new Map<string, string>();
20
+ const refreshToUid = new Map<string, string>();
21
+
22
+ function issueSession(user: FakeUser): FakeSession {
23
+ const access_token = `at_${user.id}_${accessToUid.size}_${Math.random()}`;
24
+ const refresh_token = `rt_${user.id}_${refreshToUid.size}_${Math.random()}`;
25
+ accessToUid.set(access_token, user.id);
26
+ refreshToUid.set(refresh_token, user.id);
27
+ return {
28
+ access_token,
29
+ refresh_token,
30
+ expires_in: 3600,
31
+ user: { id: user.id, email: user.email },
32
+ };
33
+ }
34
+
35
+ function findById(uid: string): FakeUser | undefined {
36
+ for (const u of users.values()) if (u.id === uid) return u;
37
+ return undefined;
38
+ }
39
+
40
+ const auth = {
41
+ async signUp({ email, password }: { email: string; password: string }) {
42
+ for (const u of users.values()) {
43
+ if (u.email === email) {
44
+ return { data: { user: null, session: null }, error: { message: 'user exists' } };
45
+ }
46
+ }
47
+ const user: FakeUser = { id: `uid_${users.size + 1}`, email, password };
48
+ users.set(user.id, user);
49
+ const session = issueSession(user);
50
+ return { data: { user: session.user, session }, error: null };
51
+ },
52
+ async signInWithPassword({ email, password }: { email: string; password: string }) {
53
+ for (const u of users.values()) {
54
+ if (u.email === email && u.password === password) {
55
+ const session = issueSession(u);
56
+ return { data: { user: session.user, session }, error: null };
57
+ }
58
+ }
59
+ return { data: { user: null, session: null }, error: { message: 'invalid credentials' } };
60
+ },
61
+ async refreshSession({ refresh_token }: { refresh_token: string }) {
62
+ const uid = refreshToUid.get(refresh_token);
63
+ if (!uid)
64
+ return { data: { session: null, user: null }, error: { message: 'invalid refresh' } };
65
+ refreshToUid.delete(refresh_token); // rotation
66
+ const user = findById(uid);
67
+ if (!user) return { data: { session: null, user: null }, error: { message: 'user missing' } };
68
+ const session = issueSession(user);
69
+ return { data: { user: session.user, session }, error: null };
70
+ },
71
+ async getUser(token?: string) {
72
+ if (!token) return { data: { user: null }, error: { message: 'missing token' } };
73
+ const uid = accessToUid.get(token);
74
+ if (!uid) return { data: { user: null }, error: { message: 'invalid token' } };
75
+ const user = findById(uid);
76
+ if (!user) return { data: { user: null }, error: { message: 'user missing' } };
77
+ return {
78
+ data: {
79
+ user: { id: user.id, email: user.email, app_metadata: { role: user.role } },
80
+ },
81
+ error: null,
82
+ };
83
+ },
84
+ admin: {
85
+ async updateUserById(uid: string, updates: { app_metadata?: { role?: string } }) {
86
+ const user = findById(uid);
87
+ if (!user) return { data: { user: null }, error: { message: 'user missing' } };
88
+ if (updates.app_metadata && typeof updates.app_metadata.role === 'string') {
89
+ user.role = updates.app_metadata.role;
90
+ }
91
+ return { data: { user: { id: user.id, email: user.email } }, error: null };
92
+ },
93
+ async getUserById(uid: string) {
94
+ const user = findById(uid);
95
+ if (!user) return { data: { user: null }, error: { message: 'user missing' } };
96
+ return {
97
+ data: {
98
+ user: { id: user.id, email: user.email, app_metadata: { role: user.role } },
99
+ },
100
+ error: null,
101
+ };
102
+ },
103
+ },
104
+ };
105
+
106
+ return { auth } as unknown as SupabaseClient;
107
+ }
@@ -0,0 +1,24 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "module": "node16",
5
+ "moduleResolution": "node16",
6
+ "forceConsistentCasingInFileNames": true,
7
+ "strict": true,
8
+ "importHelpers": true,
9
+ "noImplicitOverride": true,
10
+ "noImplicitReturns": true,
11
+ "noFallthroughCasesInSwitch": true,
12
+ "noPropertyAccessFromIndexSignature": true
13
+ },
14
+ "files": [],
15
+ "include": [],
16
+ "references": [
17
+ {
18
+ "path": "./tsconfig.lib.json"
19
+ },
20
+ {
21
+ "path": "./tsconfig.spec.json"
22
+ }
23
+ ]
24
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../../../dist/out-tsc",
5
+ "declaration": true,
6
+ "types": ["node"]
7
+ },
8
+ "include": ["src/**/*.ts"],
9
+ "exclude": [
10
+ "vite.config.ts",
11
+ "vite.config.mts",
12
+ "vitest.config.ts",
13
+ "vitest.config.mts",
14
+ "src/**/*.test.ts",
15
+ "src/**/*.spec.ts",
16
+ "src/**/*.test.tsx",
17
+ "src/**/*.spec.tsx",
18
+ "src/**/*.test.js",
19
+ "src/**/*.spec.js",
20
+ "src/**/*.test.jsx",
21
+ "src/**/*.spec.jsx"
22
+ ]
23
+ }
@@ -0,0 +1,22 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../../../dist/out-tsc",
5
+ "types": ["vitest/globals", "vitest/importMeta", "vite/client", "node", "vitest"]
6
+ },
7
+ "include": [
8
+ "vite.config.ts",
9
+ "vite.config.mts",
10
+ "vitest.config.ts",
11
+ "vitest.config.mts",
12
+ "src/**/*.test.ts",
13
+ "src/**/*.spec.ts",
14
+ "src/**/*.test.tsx",
15
+ "src/**/*.spec.tsx",
16
+ "src/**/*.test.js",
17
+ "src/**/*.spec.js",
18
+ "src/**/*.test.jsx",
19
+ "src/**/*.spec.jsx",
20
+ "src/**/*.d.ts"
21
+ ]
22
+ }
@@ -0,0 +1,22 @@
1
+ import { defineConfig } from 'vitest/config';
2
+ import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
3
+ import { nxCopyAssetsPlugin } from '@nx/vite/plugins/nx-copy-assets.plugin';
4
+
5
+ export default defineConfig(() => ({
6
+ root: __dirname,
7
+ cacheDir: '../../../node_modules/.vite/libs/auth-strategies/supabase',
8
+ plugins: [nxViteTsPaths(), nxCopyAssetsPlugin(['*.md'])],
9
+ test: {
10
+ name: 'auth-supabase',
11
+ watch: false,
12
+ globals: true,
13
+ environment: 'node',
14
+ include: ['{src,tests}/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
15
+ passWithNoTests: true,
16
+ reporters: ['default'],
17
+ coverage: {
18
+ reportsDirectory: '../../../coverage/libs/auth-strategies/supabase',
19
+ provider: 'v8' as const,
20
+ },
21
+ },
22
+ }));
@@ -0,0 +1,11 @@
1
+ # shared
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build shared` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test shared` to execute the unit tests via [Vitest](https://vitest.dev/).
@@ -0,0 +1,24 @@
1
+ import baseConfig from '../../eslint.config.mjs';
2
+
3
+ export default [
4
+ ...baseConfig,
5
+ {
6
+ files: ['**/*.json'],
7
+ rules: {
8
+ '@nx/dependency-checks': [
9
+ 'error',
10
+ {
11
+ ignoredFiles: [
12
+ '{projectRoot}/eslint.config.{js,cjs,mjs,ts,cts,mts}',
13
+ '{projectRoot}/vitest.config.{js,ts,mjs,mts}',
14
+ '{projectRoot}/src/**/*.{spec,test}.{js,ts,jsx,tsx}',
15
+ '{projectRoot}/src/**/__tests__/**/*.{js,ts,jsx,tsx}',
16
+ ],
17
+ },
18
+ ],
19
+ },
20
+ languageOptions: {
21
+ parser: await import('jsonc-eslint-parser'),
22
+ },
23
+ },
24
+ ];
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "@icore/shared",
3
+ "version": "0.0.1",
4
+ "private": true,
5
+ "type": "commonjs",
6
+ "main": "./src/index.js",
7
+ "types": "./src/index.d.ts",
8
+ "dependencies": {
9
+ "@casl/ability": "^7.0.0",
10
+ "@nestjs/microservices": "^11.0.0",
11
+ "tslib": "^2.3.0",
12
+ "vitest": "^4.0.0"
13
+ }
14
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "shared",
3
+ "$schema": "../../node_modules/nx/schemas/project-schema.json",
4
+ "sourceRoot": "libs/shared/src",
5
+ "projectType": "library",
6
+ "tags": [],
7
+ "targets": {
8
+ "build": {
9
+ "executor": "@nx/js:tsc",
10
+ "outputs": ["{options.outputPath}"],
11
+ "options": {
12
+ "outputPath": "dist/libs/shared",
13
+ "main": "libs/shared/src/index.ts",
14
+ "tsConfig": "libs/shared/tsconfig.lib.json",
15
+ "assets": ["libs/shared/*.md"]
16
+ }
17
+ }
18
+ }
19
+ }
@@ -0,0 +1,58 @@
1
+ import { describe, expect, it, beforeEach, afterEach } from 'vitest';
2
+ import { Transport } from '@nestjs/microservices';
3
+ import { buildTransport } from '../transport';
4
+
5
+ const ORIG = { ...process.env };
6
+
7
+ describe('buildTransport', () => {
8
+ beforeEach(() => {
9
+ for (const k of Object.keys(process.env)) {
10
+ if (k.startsWith('AUTH_')) delete process.env[k];
11
+ }
12
+ });
13
+
14
+ afterEach(() => {
15
+ for (const k of Object.keys(process.env)) {
16
+ if (k.startsWith('AUTH_')) delete process.env[k];
17
+ }
18
+ Object.assign(process.env, ORIG);
19
+ });
20
+
21
+ it('defaults to TCP when ${PREFIX}_TRANSPORT is unset', () => {
22
+ process.env.AUTH_HOST = '127.0.0.1';
23
+ process.env.AUTH_PORT = '4001';
24
+ const opts = buildTransport('AUTH');
25
+ expect(opts.transport).toBe(Transport.TCP);
26
+ const tcp = opts.options as { host: string; port: number };
27
+ expect(tcp.host).toBe('127.0.0.1');
28
+ expect(tcp.port).toBe(4001);
29
+ });
30
+
31
+ it('selects Redis when ${PREFIX}_TRANSPORT=redis', () => {
32
+ process.env.AUTH_TRANSPORT = 'redis';
33
+ process.env.AUTH_REDIS_URL = 'redis://localhost:6379';
34
+ const opts = buildTransport('AUTH');
35
+ expect(opts.transport).toBe(Transport.REDIS);
36
+ const redis = opts.options as { url: string };
37
+ expect(redis.url).toBe('redis://localhost:6379');
38
+ });
39
+
40
+ it('selects NATS when ${PREFIX}_TRANSPORT=nats', () => {
41
+ process.env.AUTH_TRANSPORT = 'nats';
42
+ process.env.AUTH_NATS_URL = 'nats://localhost:4222,nats://localhost:4223';
43
+ const opts = buildTransport('AUTH');
44
+ expect(opts.transport).toBe(Transport.NATS);
45
+ const nats = opts.options as { servers: string[] };
46
+ expect(nats.servers).toEqual(['nats://localhost:4222', 'nats://localhost:4223']);
47
+ });
48
+
49
+ it('throws on unknown transport', () => {
50
+ process.env.AUTH_TRANSPORT = 'sqs';
51
+ expect(() => buildTransport('AUTH')).toThrow(/sqs/);
52
+ });
53
+
54
+ it('throws when a required env var is missing', () => {
55
+ process.env.AUTH_TRANSPORT = 'redis';
56
+ expect(() => buildTransport('AUTH')).toThrow(/AUTH_REDIS_URL/);
57
+ });
58
+ });
@@ -0,0 +1,28 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { defineAbilitiesFor, emptyAbility } from '../ability';
3
+
4
+ describe('defineAbilitiesFor', () => {
5
+ it('grants admin manage on all', () => {
6
+ const ability = defineAbilitiesFor({ id: 'u1', role: 'admin' });
7
+ expect(ability.can('manage', 'all')).toBe(true);
8
+ expect(ability.can('delete', 'User')).toBe(true);
9
+ });
10
+
11
+ it('denies regular user by default', () => {
12
+ const ability = defineAbilitiesFor({ id: 'u2', role: 'user' });
13
+ expect(ability.can('manage', 'all')).toBe(false);
14
+ expect(ability.can('read', 'User')).toBe(false);
15
+ });
16
+
17
+ it('denies everything for null user', () => {
18
+ const ability = defineAbilitiesFor(null);
19
+ expect(ability.can('read', 'Profile')).toBe(false);
20
+ });
21
+ });
22
+
23
+ describe('emptyAbility', () => {
24
+ it('denies everything', () => {
25
+ const ability = emptyAbility();
26
+ expect(ability.can('manage', 'all')).toBe(false);
27
+ });
28
+ });
@@ -0,0 +1,21 @@
1
+ import { AbilityBuilder, createMongoAbility, type MongoAbility } from '@casl/ability';
2
+ import type { AbilityAction, AbilitySubject } from './subjects';
3
+
4
+ export type AppAbility = MongoAbility<[AbilityAction, AbilitySubject]>;
5
+
6
+ export interface AbilityUser {
7
+ id: string;
8
+ role: 'admin' | 'user';
9
+ }
10
+
11
+ export function defineAbilitiesFor(user: AbilityUser | null): AppAbility {
12
+ const { can, build } = new AbilityBuilder<AppAbility>(createMongoAbility);
13
+ if (user?.role === 'admin') {
14
+ can('manage', 'all');
15
+ }
16
+ return build();
17
+ }
18
+
19
+ export function emptyAbility(): AppAbility {
20
+ return createMongoAbility<[AbilityAction, AbilitySubject]>([]);
21
+ }
@@ -0,0 +1,2 @@
1
+ export * from './ability';
2
+ export * from './subjects';
@@ -0,0 +1,2 @@
1
+ export type AbilityAction = 'manage' | 'create' | 'read' | 'update' | 'delete';
2
+ export type AbilitySubject = 'all' | 'User' | 'Profile';
@@ -0,0 +1,3 @@
1
+ export * from './abilities';
2
+ export * from './strategies';
3
+ export * from './transport';
@@ -0,0 +1,4 @@
1
+ import { FakeAuthStrategy } from '../fakes/fake-auth';
2
+ import { runAuthContract } from '../contract/auth-contract';
3
+
4
+ runAuthContract('FakeAuthStrategy', () => new FakeAuthStrategy());
@@ -0,0 +1,4 @@
1
+ import { FakeStorageStrategy } from '../fakes/fake-storage';
2
+ import { runStorageContract } from '../contract/storage-contract';
3
+
4
+ runStorageContract('FakeStorageStrategy', () => new FakeStorageStrategy());
@@ -0,0 +1,21 @@
1
+ export interface AuthSession {
2
+ accessToken: string;
3
+ refreshToken: string;
4
+ expiresIn: number;
5
+ user: { id: string; email: string };
6
+ }
7
+
8
+ export interface VerifiedToken {
9
+ uid: string;
10
+ email?: string;
11
+ role?: string;
12
+ }
13
+
14
+ export interface AuthStrategy {
15
+ verifyToken(token: string): Promise<VerifiedToken>;
16
+ signIn(email: string, password: string): Promise<AuthSession>;
17
+ signUp(email: string, password: string): Promise<AuthSession>;
18
+ refresh(refreshToken: string): Promise<AuthSession>;
19
+ setRole(uid: string, role: string): Promise<void>;
20
+ getRole(uid: string): Promise<string | null>;
21
+ }
@@ -0,0 +1,66 @@
1
+ import { describe, expect, it, beforeEach } from 'vitest';
2
+ import type { AuthStrategy } from '../auth';
3
+
4
+ export function runAuthContract(name: string, factory: () => AuthStrategy): void {
5
+ describe(`AuthStrategy contract: ${name}`, () => {
6
+ let strategy: AuthStrategy;
7
+
8
+ beforeEach(() => {
9
+ strategy = factory();
10
+ });
11
+
12
+ it('signUp returns a session for new user', async () => {
13
+ const session = await strategy.signUp('a@x.com', 'pw12345!');
14
+ expect(session.accessToken).toBeTruthy();
15
+ expect(session.refreshToken).toBeTruthy();
16
+ expect(session.user.email).toBe('a@x.com');
17
+ });
18
+
19
+ it('signIn returns a session after signUp', async () => {
20
+ await strategy.signUp('b@x.com', 'pw12345!');
21
+ const session = await strategy.signIn('b@x.com', 'pw12345!');
22
+ expect(session.user.email).toBe('b@x.com');
23
+ });
24
+
25
+ it('verifyToken resolves the uid from a signUp token', async () => {
26
+ const session = await strategy.signUp('c@x.com', 'pw12345!');
27
+ const verified = await strategy.verifyToken(session.accessToken);
28
+ expect(verified.uid).toBe(session.user.id);
29
+ });
30
+
31
+ it('verifyToken rejects bogus token', async () => {
32
+ await expect(strategy.verifyToken('not-a-token')).rejects.toThrow();
33
+ });
34
+
35
+ it('refresh issues a new session for the same user', async () => {
36
+ const first = await strategy.signUp('d@x.com', 'pw12345!');
37
+ const next = await strategy.refresh(first.refreshToken);
38
+ expect(next.user.id).toBe(first.user.id);
39
+ });
40
+
41
+ it('used refresh token is rejected after rotation', async () => {
42
+ const first = await strategy.signUp('f@x.com', 'pw12345!');
43
+ await strategy.refresh(first.refreshToken);
44
+ await expect(strategy.refresh(first.refreshToken)).rejects.toThrow();
45
+ });
46
+
47
+ it('setRole writes a role visible on verifyToken', async () => {
48
+ const session = await strategy.signUp('e@x.com', 'pw12345!');
49
+ await strategy.setRole(session.user.id, 'admin');
50
+ const reLogged = await strategy.signIn('e@x.com', 'pw12345!');
51
+ const verified = await strategy.verifyToken(reLogged.accessToken);
52
+ expect(verified.role).toBe('admin');
53
+ });
54
+
55
+ it('getRole returns null when no role has been set', async () => {
56
+ const session = await strategy.signUp('g@x.com', 'pw12345!');
57
+ expect(await strategy.getRole(session.user.id)).toBeNull();
58
+ });
59
+
60
+ it('getRole returns the role most recently set via setRole', async () => {
61
+ const session = await strategy.signUp('h@x.com', 'pw12345!');
62
+ await strategy.setRole(session.user.id, 'admin');
63
+ expect(await strategy.getRole(session.user.id)).toBe('admin');
64
+ });
65
+ });
66
+ }