@service_laboratory/auth 0.0.2 → 0.0.3

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.
@@ -1,3 +1,75 @@
1
- export declare const sum: (a: string, b: string) => string;
1
+ import { Axios } from 'axios';
2
+ import { AxiosResponse } from 'axios';
3
+ import { Derived } from '@tanstack/store';
4
+ import { Store } from '@tanstack/store';
5
+
6
+ declare interface AccountData {
7
+ user: AccountUser;
8
+ isLoaded: boolean;
9
+ isAuthenticated: boolean;
10
+ }
11
+
12
+ export declare interface AccountUser {
13
+ id: string | null;
14
+ email: string | null;
15
+ roles: Role[];
16
+ is_enabled: boolean;
17
+ is_active: boolean;
18
+ }
19
+
20
+ declare interface ActivateRequest {
21
+ code: string;
22
+ }
23
+
24
+ export declare class Auth {
25
+ accountStore: Store<AccountData, (cb: AccountData) => AccountData>;
26
+ httpClient: Axios;
27
+ constructor(httpClient: Axios);
28
+ initAuthData(): void;
29
+ load(): Promise<void>;
30
+ login(values: LoginRequest): Promise<any>;
31
+ register(values: RegisterRequest): Promise<AxiosResponse<any, any>>;
32
+ activate(values: ActivateRequest): Promise<void>;
33
+ logout(): void;
34
+ startResetPassword(values: StartResetPasswordRequest): Promise<AxiosResponse<any, any>>;
35
+ resetPassword(values: ResetPasswordRequest): Promise<void>;
36
+ _initUserData({ user, access_token }: {
37
+ user: AccountUser;
38
+ access_token: string;
39
+ }): void;
40
+ _setAuthData(token: string): void;
41
+ _clearAuthData(): void;
42
+ }
43
+
44
+ export declare const isAdmin: Derived<boolean, readonly [Store<AccountData, (cb: AccountData) => AccountData>]>;
45
+
46
+ declare interface LoginRequest {
47
+ email: string;
48
+ password: string;
49
+ }
50
+
51
+ declare interface Permission {
52
+ name: string;
53
+ app: string;
54
+ }
55
+
56
+ declare interface RegisterRequest {
57
+ email: string;
58
+ password: string;
59
+ }
60
+
61
+ declare interface ResetPasswordRequest {
62
+ code: string;
63
+ password: string;
64
+ }
65
+
66
+ declare interface Role {
67
+ name: string;
68
+ permissions: Permission[];
69
+ }
70
+
71
+ declare interface StartResetPasswordRequest {
72
+ email: string;
73
+ }
2
74
 
3
75
  export { }
