better-auth 0.4.9-beta.13 → 0.4.9-beta.14

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/dist/.DS_Store CHANGED
Binary file
package/dist/access.js CHANGED
@@ -1,125 +1 @@
1
- // src/plugins/organization/access/src/access.ts
2
- var ParsingError = class extends Error {
3
- path;
4
- constructor(message, path) {
5
- super(message);
6
- this.path = path;
7
- }
8
- };
9
- var AccessControl = class {
10
- constructor(s) {
11
- this.s = s;
12
- this.statements = s;
13
- }
14
- statements;
15
- newRole(statements) {
16
- return new Role(statements);
17
- }
18
- };
19
- var Role = class _Role {
20
- statements;
21
- constructor(statements) {
22
- this.statements = statements;
23
- }
24
- authorize(request, connector) {
25
- for (const [requestedResource, requestedActions] of Object.entries(
26
- request
27
- )) {
28
- const allowedActions = this.statements[requestedResource];
29
- if (!allowedActions) {
30
- return {
31
- success: false,
32
- error: `You are not allowed to access resource: ${requestedResource}`
33
- };
34
- }
35
- const success = connector === "OR" ? requestedActions.some(
36
- (requestedAction) => allowedActions.includes(requestedAction)
37
- ) : requestedActions.every(
38
- (requestedAction) => allowedActions.includes(requestedAction)
39
- );
40
- if (success) {
41
- return { success };
42
- }
43
- return {
44
- success: false,
45
- error: `unauthorized to access resource "${requestedResource}"`
46
- };
47
- }
48
- return {
49
- success: false,
50
- error: "Not authorized"
51
- };
52
- }
53
- static fromString(s) {
54
- const statements = JSON.parse(s);
55
- if (typeof statements !== "object") {
56
- throw new ParsingError("statements is not an object", ".");
57
- }
58
- for (const [resource, actions] of Object.entries(statements)) {
59
- if (typeof resource !== "string") {
60
- throw new ParsingError("invalid resource identifier", resource);
61
- }
62
- if (!Array.isArray(actions)) {
63
- throw new ParsingError("actions is not an array", resource);
64
- }
65
- for (let i = 0; i < actions.length; i++) {
66
- if (typeof actions[i] !== "string") {
67
- throw new ParsingError("action is not a string", `${resource}[${i}]`);
68
- }
69
- }
70
- }
71
- return new _Role(statements);
72
- }
73
- toString() {
74
- return JSON.stringify(this.statements);
75
- }
76
- };
77
-
78
- // src/plugins/organization/access/statement.ts
79
- var createAccessControl = (statements) => {
80
- return new AccessControl(statements);
81
- };
82
- var defaultStatements = {
83
- organization: ["update", "delete"],
84
- member: ["create", "update", "delete"],
85
- invitation: ["create", "cancel"]
86
- };
87
- var defaultAc = createAccessControl(defaultStatements);
88
- var adminAc = defaultAc.newRole({
89
- organization: ["update"],
90
- invitation: ["create", "cancel"],
91
- member: ["create", "update", "delete"]
92
- });
93
- var ownerAc = defaultAc.newRole({
94
- organization: ["update", "delete"],
95
- member: ["create", "update", "delete"],
96
- invitation: ["create", "cancel"]
97
- });
98
- var memberAc = defaultAc.newRole({
99
- organization: [],
100
- member: [],
101
- invitation: []
102
- });
103
- var defaultRoles = {
104
- admin: adminAc,
105
- owner: ownerAc,
106
- member: memberAc
107
- };
108
-
109
- // src/plugins/organization/access/utils.ts
110
- var permissionFromString = (permission) => {
111
- return Role.fromString(permission ?? "");
112
- };
113
- export {
114
- AccessControl,
115
- ParsingError,
116
- Role,
117
- adminAc,
118
- createAccessControl,
119
- defaultAc,
120
- defaultRoles,
121
- defaultStatements,
122
- memberAc,
123
- ownerAc,
124
- permissionFromString
125
- };
1
+ var a=class extends Error{path;constructor(e,n){super(e),this.path=n}},c=class{constructor(e){this.s=e;this.statements=e}statements;newRole(e){return new i(e)}},i=class s{statements;constructor(e){this.statements=e}authorize(e,n){for(let[t,o]of Object.entries(e)){let r=this.statements[t];if(!r)return{success:!1,error:`You are not allowed to access resource: ${t}`};let p=n==="OR"?o.some(m=>r.includes(m)):o.every(m=>r.includes(m));return p?{success:p}:{success:!1,error:`unauthorized to access resource "${t}"`}}return{success:!1,error:"Not authorized"}}static fromString(e){let n=JSON.parse(e);if(typeof n!="object")throw new a("statements is not an object",".");for(let[t,o]of Object.entries(n)){if(typeof t!="string")throw new a("invalid resource identifier",t);if(!Array.isArray(o))throw new a("actions is not an array",t);for(let r=0;r<o.length;r++)if(typeof o[r]!="string")throw new a("action is not a string",`${t}[${r}]`)}return new s(n)}toString(){return JSON.stringify(this.statements)}};var l=s=>new c(s),d={organization:["update","delete"],member:["create","update","delete"],invitation:["create","cancel"]},u=l(d),S=u.newRole({organization:["update"],invitation:["create","cancel"],member:["create","update","delete"]}),f=u.newRole({organization:["update","delete"],member:["create","update","delete"],invitation:["create","cancel"]}),x=u.newRole({organization:[],member:[],invitation:[]}),b={admin:S,owner:f,member:x};var T=s=>i.fromString(s??"");export{c as AccessControl,a as ParsingError,i as Role,S as adminAc,l as createAccessControl,u as defaultAc,b as defaultRoles,d as defaultStatements,x as memberAc,f as ownerAc,T as permissionFromString};
@@ -1,136 +1 @@
1
- // src/adapters/drizzle-adapter/index.ts
2
- import { and, asc, desc, eq, or } from "drizzle-orm";
3
-
4
- // src/error/index.ts
5
- var BetterAuthError = class extends Error {
6
- constructor(message, cause) {
7
- super(message);
8
- this.name = "BetterAuthError";
9
- this.message = message;
10
- this.cause = cause;
11
- this.stack = "";
12
- }
13
- };
14
-
15
- // src/adapters/drizzle-adapter/index.ts
16
- function getSchema(modelName, options) {
17
- const schema = options.schema;
18
- if (!schema) {
19
- throw new BetterAuthError(
20
- "Drizzle adapter failed to initialize. Schema not found. Please provide a schema object in the adapter options object."
21
- );
22
- }
23
- const model = options.usePlural ? `${modelName}s` : modelName;
24
- const schemaModel = schema[model];
25
- if (!schemaModel) {
26
- throw new BetterAuthError(
27
- `[# Drizzle Adapter]: The model "${modelName}" was not found in the schema object. Please pass the schema directly to the adapter options.`
28
- );
29
- }
30
- return schemaModel;
31
- }
32
- function whereConvertor(where, schemaModel) {
33
- if (!where) return [];
34
- if (where.length === 1) {
35
- const w = where[0];
36
- if (!w) {
37
- return [];
38
- }
39
- return [eq(schemaModel[w.field], w.value)];
40
- }
41
- const andGroup = where.filter((w) => w.connector === "AND" || !w.connector);
42
- const orGroup = where.filter((w) => w.connector === "OR");
43
- const andClause = and(
44
- ...andGroup.map((w) => {
45
- return eq(schemaModel[w.field], w.value);
46
- })
47
- );
48
- const orClause = or(
49
- ...orGroup.map((w) => {
50
- return eq(schemaModel[w.field], w.value);
51
- })
52
- );
53
- const clause = [];
54
- if (andGroup.length) clause.push(andClause);
55
- if (orGroup.length) clause.push(orClause);
56
- return clause;
57
- }
58
- var drizzleAdapter = (db, options) => {
59
- const schema = options.schema || db._.fullSchema;
60
- const databaseType = options?.provider;
61
- return {
62
- id: "drizzle",
63
- async create(data) {
64
- const { model, data: val } = data;
65
- const schemaModel = getSchema(model, {
66
- schema,
67
- usePlural: options.usePlural
68
- });
69
- const mutation = db.insert(schemaModel).values(val);
70
- if (databaseType !== "mysql") return (await mutation.returning())[0];
71
- await mutation;
72
- const res = await db.select().from(schemaModel).where(eq(schemaModel.id, data.data.id));
73
- return res[0];
74
- },
75
- async findOne(data) {
76
- const { model, where, select: included } = data;
77
- const schemaModel = getSchema(model, {
78
- schema,
79
- usePlural: options.usePlural
80
- });
81
- const wheres = whereConvertor(where, schemaModel);
82
- let res = null;
83
- if (!!included?.length) {
84
- res = await db.select(
85
- ...included.map((include) => {
86
- return {
87
- [include]: schemaModel[include]
88
- };
89
- })
90
- ).from(schemaModel).where(...wheres);
91
- } else {
92
- res = await db.select().from(schemaModel).where(...wheres);
93
- }
94
- if (!!res.length) return res[0];
95
- else return null;
96
- },
97
- async findMany(data) {
98
- const { model, where, limit, offset, sortBy } = data;
99
- const schemaModel = getSchema(model, {
100
- schema,
101
- usePlural: options.usePlural
102
- });
103
- const wheres = where ? whereConvertor(where, schemaModel) : [];
104
- const fn = sortBy?.direction === "desc" ? desc : asc;
105
- const res = await db.select().from(schemaModel).limit(limit || 100).offset(offset || 0).orderBy(fn(schemaModel[sortBy?.field || "id"])).where(...wheres.length ? wheres : []);
106
- return res;
107
- },
108
- async update(data) {
109
- const { model, where, update } = data;
110
- const schemaModel = getSchema(model, {
111
- schema,
112
- usePlural: options.usePlural
113
- });
114
- const wheres = whereConvertor(where, schemaModel);
115
- const mutation = db.update(schemaModel).set(update).where(...wheres);
116
- if (databaseType !== "mysql") return (await mutation.returning())[0];
117
- await mutation;
118
- const res = await db.select().from(schemaModel).where(eq(schemaModel.id, data.update.id));
119
- return res[0];
120
- },
121
- async delete(data) {
122
- const { model, where } = data;
123
- const schemaModel = getSchema(model, {
124
- schema,
125
- usePlural: options.usePlural
126
- });
127
- const wheres = whereConvertor(where, schemaModel);
128
- const res = await db.delete(schemaModel).where(...wheres);
129
- return res[0];
130
- },
131
- options
132
- };
133
- };
134
- export {
135
- drizzleAdapter
136
- };
1
+ import{and as y,asc as g,desc as P,eq as m,or as z}from"drizzle-orm";var h=class extends Error{constructor(r,l){super(r),this.name="BetterAuthError",this.message=r,this.cause=l,this.stack=""}};function f(t,r){let l=r.schema;if(!l)throw new h("Drizzle adapter failed to initialize. Schema not found. Please provide a schema object in the adapter options object.");let u=r.usePlural?`${t}s`:t,s=l[u];if(!s)throw new h(`[# Drizzle Adapter]: The model "${t}" was not found in the schema object. Please pass the schema directly to the adapter options.`);return s}function p(t,r){if(!t)return[];if(t.length===1){let e=t[0];return e?[m(r[e.field],e.value)]:[]}let l=t.filter(e=>e.connector==="AND"||!e.connector),u=t.filter(e=>e.connector==="OR"),s=y(...l.map(e=>m(r[e.field],e.value))),o=z(...u.map(e=>m(r[e.field],e.value))),a=[];return l.length&&a.push(s),u.length&&a.push(o),a}var B=(t,r)=>{let l=r.schema||t._.fullSchema,u=r?.provider;return{id:"drizzle",async create(s){let{model:o,data:a}=s,e=f(o,{schema:l,usePlural:r.usePlural}),n=t.insert(e).values(a);return u!=="mysql"?(await n.returning())[0]:(await n,(await t.select().from(e).where(m(e.id,s.data.id)))[0])},async findOne(s){let{model:o,where:a,select:e}=s,n=f(o,{schema:l,usePlural:r.usePlural}),i=p(a,n),c=null;return e?.length?c=await t.select(...e.map(d=>({[d]:n[d]}))).from(n).where(...i):c=await t.select().from(n).where(...i),c.length?c[0]:null},async findMany(s){let{model:o,where:a,limit:e,offset:n,sortBy:i}=s,c=f(o,{schema:l,usePlural:r.usePlural}),d=a?p(a,c):[],w=i?.direction==="desc"?P:g;return await t.select().from(c).limit(e||100).offset(n||0).orderBy(w(c[i?.field||"id"])).where(...d.length?d:[])},async update(s){let{model:o,where:a,update:e}=s,n=f(o,{schema:l,usePlural:r.usePlural}),i=p(a,n),c=t.update(n).set(e).where(...i);return u!=="mysql"?(await c.returning())[0]:(await c,(await t.select().from(n).where(m(n.id,s.update.id)))[0])},async delete(s){let{model:o,where:a}=s,e=f(o,{schema:l,usePlural:r.usePlural}),n=p(a,e);return(await t.delete(e).where(...n))[0]},options:r}};export{B as drizzleAdapter};