@murumets-ee/auth 0.1.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/LICENSE +94 -0
- package/README.md +15 -0
- package/dist/admin/index.d.ts +72 -0
- package/dist/admin/index.js +1 -0
- package/dist/chunk-2GGGFSMD.js +1 -0
- package/dist/chunk-NH6AU5Z6.js +1 -0
- package/dist/client.d.ts +2064 -0
- package/dist/client.js +1 -0
- package/dist/index.d.ts +114 -0
- package/dist/index.js +1 -0
- package/dist/plugin-C-MA3A5U.d.ts +993 -0
- package/dist/plugin.d.ts +6 -0
- package/dist/plugin.js +1 -0
- package/dist/server-GEU5KK6Y.js +1 -0
- package/package.json +47 -0
package/dist/plugin.d.ts
ADDED
package/dist/plugin.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{a,b}from"./chunk-2GGGFSMD.js";export{b as auth,a as getAuth};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{i as l,j as p}from"./chunk-NH6AU5Z6.js";import{sql as f}from"drizzle-orm";import{betterAuth as w}from"better-auth";import{drizzleAdapter as y}from"better-auth/adapters/drizzle";import{APIError as b,createAuthMiddleware as h}from"better-auth/api";import{nextCookies as A}from"better-auth/next-js";import{admin as I}from"better-auth/plugins";import{createAccessControl as k}from"better-auth/plugins/access";import{organization as R}from"better-auth/plugins/organization";function c(n){let s=n?.context?.session,e=s?.user;return{id:e?.id??s?.userId,name:e?.name}}var S=new Set(["updatedAt","createdAt"]);function T(n,s){function e(t){s?.log(t).catch(()=>{})}let m=null;return{user:{create:{after:async t=>{try{await n.readWrite.execute(f`UPDATE "user" SET role = 'admin' WHERE id = ${t.id} AND (SELECT COUNT(*) FROM "user") = 1`)}catch{}e({action:"auth.signup",entityType:"user",entityId:t.id,userId:t.id,userName:t.name,changes:{fields:{name:t.name,email:t.email}}})}},update:{before:async t=>{let i={};for(let[r,a]of Object.entries(t))S.has(r)||r==="id"||(i[r]=a);m=Object.keys(i).length>0?i:null},after:async(t,i)=>{let r=c(i),a=m;m=null,e({action:"auth.user.update",entityType:"user",entityId:t.id,userId:r.id,userName:r.name,changes:a?{fields:a}:void 0,metadata:{targetUser:t.name}})}},delete:{after:async(t,i)=>{let r=c(i);e({action:"auth.user.delete",entityType:"user",entityId:t.id,userId:r.id,userName:r.name,metadata:{targetUser:t.name}})}}}}}var N=["/sign-in/email","/sign-in/social"],_="/sign-out",v={"/change-password":"auth.password.change","/forget-password":"auth.password.reset_request","/reset-password":"auth.password.reset"},P={"/admin/set-role":"auth.admin.set_role","/admin/ban-user":"auth.admin.ban","/admin/unban-user":"auth.admin.unban","/admin/create-user":"auth.admin.create_user","/admin/remove-user":"auth.admin.remove_user","/admin/impersonate-user":"auth.admin.impersonate","/admin/stop-impersonating":"auth.admin.stop_impersonating","/admin/revoke-session":"auth.admin.revoke_session","/admin/revoke-sessions":"auth.admin.revoke_sessions"};function E(n){function s(e){n.log(e).catch(()=>{})}return{after:h(async e=>{if(N.some(d=>e.path.startsWith(d))){let o=e.context.returned?.status;if(!o)return;if(o>=400){let u=e.body?.email;s({action:"auth.login.failed",metadata:{...typeof u=="string"?{email:u}:{},status:o,path:e.path}})}else{let u=e.context.newSession;u?.user?.id&&s({action:"auth.login",entityType:"user",entityId:u.user.id,userId:u.user.id,userName:u.user.name})}return}if(e.path===_){let d=e.context.returned;if(!d?.status||d.status<400){let o=c(e);o.id&&s({action:"auth.logout",entityType:"user",entityId:o.id,userId:o.id,userName:o.name})}return}let t=v[e.path];if(t){let d=e.context.returned;if(!d?.status||d.status<400){let o=c(e),u=e.body;s({action:t,entityType:"user",userId:o.id,userName:o.name,metadata:{...typeof u?.email=="string"?{email:u.email}:{},path:e.path}})}return}let i=P[e.path];if(!i)return;let r=e.context.returned;if(r?.status&&r.status>=400)return;let a=c(e),g=e.body;s({action:i,entityType:"user",entityId:g?.userId??void 0,userId:a.id,userName:a.name,metadata:{targetUserId:g?.userId,...g?.role?{role:g.role}:{},path:e.path}})})}}function D(n){return h(async s=>{if(s.path!=="/sign-up/email")return;let e=await n.readWrite.execute(f`SELECT COUNT(*)::text as count FROM "user"`);if(Number(e[0]?.count)>0)throw new b("FORBIDDEN",{message:"Sign-up is closed"})})}function q(n,s,e){let m=[...s.entities.values()],t=l(m),i=k(t),r=p(i,m),a={};return n.social?.google&&(a.google=n.social.google),n.social?.github&&(a.github=n.social.github),w({database:y(s.db.readWrite,{provider:"pg",...n.schema?{schema:n.schema}:{}}),emailAndPassword:{enabled:n.providers?.includes("email")??!0},socialProviders:a,session:{expiresIn:n.session?.expiresIn??3600*2,updateAge:n.session?.updateAge??3600},rateLimit:{enabled:!0,window:60,max:100,storage:"memory",customRules:{"/sign-in/email":{window:60,max:5},"/sign-in/social":{window:60,max:10},"/sign-up/email":{window:60,max:3},"/forget-password":{window:60,max:3},"/reset-password":{window:60,max:5},"/admin/*":{window:60,max:20}}},databaseHooks:T(s.db,e),hooks:{before:D(s.db),...e?E(e):{}},plugins:[I({ac:i,roles:r,defaultRole:"authenticated"}),...n.organizations?[R({ac:i,roles:r})]:[],...n.betterAuthPlugins??[],A()]})}export{q as createAuthServer};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@murumets-ee/auth",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "Elastic-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./client": {
|
|
12
|
+
"types": "./dist/client.d.ts",
|
|
13
|
+
"import": "./dist/client.js"
|
|
14
|
+
},
|
|
15
|
+
"./plugin": {
|
|
16
|
+
"types": "./dist/plugin.d.ts",
|
|
17
|
+
"import": "./dist/plugin.js"
|
|
18
|
+
},
|
|
19
|
+
"./admin": {
|
|
20
|
+
"types": "./dist/admin/index.d.ts",
|
|
21
|
+
"import": "./dist/admin/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"better-auth": "^1.4.0",
|
|
29
|
+
"drizzle-orm": "^0.45.0",
|
|
30
|
+
"server-only": "^0.0.1",
|
|
31
|
+
"@murumets-ee/core": "0.1.0",
|
|
32
|
+
"@murumets-ee/db": "0.1.0",
|
|
33
|
+
"@murumets-ee/logging": "0.1.0",
|
|
34
|
+
"@murumets-ee/entity": "0.1.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^22.10.5",
|
|
38
|
+
"tsup": "^8.3.5",
|
|
39
|
+
"typescript": "^5.7.3",
|
|
40
|
+
"vitest": "^2.1.8"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsup",
|
|
44
|
+
"dev": "tsup --watch",
|
|
45
|
+
"test": "vitest"
|
|
46
|
+
}
|
|
47
|
+
}
|