@fougere/auth-better 0.12.2-alpha.0 → 0.12.4-alpha.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.
- package/README.md +2 -1
- package/dist/entity/AuthUser.d.ts +4 -4
- package/dist/entity/AuthUser.d.ts.map +1 -1
- package/dist/entity/AuthUser.js +3 -2
- package/dist/entity/AuthUser.js.map +1 -1
- package/dist/entity/AuthVerification.d.ts +3 -3
- package/dist/entity/AuthVerification.d.ts.map +1 -1
- package/dist/entity/AuthVerification.js +2 -1
- package/dist/entity/AuthVerification.js.map +1 -1
- package/dist/entity/authEntities.js +3 -3
- package/dist/entity/authEntities.js.map +1 -1
- package/dist/index.d.ts +7 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +30 -32
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/entity/AuthUser.ts +4 -2
- package/src/entity/AuthVerification.ts +3 -1
- package/src/entity/authEntities.ts +3 -3
- package/src/index.ts +32 -36
package/README.md
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
> Fougere's auth
|
|
3
3
|
A translation layer onto [better-auth](https://better-auth.com): its writes go through
|
|
4
4
|
Fougere's storage, and the application's `User` stays its own — Session and Account are
|
|
5
|
-
built against it.
|
|
5
|
+
built against it. `betterAuth()` is an extension: it brings the frond those rows live in, and
|
|
6
|
+
registers the runtime under `AUTH` once the app exists.
|
|
6
7
|
|
|
7
8
|
## Installation
|
|
8
9
|
```bash
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
declare const
|
|
1
|
+
declare const User_base: import("@fougere/schema").SchemaConstructor<{
|
|
2
2
|
id: import("@fougere/schema").Field<string>;
|
|
3
3
|
name: import("@fougere/schema").Field<string>;
|
|
4
4
|
email: import("@fougere/schema").Field<string>;
|
|
@@ -7,8 +7,8 @@ declare const AuthUser_base: import("@fougere/schema").SchemaConstructor<{
|
|
|
7
7
|
createdAt: import("@fougere/schema").Field<Date>;
|
|
8
8
|
updatedAt: import("@fougere/schema").Field<Date>;
|
|
9
9
|
}>;
|
|
10
|
-
/**
|
|
11
|
-
|
|
10
|
+
/** The user better-auth writes when the app names none — `User`, since the model is `user`. */
|
|
11
|
+
declare class User extends User_base {
|
|
12
12
|
}
|
|
13
|
-
export {};
|
|
13
|
+
export { User as AuthUser };
|
|
14
14
|
//# sourceMappingURL=AuthUser.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthUser.d.ts","sourceRoot":"","sources":["../../src/entity/AuthUser.ts"],"names":[],"mappings":";;;;;;;;;AAEA
|
|
1
|
+
{"version":3,"file":"AuthUser.d.ts","sourceRoot":"","sources":["../../src/entity/AuthUser.ts"],"names":[],"mappings":";;;;;;;;;AAEA,+FAA+F;AAC/F,cAAM,IAAK,SAAQ,SAQjB;CAAG;AAEL,OAAO,EAAE,IAAI,IAAI,QAAQ,EAAE,CAAC"}
|
package/dist/entity/AuthUser.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { entity, primary, text, bool, created, optional } from '@fougere/schema';
|
|
2
|
-
/**
|
|
3
|
-
|
|
2
|
+
/** The user better-auth writes when the app names none — `User`, since the model is `user`. */
|
|
3
|
+
class User extends entity({
|
|
4
4
|
id: primary(),
|
|
5
5
|
name: text(),
|
|
6
6
|
email: text(),
|
|
@@ -10,4 +10,5 @@ export class AuthUser extends entity({
|
|
|
10
10
|
updatedAt: created(),
|
|
11
11
|
}) {
|
|
12
12
|
}
|
|
13
|
+
export { User as AuthUser };
|
|
13
14
|
//# sourceMappingURL=AuthUser.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthUser.js","sourceRoot":"","sources":["../../src/entity/AuthUser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAEjF
|
|
1
|
+
{"version":3,"file":"AuthUser.js","sourceRoot":"","sources":["../../src/entity/AuthUser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAEjF,+FAA+F;AAC/F,MAAM,IAAK,SAAQ,MAAM,CAAC;IACxB,EAAE,EAAE,OAAO,EAAE;IACb,IAAI,EAAE,IAAI,EAAE;IACZ,KAAK,EAAE,IAAI,EAAE;IACb,aAAa,EAAE,IAAI,EAAE;IACrB,KAAK,EAAE,QAAQ,CAAC,IAAI,EAAE,CAAC;IACvB,SAAS,EAAE,OAAO,EAAE;IACpB,SAAS,EAAE,OAAO,EAAE;CACrB,CAAC;CAAG;AAEL,OAAO,EAAE,IAAI,IAAI,QAAQ,EAAE,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
declare const
|
|
1
|
+
declare const Verification_base: import("@fougere/schema").SchemaConstructor<{
|
|
2
2
|
id: import("@fougere/schema").Field<string>;
|
|
3
3
|
identifier: import("@fougere/schema").Field<string>;
|
|
4
4
|
value: import("@fougere/schema").Field<string>;
|
|
@@ -10,7 +10,7 @@ declare const AuthVerification_base: import("@fougere/schema").SchemaConstructor
|
|
|
10
10
|
* Default Verification entity — better-auth shape.
|
|
11
11
|
* Used for email verification, password reset, magic links, etc.
|
|
12
12
|
*/
|
|
13
|
-
|
|
13
|
+
declare class Verification extends Verification_base {
|
|
14
14
|
}
|
|
15
|
-
export {};
|
|
15
|
+
export { Verification as AuthVerification };
|
|
16
16
|
//# sourceMappingURL=AuthVerification.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthVerification.d.ts","sourceRoot":"","sources":["../../src/entity/AuthVerification.ts"],"names":[],"mappings":";;;;;;;;AAEA;;;GAGG;AACH,
|
|
1
|
+
{"version":3,"file":"AuthVerification.d.ts","sourceRoot":"","sources":["../../src/entity/AuthVerification.ts"],"names":[],"mappings":";;;;;;;;AAEA;;;GAGG;AACH,cAAM,YAAa,SAAQ,iBAOzB;CAAG;AAEL,OAAO,EAAE,YAAY,IAAI,gBAAgB,EAAE,CAAC"}
|
|
@@ -3,7 +3,7 @@ import { entity, primary, text, date, created } from '@fougere/schema';
|
|
|
3
3
|
* Default Verification entity — better-auth shape.
|
|
4
4
|
* Used for email verification, password reset, magic links, etc.
|
|
5
5
|
*/
|
|
6
|
-
|
|
6
|
+
class Verification extends entity({
|
|
7
7
|
id: primary(),
|
|
8
8
|
identifier: text(),
|
|
9
9
|
value: text(),
|
|
@@ -12,4 +12,5 @@ export class AuthVerification extends entity({
|
|
|
12
12
|
updatedAt: created(),
|
|
13
13
|
}) {
|
|
14
14
|
}
|
|
15
|
+
export { Verification as AuthVerification };
|
|
15
16
|
//# sourceMappingURL=AuthVerification.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthVerification.js","sourceRoot":"","sources":["../../src/entity/AuthVerification.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAEvE;;;GAGG;AACH,MAAM,
|
|
1
|
+
{"version":3,"file":"AuthVerification.js","sourceRoot":"","sources":["../../src/entity/AuthVerification.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAEvE;;;GAGG;AACH,MAAM,YAAa,SAAQ,MAAM,CAAC;IAChC,EAAE,EAAE,OAAO,EAAE;IACb,UAAU,EAAE,IAAI,EAAE;IAClB,KAAK,EAAE,IAAI,EAAE;IACb,SAAS,EAAE,IAAI,EAAE;IACjB,SAAS,EAAE,OAAO,EAAE;IACpB,SAAS,EAAE,OAAO,EAAE;CACrB,CAAC;CAAG;AAEL,OAAO,EAAE,YAAY,IAAI,gBAAgB,EAAE,CAAC"}
|
|
@@ -5,7 +5,7 @@ export function authEntities(User) {
|
|
|
5
5
|
* Session entity — better-auth shape.
|
|
6
6
|
* `token` carries the opaque session secret (cookie value).
|
|
7
7
|
*/
|
|
8
|
-
class
|
|
8
|
+
class Session extends entity({
|
|
9
9
|
id: primary(),
|
|
10
10
|
userId: ref(target),
|
|
11
11
|
token: text(),
|
|
@@ -20,7 +20,7 @@ export function authEntities(User) {
|
|
|
20
20
|
* Account entity — better-auth shape (single PK, accountId/providerId pair,
|
|
21
21
|
* separate token columns instead of a JSON blob).
|
|
22
22
|
*/
|
|
23
|
-
class
|
|
23
|
+
class Account extends entity({
|
|
24
24
|
id: primary(),
|
|
25
25
|
accountId: text(),
|
|
26
26
|
providerId: text(),
|
|
@@ -36,6 +36,6 @@ export function authEntities(User) {
|
|
|
36
36
|
updatedAt: created(),
|
|
37
37
|
}) {
|
|
38
38
|
}
|
|
39
|
-
return { AuthSession, AuthAccount };
|
|
39
|
+
return { AuthSession: Session, AuthAccount: Account };
|
|
40
40
|
}
|
|
41
41
|
//# sourceMappingURL=authEntities.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authEntities.js","sourceRoot":"","sources":["../../src/entity/authEntities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAmB,MAAM,iBAAiB,CAAC;AAKvG,MAAM,UAAU,YAAY,CAAC,IAAgB;IAI3C,MAAM,MAAM,GAAG,IAA6B,CAAC;IAE7C;;;OAGG;IACH,MAAM,
|
|
1
|
+
{"version":3,"file":"authEntities.js","sourceRoot":"","sources":["../../src/entity/authEntities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAmB,MAAM,iBAAiB,CA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1
|
+
import { type Extension } from '@fougere/core';
|
|
2
|
+
import { type SchemaView } from '@fougere/schema';
|
|
3
3
|
import { type FougereProviders } from './FougereProviders.js';
|
|
4
4
|
export { AuthUser } from './entity/AuthUser.js';
|
|
5
5
|
/** Options accepted by the betterAuth() factory in fougere.config.ts. */
|
|
@@ -15,6 +15,9 @@ export interface BetterAuthOptions {
|
|
|
15
15
|
account?: SchemaView;
|
|
16
16
|
verification?: SchemaView;
|
|
17
17
|
}
|
|
18
|
-
/**
|
|
19
|
-
|
|
18
|
+
/**
|
|
19
|
+
* The auth provider, as an extension: it brings the frond its rows live in — the user too, when
|
|
20
|
+
* the app names none — and registers the runtime under `AUTH` once the app exists.
|
|
21
|
+
*/
|
|
22
|
+
export declare function betterAuth(opts: BetterAuthOptions): Extension;
|
|
20
23
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAA2C,KAAK,SAAS,EAAgB,MAAM,eAAe,CAAC;AACtG,OAAO,EAAc,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAK9D,OAAO,EAA0D,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEtH,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAEhD,yEAAyE;AACzE,MAAM,WAAW,iBAAiB;IAChC,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,gBAAgB,CAAC;IAC7B,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,UAAU,CAAC;IACrB,OAAO,CAAC,EAAE,UAAU,CAAC;IACrB,YAAY,CAAC,EAAE,UAAU,CAAC;CAC3B;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,iBAAiB,GAAG,SAAS,CAkC7D"}
|
package/dist/index.js
CHANGED
|
@@ -1,36 +1,35 @@
|
|
|
1
1
|
import { betterAuth as betterAuthLib } from 'better-auth';
|
|
2
2
|
import { createId } from '@paralleldrive/cuid2';
|
|
3
|
+
import { AUTH, frond } from '@fougere/core';
|
|
4
|
+
import { lowerFirst } from '@fougere/schema';
|
|
3
5
|
import { AuthUser } from './entity/AuthUser.js';
|
|
4
6
|
import { AuthVerification } from './entity/AuthVerification.js';
|
|
5
7
|
import { authEntities } from './entity/authEntities.js';
|
|
6
8
|
import { fougereAdapter } from './adapter.js';
|
|
7
9
|
import { translateCredential, translatePlugins, translateSocial } from './FougereProviders.js';
|
|
8
10
|
export { AuthUser } from './entity/AuthUser.js';
|
|
9
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* The auth provider, as an extension: it brings the frond its rows live in — the user too, when
|
|
13
|
+
* the app names none — and registers the runtime under `AUTH` once the app exists.
|
|
14
|
+
*/
|
|
10
15
|
export function betterAuth(opts) {
|
|
11
|
-
const
|
|
12
|
-
const { AuthSession, AuthAccount } = authEntities(
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
const user = opts.user ?? AuthUser;
|
|
17
|
+
const { AuthSession, AuthAccount } = authEntities(user);
|
|
18
|
+
const models = {
|
|
19
|
+
user,
|
|
20
|
+
session: opts.session ?? AuthSession,
|
|
21
|
+
account: opts.account ?? AuthAccount,
|
|
22
|
+
verification: opts.verification ?? AuthVerification,
|
|
23
|
+
};
|
|
24
|
+
const brought = Object.values(models).filter((schema) => schema !== opts.user);
|
|
16
25
|
const providers = opts.providers ?? {};
|
|
17
26
|
const basePath = opts.basePath ?? '/auth';
|
|
18
27
|
return {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
account: accountSchema,
|
|
23
|
-
verification: verificationSchema,
|
|
24
|
-
},
|
|
25
|
-
create({ storageFactory }) {
|
|
26
|
-
const storageMap = new Map([
|
|
27
|
-
['user', storageFactory(userSchema, 'user')],
|
|
28
|
-
['session', storageFactory(sessionSchema, 'session')],
|
|
29
|
-
['account', storageFactory(accountSchema, 'account')],
|
|
30
|
-
['verification', storageFactory(verificationSchema, 'verification')],
|
|
31
|
-
]);
|
|
28
|
+
name: 'auth',
|
|
29
|
+
fronds: [frond('auth', { entities: brought })],
|
|
30
|
+
up(app) {
|
|
32
31
|
const engine = betterAuthLib({
|
|
33
|
-
database: fougereAdapter(
|
|
32
|
+
database: fougereAdapter(storagesOf(app, models)),
|
|
34
33
|
secret: opts.secret,
|
|
35
34
|
baseURL: opts.baseUrl,
|
|
36
35
|
basePath,
|
|
@@ -41,19 +40,18 @@ export function betterAuth(opts) {
|
|
|
41
40
|
socialProviders: translateSocial(providers),
|
|
42
41
|
plugins: translatePlugins(providers),
|
|
43
42
|
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
user: userSchema,
|
|
47
|
-
session: sessionSchema,
|
|
48
|
-
account: accountSchema,
|
|
49
|
-
verification: verificationSchema,
|
|
50
|
-
},
|
|
51
|
-
storages: Object.fromEntries(storageMap),
|
|
52
|
-
handler: engine.handler,
|
|
53
|
-
api: engine.api,
|
|
54
|
-
basePath,
|
|
55
|
-
};
|
|
43
|
+
const runtime = { handler: engine.handler, api: engine.api, basePath };
|
|
44
|
+
app.container.registerValue(AUTH, runtime);
|
|
56
45
|
},
|
|
57
46
|
};
|
|
58
47
|
}
|
|
48
|
+
function storagesOf(app, models) {
|
|
49
|
+
return new Map(Object.entries(models).map(([model, schema]) => {
|
|
50
|
+
const entity = lowerFirst(schema.name);
|
|
51
|
+
const storage = app.storageFor(entity);
|
|
52
|
+
if (!storage)
|
|
53
|
+
throw new Error(`[auth] no frond in this process holds '${entity}', the ${model} better-auth writes.`);
|
|
54
|
+
return [model, storage];
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
59
57
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,aAAa,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,aAAa,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,KAAK,EAA4D,MAAM,eAAe,CAAC;AACtG,OAAO,EAAE,UAAU,EAAmB,MAAM,iBAAiB,CAAC;AAC9D,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,cAAc,EAAmB,MAAM,cAAc,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,eAAe,EAAyB,MAAM,uBAAuB,CAAC;AAEtH,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAgBhD;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,IAAuB;IAChD,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,IAAI,QAAQ,CAAC;IACnC,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IACxD,MAAM,MAAM,GAA+B;QACz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,OAAO,GAAgB,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,GAAG,EAAE,MAAM,CAAC,GAA8B,EAAE,QAAQ,EAAE,CAAC;YAE/G,GAAG,CAAC,SAAS,CAAC,aAAa,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC7C,CAAC;KACF,CAAC;AACJ,CAAC;AAED,SAAS,UAAU,CAAC,GAAQ,EAAE,MAAkC;IAC9D,OAAO,IAAI,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,EAAE;QAC5D,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACvC,MAAM,OAAO,GAAwB,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QAC5D,IAAI,CAAC,OAAO;YAAE,MAAM,IAAI,KAAK,CAAC,0CAA0C,MAAM,UAAU,KAAK,sBAAsB,CAAC,CAAC;QAErH,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC1B,CAAC,CAAC,CAAC,CAAC;AACN,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fougere/auth-better",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.4-alpha.0",
|
|
4
4
|
"description": "Fougere's auth — a translation layer onto better-auth.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fougere",
|
|
@@ -31,12 +31,12 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@paralleldrive/cuid2": "^3.3.0",
|
|
33
33
|
"better-auth": "^1.6.11",
|
|
34
|
-
"@fougere/
|
|
35
|
-
"@fougere/
|
|
34
|
+
"@fougere/core": "0.12.4-alpha.0",
|
|
35
|
+
"@fougere/schema": "0.12.4-alpha.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"vitest": "^4.1.0",
|
|
39
|
-
"@fougere/adapter-sql": "0.12.
|
|
39
|
+
"@fougere/adapter-sql": "0.12.4-alpha.0"
|
|
40
40
|
},
|
|
41
41
|
"publishConfig": {
|
|
42
42
|
"access": "public"
|
package/src/entity/AuthUser.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { entity, primary, text, bool, created, optional } from '@fougere/schema';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
|
|
3
|
+
/** The user better-auth writes when the app names none — `User`, since the model is `user`. */
|
|
4
|
+
class User extends entity({
|
|
5
5
|
id: primary(),
|
|
6
6
|
name: text(),
|
|
7
7
|
email: text(),
|
|
@@ -10,3 +10,5 @@ export class AuthUser extends entity({
|
|
|
10
10
|
createdAt: created(),
|
|
11
11
|
updatedAt: created(),
|
|
12
12
|
}) {}
|
|
13
|
+
|
|
14
|
+
export { User as AuthUser };
|
|
@@ -4,7 +4,7 @@ import { entity, primary, text, date, created } from '@fougere/schema';
|
|
|
4
4
|
* Default Verification entity — better-auth shape.
|
|
5
5
|
* Used for email verification, password reset, magic links, etc.
|
|
6
6
|
*/
|
|
7
|
-
|
|
7
|
+
class Verification extends entity({
|
|
8
8
|
id: primary(),
|
|
9
9
|
identifier: text(),
|
|
10
10
|
value: text(),
|
|
@@ -12,3 +12,5 @@ export class AuthVerification extends entity({
|
|
|
12
12
|
createdAt: created(),
|
|
13
13
|
updatedAt: created(),
|
|
14
14
|
}) {}
|
|
15
|
+
|
|
16
|
+
export { Verification as AuthVerification };
|
|
@@ -13,7 +13,7 @@ export function authEntities(User: SchemaView): {
|
|
|
13
13
|
* Session entity — better-auth shape.
|
|
14
14
|
* `token` carries the opaque session secret (cookie value).
|
|
15
15
|
*/
|
|
16
|
-
class
|
|
16
|
+
class Session extends entity({
|
|
17
17
|
id: primary(),
|
|
18
18
|
userId: ref(target),
|
|
19
19
|
token: text(),
|
|
@@ -28,7 +28,7 @@ export function authEntities(User: SchemaView): {
|
|
|
28
28
|
* Account entity — better-auth shape (single PK, accountId/providerId pair,
|
|
29
29
|
* separate token columns instead of a JSON blob).
|
|
30
30
|
*/
|
|
31
|
-
class
|
|
31
|
+
class Account extends entity({
|
|
32
32
|
id: primary(),
|
|
33
33
|
accountId: text(),
|
|
34
34
|
providerId: text(),
|
|
@@ -44,5 +44,5 @@ export function authEntities(User: SchemaView): {
|
|
|
44
44
|
updatedAt: created(),
|
|
45
45
|
}) {}
|
|
46
46
|
|
|
47
|
-
return { AuthSession, AuthAccount };
|
|
47
|
+
return { AuthSession: Session, AuthAccount: Account };
|
|
48
48
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { betterAuth as betterAuthLib } from 'better-auth';
|
|
2
2
|
import { createId } from '@paralleldrive/cuid2';
|
|
3
|
-
import
|
|
4
|
-
import type
|
|
3
|
+
import { AUTH, frond, type App, type AuthRuntime, type Extension, type Storage } from '@fougere/core';
|
|
4
|
+
import { lowerFirst, type SchemaView } from '@fougere/schema';
|
|
5
5
|
import { AuthUser } from './entity/AuthUser.js';
|
|
6
6
|
import { AuthVerification } from './entity/AuthVerification.js';
|
|
7
7
|
import { authEntities } from './entity/authEntities.js';
|
|
@@ -24,33 +24,29 @@ export interface BetterAuthOptions {
|
|
|
24
24
|
verification?: SchemaView;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
/**
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
const
|
|
27
|
+
/**
|
|
28
|
+
* The auth provider, as an extension: it brings the frond its rows live in — the user too, when
|
|
29
|
+
* the app names none — and registers the runtime under `AUTH` once the app exists.
|
|
30
|
+
*/
|
|
31
|
+
export function betterAuth(opts: BetterAuthOptions): Extension {
|
|
32
|
+
const user = opts.user ?? AuthUser;
|
|
33
|
+
const { AuthSession, AuthAccount } = authEntities(user);
|
|
34
|
+
const models: Record<string, SchemaView> = {
|
|
35
|
+
user,
|
|
36
|
+
session: opts.session ?? AuthSession,
|
|
37
|
+
account: opts.account ?? AuthAccount,
|
|
38
|
+
verification: opts.verification ?? AuthVerification,
|
|
39
|
+
};
|
|
40
|
+
const brought = Object.values(models).filter((schema) => schema !== opts.user);
|
|
34
41
|
const providers = opts.providers ?? {};
|
|
35
42
|
const basePath = opts.basePath ?? '/auth';
|
|
36
43
|
|
|
37
44
|
return {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
account: accountSchema,
|
|
42
|
-
verification: verificationSchema,
|
|
43
|
-
},
|
|
44
|
-
create({ storageFactory }: AuthContext): AuthRuntime {
|
|
45
|
-
const storageMap: StorageMap = new Map([
|
|
46
|
-
['user', storageFactory(userSchema, 'user')],
|
|
47
|
-
['session', storageFactory(sessionSchema, 'session')],
|
|
48
|
-
['account', storageFactory(accountSchema, 'account')],
|
|
49
|
-
['verification', storageFactory(verificationSchema, 'verification')],
|
|
50
|
-
]);
|
|
51
|
-
|
|
45
|
+
name: 'auth',
|
|
46
|
+
fronds: [frond('auth', { entities: brought })],
|
|
47
|
+
up(app) {
|
|
52
48
|
const engine = betterAuthLib({
|
|
53
|
-
database: fougereAdapter(
|
|
49
|
+
database: fougereAdapter(storagesOf(app, models)),
|
|
54
50
|
secret: opts.secret,
|
|
55
51
|
baseURL: opts.baseUrl,
|
|
56
52
|
basePath,
|
|
@@ -61,19 +57,19 @@ export function betterAuth(opts: BetterAuthOptions): AuthConfig {
|
|
|
61
57
|
socialProviders: translateSocial(providers),
|
|
62
58
|
plugins: translatePlugins(providers),
|
|
63
59
|
});
|
|
60
|
+
const runtime: AuthRuntime = { handler: engine.handler, api: engine.api as Record<string, unknown>, basePath };
|
|
64
61
|
|
|
65
|
-
|
|
66
|
-
entities: {
|
|
67
|
-
user: userSchema,
|
|
68
|
-
session: sessionSchema,
|
|
69
|
-
account: accountSchema,
|
|
70
|
-
verification: verificationSchema,
|
|
71
|
-
},
|
|
72
|
-
storages: Object.fromEntries(storageMap),
|
|
73
|
-
handler: engine.handler,
|
|
74
|
-
api: engine.api as Record<string, unknown>,
|
|
75
|
-
basePath,
|
|
76
|
-
};
|
|
62
|
+
app.container.registerValue(AUTH, runtime);
|
|
77
63
|
},
|
|
78
64
|
};
|
|
79
65
|
}
|
|
66
|
+
|
|
67
|
+
function storagesOf(app: App, models: Record<string, SchemaView>): StorageMap {
|
|
68
|
+
return new Map(Object.entries(models).map(([model, schema]) => {
|
|
69
|
+
const entity = lowerFirst(schema.name);
|
|
70
|
+
const storage: Storage | undefined = app.storageFor(entity);
|
|
71
|
+
if (!storage) throw new Error(`[auth] no frond in this process holds '${entity}', the ${model} better-auth writes.`);
|
|
72
|
+
|
|
73
|
+
return [model, storage];
|
|
74
|
+
}));
|
|
75
|
+
}
|