@boostkit/auth 0.0.1
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/LICENSE +21 -0
- package/README.md +38 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +56 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 BoostKit
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# @boostkit/auth
|
|
2
|
+
|
|
3
|
+
Shared authentication result and entity types.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @boostkit/auth
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import type { AuthUser, AuthSession, AuthResult } from '@boostkit/auth'
|
|
15
|
+
|
|
16
|
+
const user: AuthUser = {
|
|
17
|
+
id: '1',
|
|
18
|
+
name: 'A',
|
|
19
|
+
email: 'a@example.com',
|
|
20
|
+
emailVerified: false,
|
|
21
|
+
createdAt: new Date(),
|
|
22
|
+
updatedAt: new Date(),
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## API Reference
|
|
27
|
+
|
|
28
|
+
- `AuthUser`
|
|
29
|
+
- `AuthSession`
|
|
30
|
+
- `AuthResult`
|
|
31
|
+
|
|
32
|
+
## Configuration
|
|
33
|
+
|
|
34
|
+
This package has no runtime config object.
|
|
35
|
+
|
|
36
|
+
## Notes
|
|
37
|
+
|
|
38
|
+
- This package exports types/interfaces only.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { ServiceProvider, type Application } from '@boostkit/core';
|
|
2
|
+
export interface AuthUser {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
email: string;
|
|
6
|
+
emailVerified: boolean;
|
|
7
|
+
image?: string;
|
|
8
|
+
createdAt: Date;
|
|
9
|
+
updatedAt: Date;
|
|
10
|
+
}
|
|
11
|
+
export interface AuthSession {
|
|
12
|
+
id: string;
|
|
13
|
+
userId: string;
|
|
14
|
+
token: string;
|
|
15
|
+
expiresAt: Date;
|
|
16
|
+
ipAddress?: string;
|
|
17
|
+
userAgent?: string;
|
|
18
|
+
createdAt: Date;
|
|
19
|
+
updatedAt: Date;
|
|
20
|
+
}
|
|
21
|
+
export interface AuthResult {
|
|
22
|
+
user: AuthUser;
|
|
23
|
+
session: AuthSession;
|
|
24
|
+
}
|
|
25
|
+
export interface BetterAuthConfig {
|
|
26
|
+
/** 32+ character secret. Falls back to process.env.AUTH_SECRET */
|
|
27
|
+
secret?: string;
|
|
28
|
+
/** App base URL. Falls back to process.env.APP_URL */
|
|
29
|
+
baseUrl?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Pass a PrismaClient instance — auto-wrapped with prismaAdapter internally.
|
|
32
|
+
* Or pass a pre-built better-auth database adapter for advanced use.
|
|
33
|
+
*/
|
|
34
|
+
database: unknown;
|
|
35
|
+
/** Required when passing a PrismaClient. Default: 'sqlite' */
|
|
36
|
+
databaseProvider?: 'sqlite' | 'postgresql' | 'mysql';
|
|
37
|
+
emailAndPassword?: {
|
|
38
|
+
enabled?: boolean;
|
|
39
|
+
requireEmailVerification?: boolean;
|
|
40
|
+
};
|
|
41
|
+
socialProviders?: Record<string, {
|
|
42
|
+
clientId: string;
|
|
43
|
+
clientSecret: string;
|
|
44
|
+
}>;
|
|
45
|
+
trustedOrigins?: string[];
|
|
46
|
+
/** Called after a new user is successfully created — ideal for dispatching jobs or events */
|
|
47
|
+
onUserCreated?: (user: {
|
|
48
|
+
id: string;
|
|
49
|
+
name: string;
|
|
50
|
+
email: string;
|
|
51
|
+
}) => void | Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns a ServiceProvider constructor that configures better-auth and
|
|
55
|
+
* binds the auth instance to the DI container in boot().
|
|
56
|
+
*
|
|
57
|
+
* Usage in bootstrap/providers.ts:
|
|
58
|
+
* import { betterAuth } from '@boostkit/auth'
|
|
59
|
+
* import configs from '../config/index.ts'
|
|
60
|
+
* export default [..., betterAuth(configs.auth), ...]
|
|
61
|
+
*/
|
|
62
|
+
export declare function betterAuth(config: BetterAuthConfig): new (app: Application) => ServiceProvider;
|
|
63
|
+
export type BetterAuthInstance = Awaited<ReturnType<typeof import('better-auth').betterAuth>>;
|
|
64
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
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,CAAC,CAAA;IAC5E,cAAc,CAAC,EAAE,MAAM,EAAE,CAAA;IACzB,6FAA6F;IAC7F,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CAC5F;AAID;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,gBAAgB,GAAG,KAAK,GAAG,EAAE,WAAW,KAAK,eAAe,CAiD9F;AAED,MAAM,MAAM,kBAAkB,GAAG,OAAO,CAAC,UAAU,CAAC,cAAc,aAAa,EAAE,UAAU,CAAC,CAAC,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { ServiceProvider } from '@boostkit/core';
|
|
2
|
+
// ─── betterAuth() Factory ──────────────────────────────────
|
|
3
|
+
/**
|
|
4
|
+
* Returns a ServiceProvider constructor that configures better-auth and
|
|
5
|
+
* binds the auth instance to the DI container in boot().
|
|
6
|
+
*
|
|
7
|
+
* Usage in bootstrap/providers.ts:
|
|
8
|
+
* import { betterAuth } from '@boostkit/auth'
|
|
9
|
+
* import configs from '../config/index.ts'
|
|
10
|
+
* export default [..., betterAuth(configs.auth), ...]
|
|
11
|
+
*/
|
|
12
|
+
export function betterAuth(config) {
|
|
13
|
+
class BetterAuthProvider extends ServiceProvider {
|
|
14
|
+
register() { }
|
|
15
|
+
async boot() {
|
|
16
|
+
const { betterAuth: createAuth } = await import('better-auth');
|
|
17
|
+
// Resolve database if it's a Promise (supports async factory pattern)
|
|
18
|
+
let database = config.database instanceof Promise ? await config.database : config.database;
|
|
19
|
+
if (database && typeof database['$connect'] === 'function') {
|
|
20
|
+
const { prismaAdapter } = await import('better-auth/adapters/prisma');
|
|
21
|
+
database = prismaAdapter(database, {
|
|
22
|
+
provider: config.databaseProvider ?? 'sqlite',
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
const auth = createAuth({
|
|
26
|
+
secret: config.secret ?? process.env['AUTH_SECRET'] ?? '',
|
|
27
|
+
baseURL: config.baseUrl ?? process.env['APP_URL'] ?? 'http://localhost:3000',
|
|
28
|
+
database: database,
|
|
29
|
+
emailAndPassword: {
|
|
30
|
+
enabled: config.emailAndPassword?.enabled ?? true,
|
|
31
|
+
...(config.emailAndPassword?.requireEmailVerification !== undefined
|
|
32
|
+
? { requireEmailVerification: config.emailAndPassword.requireEmailVerification }
|
|
33
|
+
: {}),
|
|
34
|
+
},
|
|
35
|
+
...(config.socialProviders && { socialProviders: config.socialProviders }),
|
|
36
|
+
...(config.trustedOrigins && { trustedOrigins: config.trustedOrigins }),
|
|
37
|
+
...(config.onUserCreated && {
|
|
38
|
+
databaseHooks: {
|
|
39
|
+
user: {
|
|
40
|
+
create: {
|
|
41
|
+
after: async (user) => {
|
|
42
|
+
await config.onUserCreated(user);
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
}),
|
|
48
|
+
});
|
|
49
|
+
// Bind into DI container
|
|
50
|
+
this.app.instance('auth', auth);
|
|
51
|
+
console.log('[BetterAuthServiceProvider] booted — auth instance bound to DI container');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return BetterAuthProvider;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAoB,MAAM,gBAAgB,CAAA;AAmDlE,8DAA8D;AAE9D;;;;;;;;GAQG;AACH,MAAM,UAAU,UAAU,CAAC,MAAwB;IACjD,MAAM,kBAAmB,SAAQ,eAAe;QAC9C,QAAQ,KAAU,CAAC;QAEnB,KAAK,CAAC,IAAI;YACR,MAAM,EAAE,UAAU,EA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AAM,CAAC,aAAa,IAAI;oBAC1B,aAAa,EAAE;wBACb,IAAI,EAAE;4BACJ,MAAM,EAAE;gCACN,KAAK,EAAE,KAAK,EAAE,IAAiD,EAAE,EAAE;oCACjE,MAAM,MAAM,CAAC,aAAc,CAAC,IAAI,CAAC,CAAA;gCACnC,CAAC;6BACF;yBACF;qBACF;iBACF,CAAC;aACH,CAAC,CAAA;YAEF,yBAAyB;YACzB,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;YAE/B,OAAO,CAAC,GAAG,CAAC,0EAA0E,CAAC,CAAA;QACzF,CAAC;KACF;IAED,OAAO,kBAAkB,CAAA;AAC3B,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@boostkit/auth",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/boostkitjs/boostkit",
|
|
8
|
+
"directory": "packages/auth"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"better-auth": "^1.0.0",
|
|
24
|
+
"@boostkit/core": "0.0.1",
|
|
25
|
+
"@boostkit/router": "0.0.1"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^20.0.0",
|
|
29
|
+
"typescript": "^5.4.0"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc",
|
|
33
|
+
"dev": "tsc --watch",
|
|
34
|
+
"typecheck": "tsc --noEmit",
|
|
35
|
+
"clean": "rm -rf dist"
|
|
36
|
+
}
|
|
37
|
+
}
|