@@ -1,4 +1,228 @@
1
- const t = (o, s) => o + s;
1
+ const c = /* @__PURE__ */ new WeakMap(), u = /* @__PURE__ */ new WeakMap(), p = {
2
+ current: []
3
+ };
4
+ let d = !1;
5
+ const h = /* @__PURE__ */ new Set(), _ = /* @__PURE__ */ new Map();
6
+ function S(a) {
7
+ const e = Array.from(a).sort((t, s) => t instanceof r && t.options.deps.includes(s) ? 1 : s instanceof r && s.options.deps.includes(t) ? -1 : 0);
8
+ for (const t of e) {
9
+ if (p.current.includes(t))
10
+ continue;
11
+ p.current.push(t), t.recompute();
12
+ const s = u.get(t);
13
+ if (s)
14
+ for (const i of s) {
15
+ const n = c.get(i);
16
+ n && S(n);
17
+ }
18
+ }
19
+ }
20
+ function D(a) {
21
+ a.listeners.forEach(
22
+ (e) => e({
23
+ prevVal: a.prevState,
24
+ currentVal: a.state
25
+ })
26
+ );
27
+ }
28
+ function V(a) {
29
+ a.listeners.forEach(
30
+ (e) => e({
31
+ prevVal: a.prevState,
32
+ currentVal: a.state
33
+ })
34
+ );
35
+ }
36
+ function m(a) {
37
+ if (h.add(a), !d)
38
+ try {
39
+ for (d = !0; h.size > 0; ) {
40
+ const e = Array.from(h);
41
+ h.clear();
42
+ for (const t of e) {
43
+ const s = _.get(t) ?? t.prevState;
44
+ t.prevState = s, D(t);
45
+ }
46
+ for (const t of e) {
47
+ const s = c.get(t);
48
+ s && (p.current.push(t), S(s));
49
+ }
50
+ for (const t of e) {
51
+ const s = c.get(t);
52
+ if (s)
53
+ for (const i of s)
54
+ V(i);
55
+ }
56
+ }
57
+ } finally {
58
+ d = !1, p.current = [], _.clear();
59
+ }
60
+ }
61
+ function w(a) {
62
+ return typeof a == "function";
63
+ }
64
+ class v {
65
+ constructor(e, t) {
66
+ this.listeners = /* @__PURE__ */ new Set(), this.subscribe = (s) => {
67
+ var i, n;
68
+ this.listeners.add(s);
69
+ const l = (n = (i = this.options) == null ? void 0 : i.onSubscribe) == null ? void 0 : n.call(i, s, this);
70
+ return () => {
71
+ this.listeners.delete(s), l?.();
72
+ };
73
+ }, this.prevState = e, this.state = e, this.options = t;
74
+ }
75
+ setState(e) {
76
+ var t, s, i;
77
+ this.prevState = this.state, (t = this.options) != null && t.updateFn ? this.state = this.options.updateFn(this.prevState)(e) : w(e) ? this.state = e(this.prevState) : this.state = e, (i = (s = this.options) == null ? void 0 : s.onUpdate) == null || i.call(s), m(this);
78
+ }
79
+ }
80
+ class r {
81
+ constructor(e) {
82
+ this.listeners = /* @__PURE__ */ new Set(), this._subscriptions = [], this.lastSeenDepValues = [], this.getDepVals = () => {
83
+ const t = [], s = [];
84
+ for (const i of this.options.deps)
85
+ t.push(i.prevState), s.push(i.state);
86
+ return this.lastSeenDepValues = s, {
87
+ prevDepVals: t,
88
+ currDepVals: s,
89
+ prevVal: this.prevState ?? void 0
90
+ };
91
+ }, this.recompute = () => {
92
+ var t, s;
93
+ this.prevState = this.state;
94
+ const { prevDepVals: i, currDepVals: n, prevVal: l } = this.getDepVals();
95
+ this.state = this.options.fn({
96
+ prevDepVals: i,
97
+ currDepVals: n,
98
+ prevVal: l
99
+ }), (s = (t = this.options).onUpdate) == null || s.call(t);
100
+ }, this.checkIfRecalculationNeededDeeply = () => {
101
+ for (const n of this.options.deps)
102
+ n instanceof r && n.checkIfRecalculationNeededDeeply();
103
+ let t = !1;
104
+ const s = this.lastSeenDepValues, { currDepVals: i } = this.getDepVals();
105
+ for (let n = 0; n < i.length; n++)
106
+ if (i[n] !== s[n]) {
107
+ t = !0;
108
+ break;
109
+ }
110
+ t && this.recompute();
111
+ }, this.mount = () => (this.registerOnGraph(), this.checkIfRecalculationNeededDeeply(), () => {
112
+ this.unregisterFromGraph();
113
+ for (const t of this._subscriptions)
114
+ t();
115
+ }), this.subscribe = (t) => {
116
+ var s, i;
117
+ this.listeners.add(t);
118
+ const n = (i = (s = this.options).onSubscribe) == null ? void 0 : i.call(s, t, this);
119
+ return () => {
120
+ this.listeners.delete(t), n?.();
121
+ };
122
+ }, this.options = e, this.state = e.fn({
123
+ prevDepVals: void 0,
124
+ prevVal: void 0,
125
+ currDepVals: this.getDepVals().currDepVals
126
+ });
127
+ }
128
+ registerOnGraph(e = this.options.deps) {
129
+ for (const t of e)
130
+ if (t instanceof r)
131
+ t.registerOnGraph(), this.registerOnGraph(t.options.deps);
132
+ else if (t instanceof v) {
133
+ let s = c.get(t);
134
+ s || (s = /* @__PURE__ */ new Set(), c.set(t, s)), s.add(this);
135
+ let i = u.get(this);
136
+ i || (i = /* @__PURE__ */ new Set(), u.set(this, i)), i.add(t);
137
+ }
138
+ }
139
+ unregisterFromGraph(e = this.options.deps) {
140
+ for (const t of e)
141
+ if (t instanceof r)
142
+ this.unregisterFromGraph(t.options.deps);
143
+ else if (t instanceof v) {
144
+ const s = c.get(t);
145
+ s && s.delete(this);
146
+ const i = u.get(this);
147
+ i && i.delete(t);
148
+ }
149
+ }
150
+ }
151
+ const f = "Authorization", g = () => ({
152
+ user: {
153
+ id: null,
154
+ email: null,
155
+ roles: [],
156
+ is_enabled: !1,
157
+ is_active: !1
158
+ },
159
+ isAuthenticated: !1,
160
+ isLoaded: !1
161
+ }), o = new v(g()), b = new r({
162
+ fn: () => !!o.state.user?.roles.find((a) => a.name === "admin"),
163
+ deps: [o]
164
+ });
165
+ class A {
166
+ accountStore;
167
+ httpClient;
168
+ constructor(e) {
169
+ this.httpClient = e, this.accountStore = o;
170
+ }
171
+ initAuthData() {
172
+ const e = localStorage.getItem("token");
173
+ e && (this.httpClient.defaults.headers.common[f] = e);
174
+ }
175
+ async load() {
176
+ try {
177
+ const { data: e } = await this.httpClient.get("/api/auth/account/me");
178
+ o.setState(() => ({
179
+ user: e,
180
+ isAuthenticated: !0,
181
+ isLoaded: !0
182
+ }));
183
+ } catch {
184
+ o.setState((e) => ({
185
+ ...e,
186
+ isLoaded: !0
187
+ }));
188
+ }
189
+ }
190
+ async login(e) {
191
+ const t = await this.httpClient.post("/api/auth/account/login", e), { user: s, access_token: i } = t.data;
192
+ return s.is_active && this._initUserData({ user: s, access_token: i }), s;
193
+ }
194
+ register(e) {
195
+ return this.httpClient.post("/api/auth/account/register", e);
196
+ }
197
+ async activate(e) {
198
+ const t = await this.httpClient.post("/api/auth/account/activate", e);
199
+ this._initUserData(t.data);
200
+ }
201
+ logout() {
202
+ o.setState(() => ({ ...g(), isLoaded: !0 })), this._clearAuthData();
203
+ }
204
+ startResetPassword(e) {
205
+ return this.httpClient.post("/api/auth/account/start-reset-password", e);
206
+ }
207
+ async resetPassword(e) {
208
+ const t = await this.httpClient.post("/api/auth/account/reset-password", e);
209
+ this._initUserData(t.data);
210
+ }
211
+ _initUserData({ user: e, access_token: t }) {
212
+ this._setAuthData(t), o.setState(() => ({
213
+ user: e,
214
+ isAuthenticated: !0,
215
+ isLoaded: !0
216
+ }));
217
+ }
218
+ _setAuthData(e) {
219
+ localStorage.setItem("token", e), this.httpClient.defaults.headers.common[f] = e;
220
+ }
221
+ _clearAuthData() {
222
+ localStorage.removeItem("token"), delete this.httpClient.defaults.headers.common[f];
223
+ }
224
+ }
2
225
  export {
3
- t as sum
226
+ A as Auth,
227
+ b as isAdmin
4
228
  };
