@mframework/layer-auth 0.0.1 → 0.0.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.
@@ -1,20 +1,8 @@
1
- import type {
2
- Subscription
3
- } from '@better-auth/stripe'
4
- import type {
5
- CustomerState
6
- } from '@polar-sh/sdk/models/components/customerstate.js'
7
- import type {
8
- BetterAuthClientOptions,
9
- InferSessionFromClient,
10
- User
11
- } from 'better-auth/client'
12
- import {
13
- createAuthClient
14
- } from 'better-auth/client'
15
- import {
16
- getAuthPlugins
17
- } from '../utils/plugins'
1
+ import type { Subscription } from '@better-auth/stripe'
2
+ import type { CustomerState } from '@polar-sh/sdk/models/components/customerstate.js'
3
+ import type { BetterAuthClientOptions, InferSessionFromClient, User } from 'better-auth/client'
4
+ import { createAuthClient } from 'better-auth/client'
5
+ import { getAuthPlugins } from '../utils/plugins'
18
6
 
19
7
  export type UseAuthOptions = {
20
8
  /** An existing auth client. If provided, it's used as-is. */
@@ -41,29 +29,50 @@ export function useAuth(options: UseAuthOptions = {}) {
41
29
  reload
42
30
  } = options
43
31
 
44
- const client = providedClient || createAuthClient({
45
- baseURL: baseURL || undefined,
46
- fetchOptions: {
47
- headers
48
- },
49
- socialProviders: {
50
- github: {
51
- clientId: process.env.GITHUB_CLIENT_ID!,
52
- clientSecret: process.env.GITHUB_CLIENT_SECRET!
53
- },
54
- microsoft: {
55
- clientId: process.env.MICROSOFT_CLIENT_ID!,
56
- clientSecret: process.env.MICROSOFT_CLIENT_SECRET!
57
- },
58
- twitter: {
59
- clientId: process.env.TWITTER_CLIENT_ID!,
60
- clientSecret: process.env.TWITTER_CLIENT_SECRET!
32
+ // Resolve a client in this order:
33
+ // 1. providedClient (explicit)
34
+ // 2. adapter provider (if adapter-betterauth is present)
35
+ // 3. local createAuthClient fallback
36
+ let client: any = providedClient || null
37
+
38
+ if (!client) {
39
+ // Attempt to use adapter registry/provider if available
40
+ try {
41
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
42
+ const adapter = require('@mframework/adapter-betterauth')
43
+ if (adapter && typeof adapter.getAuthProvider === 'function') {
44
+ const prov = adapter.getAuthProvider()
45
+ // Wrap provider methods into a minimal client-like interface
46
+ client = {
47
+ async getSession() {
48
+ const s = await prov.session()
49
+ return { data: { session: s, user: (s && s.user) || null } }
50
+ },
51
+ signIn: prov.login?.bind(prov),
52
+ signUp: prov.register?.bind(prov),
53
+ signOut: prov.logout?.bind(prov),
54
+ refresh: prov.refresh?.bind(prov),
55
+ subscription: { list: async () => ({ data: [] }) },
56
+ $ERROR_CODES: {},
57
+ }
61
58
  }
62
- },
63
- plugins: getAuthPlugins({
64
- subscription: true
59
+ } catch (e) {
60
+ // adapter-betterauth not available — fall back to client library below
61
+ }
62
+ }
63
+
64
+ if (!client) {
65
+ client = createAuthClient({
66
+ baseURL: baseURL || undefined,
67
+ fetchOptions: { headers },
68
+ socialProviders: {
69
+ github: { clientId: process.env.GITHUB_CLIENT_ID!, clientSecret: process.env.GITHUB_CLIENT_SECRET! },
70
+ microsoft: { clientId: process.env.MICROSOFT_CLIENT_ID!, clientSecret: process.env.MICROSOFT_CLIENT_SECRET! },
71
+ twitter: { clientId: process.env.TWITTER_CLIENT_ID!, clientSecret: process.env.TWITTER_CLIENT_SECRET! }
72
+ },
73
+ plugins: getAuthPlugins({ subscription: true })
65
74
  })
66
- })
75
+ }
67
76
 
68
77
  let session: InferSessionFromClient < BetterAuthClientOptions > | null = null
69
78
  let user: User | null = null
@@ -0,0 +1,4 @@
1
+ declare module '@mframework/api' {
2
+ export const prisma: any
3
+ export default prisma
4
+ }
@@ -1,24 +1,42 @@
1
- import { stripeClient } from '@better-auth/stripe/client'
2
- import { polarClient, adminClient, inferAdditionalFields } from '@mframework/adapter-betterauth/'
3
-
4
- export type AuthPluginOptions = {
5
- /** whether to enable subscription support for stripe plugin */
6
- subscription?: boolean
7
- }
1
+ export type AuthPluginOptions = { subscription?: boolean }
8
2
 
9
3
  export function getAuthPlugins(opts: AuthPluginOptions = {}): any[] {
10
4
  const { subscription = true } = opts
11
5
 
12
- return [
13
- inferAdditionalFields({
14
- user: {
15
- polarCustomerId: {
16
- type: 'string'
17
- }
18
- }
19
- }),
20
- adminClient(),
21
- polarClient(),
22
- stripeClient({ subscription })
23
- ]
6
+ const plugins: any[] = []
7
+
8
+ // Try to load adapter-provided plugin helpers when available, otherwise
9
+ // return an empty plugin list (safer for build-time without adapters).
10
+ try {
11
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
12
+ const adapter = require('@mframework/adapter-betterauth')
13
+ const inferAdditionalFields = adapter.inferAdditionalFields || adapter.default?.inferAdditionalFields
14
+ const adminClient = adapter.adminClient || adapter.default?.adminClient
15
+ const polarClient = adapter.polarClient || adapter.default?.polarClient
16
+
17
+ if (inferAdditionalFields) {
18
+ plugins.push(
19
+ inferAdditionalFields({
20
+ user: { polarCustomerId: { type: 'string' } }
21
+ })
22
+ )
23
+ }
24
+
25
+ if (adminClient) plugins.push(adminClient())
26
+ if (polarClient) plugins.push(polarClient())
27
+ } catch (e) {
28
+ // adapter not present — ignore
29
+ }
30
+
31
+ // Try to load stripe client plugin separately
32
+ try {
33
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
34
+ const stripe = require('@better-auth/stripe/client')
35
+ const stripeClient = stripe?.stripeClient || stripe?.default || stripe
36
+ if (typeof stripeClient === 'function') plugins.push(stripeClient({ subscription }))
37
+ } catch (e) {
38
+ // stripe plugin not available — ignore
39
+ }
40
+
41
+ return plugins
24
42
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mframework/layer-auth",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "The authentication layer for M Framework applications.",
5
5
  "type": "module",
6
6
  "main": "./nuxt.config.ts",
@@ -19,7 +19,6 @@
19
19
  "author": "M Framework",
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
- "@mframework/adapter-betterauth": "^0.0.7",
23
22
  "typescript": "^5.9.3"
24
23
  },
25
24
  "devDependencies": {
package/tsconfig.json CHANGED
@@ -5,12 +5,15 @@
5
5
  "declaration": true,
6
6
  "emitDeclarationOnly": false,
7
7
  "outDir": "dist",
8
- "moduleResolution": "bundler",
9
8
  "module": "ESNext",
10
- "target": "ESNext",
9
+ "target": "ES2017",
10
+ "moduleResolution": "bundler",
11
+ "esModuleInterop": true,
12
+ "allowSyntheticDefaultImports": true,
13
+ "lib": ["ES2017", "DOM"],
11
14
  "strict": true,
12
15
  "jsx": "react-jsx",
13
- "skipLibCheck": true,
16
+ "skipLibCheck": true,
14
17
  "noEmitOnError": false
15
18
  ,
16
19
  "paths": {
@@ -20,4 +23,13 @@
20
23
  "@mframework/api": ["../../packages/modules/api/src/index.ts"]
21
24
  }
22
25
  }
26
+ ,
27
+ "exclude": [
28
+ "node_modules",
29
+ "dist"
30
+ ],
31
+ "include": [
32
+ "app/**/*",
33
+ "server/**/*"
34
+ ]
23
35
  }