@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.
- package/app/composables/useAuth.ts +47 -38
- package/app/types/shims-mframework-api.d.ts +4 -0
- package/app/utils/plugins.ts +37 -19
- package/package.json +1 -2
- package/tsconfig.json +15 -3
|
@@ -1,20 +1,8 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
} from '
|
|
4
|
-
import
|
|
5
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
64
|
-
|
|
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
|
package/app/utils/plugins.ts
CHANGED
|
@@ -1,24 +1,42 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
adminClient
|
|
21
|
-
polarClient
|
|
22
|
-
|
|
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.
|
|
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": "
|
|
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
|
}
|