@@ -1 +1 @@
1
- (function(e,t){typeof exports=="object"&&typeof module<"u"?t(exports):typeof define=="function"&&define.amd?define(["exports"],t):(e=typeof globalThis<"u"?globalThis:e||self,t(e.laboratory={}))})(this,(function(e){"use strict";const t=(n,o)=>n+o;e.sum=t,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(r,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(r=typeof globalThis<"u"?globalThis:r||self,a(r.laboratory={}))})(this,(function(r){"use strict";const a=new WeakMap,u=new WeakMap,h={current:[]};let f=!1;const p=new Set,S=new Map;function g(o){const e=Array.from(o).sort((t,s)=>t instanceof c&&t.options.deps.includes(s)?1:s instanceof c&&s.options.deps.includes(t)?-1:0);for(const t of e){if(h.current.includes(t))continue;h.current.push(t),t.recompute();const s=u.get(t);if(s)for(const i of s){const n=a.get(i);n&&g(n)}}}function m(o){o.listeners.forEach(e=>e({prevVal:o.prevState,currentVal:o.state}))}function V(o){o.listeners.forEach(e=>e({prevVal:o.prevState,currentVal:o.state}))}function b(o){if(p.add(o),!f)try{for(f=!0;p.size>0;){const e=Array.from(p);p.clear();for(const t of e){const s=S.get(t)??t.prevState;t.prevState=s,m(t)}for(const t of e){const s=a.get(t);s&&(h.current.push(t),g(s))}for(const t of e){const s=a.get(t);if(s)for(const i of s)V(i)}}}finally{f=!1,h.current=[],S.clear()}}function y(o){return typeof o=="function"}class v{constructor(e,t){this.listeners=new Set,this.subscribe=s=>{var i,n;this.listeners.add(s);const d=(n=(i=this.options)==null?void 0:i.onSubscribe)==null?void 0:n.call(i,s,this);return()=>{this.listeners.delete(s),d?.()}},this.prevState=e,this.state=e,this.options=t}setState(e){var t,s,i;this.prevState=this.state,(t=this.options)!=null&&t.updateFn?this.state=this.options.updateFn(this.prevState)(e):y(e)?this.state=e(this.prevState):this.state=e,(i=(s=this.options)==null?void 0:s.onUpdate)==null||i.call(s),b(this)}}class c{constructor(e){this.listeners=new Set,this._subscriptions=[],this.lastSeenDepValues=[],this.getDepVals=()=>{const t=[],s=[];for(const i of this.options.deps)t.push(i.prevState),s.push(i.state);return this.lastSeenDepValues=s,{prevDepVals:t,currDepVals:s,prevVal:this.prevState??void 0}},this.recompute=()=>{var t,s;this.prevState=this.state;const{prevDepVals:i,currDepVals:n,prevVal:d}=this.getDepVals();this.state=this.options.fn({prevDepVals:i,currDepVals:n,prevVal:d}),(s=(t=this.options).onUpdate)==null||s.call(t)},this.checkIfRecalculationNeededDeeply=()=>{for(const n of this.options.deps)n instanceof c&&n.checkIfRecalculationNeededDeeply();let t=!1;const s=this.lastSeenDepValues,{currDepVals:i}=this.getDepVals();for(let n=0;n<i.length;n++)if(i[n]!==s[n]){t=!0;break}t&&this.recompute()},this.mount=()=>(this.registerOnGraph(),this.checkIfRecalculationNeededDeeply(),()=>{this.unregisterFromGraph();for(const t of this._subscriptions)t()}),this.subscribe=t=>{var s,i;this.listeners.add(t);const n=(i=(s=this.options).onSubscribe)==null?void 0:i.call(s,t,this);return()=>{this.listeners.delete(t),n?.()}},this.options=e,this.state=e.fn({prevDepVals:void 0,prevVal:void 0,currDepVals:this.getDepVals().currDepVals})}registerOnGraph(e=this.options.deps){for(const t of e)if(t instanceof c)t.registerOnGraph(),this.registerOnGraph(t.options.deps);else if(t instanceof v){let s=a.get(t);s||(s=new Set,a.set(t,s)),s.add(this);let i=u.get(this);i||(i=new Set,u.set(this,i)),i.add(t)}}unregisterFromGraph(e=this.options.deps){for(const t of e)if(t instanceof c)this.unregisterFromGraph(t.options.deps);else if(t instanceof v){const s=a.get(t);s&&s.delete(this);const i=u.get(this);i&&i.delete(t)}}}const _="Authorization",D=()=>({user:{id:null,email:null,roles:[],is_enabled:!1,is_active:!1},isAuthenticated:!1,isLoaded:!1}),l=new v(D()),w=new c({fn:()=>!!l.state.user?.roles.find(o=>o.name==="admin"),deps:[l]});class A{accountStore;httpClient;constructor(e){this.httpClient=e,this.accountStore=l}initAuthData(){const e=localStorage.getItem("token");e&&(this.httpClient.defaults.headers.common[_]=e)}async load(){try{const{data:e}=await this.httpClient.get("/api/auth/account/me");l.setState(()=>({user:e,isAuthenticated:!0,isLoaded:!0}))}catch{l.setState(e=>({...e,isLoaded:!0}))}}async login(e){const t=await this.httpClient.post("/api/auth/account/login",e),{user:s,access_token:i}=t.data;return s.is_active&&this._initUserData({user:s,access_token:i}),s}register(e){return this.httpClient.post("/api/auth/account/register",e)}async activate(e){const t=await this.httpClient.post("/api/auth/account/activate",e);this._initUserData(t.data)}logout(){l.setState(()=>({...D(),isLoaded:!0})),this._clearAuthData()}startResetPassword(e){return this.httpClient.post("/api/auth/account/start-reset-password",e)}async resetPassword(e){const t=await this.httpClient.post("/api/auth/account/reset-password",e);this._initUserData(t.data)}_initUserData({user:e,access_token:t}){this._setAuthData(t),l.setState(()=>({user:e,isAuthenticated:!0,isLoaded:!0}))}_setAuthData(e){localStorage.setItem("token",e),this.httpClient.defaults.headers.common[_]=e}_clearAuthData(){localStorage.removeItem("token"),delete this.httpClient.defaults.headers.common[_]}}r.Auth=A,r.isAdmin=w,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@service_laboratory/auth",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "main": "./dist/laboratory.umd.cjs",
6
6
  "module": "./dist/laboratory.js",
@@ -19,6 +19,10 @@
19
19
  "require": "./dist/laboratory.umd.cjs"
20
20
  }
21
21
  },
22
+ "dependencies": {
23
+ "@tanstack/store": "^0.7.4",
24
+ "axios": "^1.11.0"
25
+ },
22
26
  "devDependencies": {
23
27
  "typescript": "~5.8.3",
24
28
  "vite": "^7.1.4",