@service_laboratory/auth 2.0.8 → 2.0.9

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/index.d.ts CHANGED
@@ -9,6 +9,7 @@ declare interface AccountData {
9
9
  user: AccountUser;
10
10
  isLoaded: boolean;
11
11
  isAuthenticated: boolean;
12
+ sessionID?: string | null;
12
13
  }
13
14
 
14
15
  export declare interface AccountUser {
@@ -35,9 +36,10 @@ export declare class Auth {
35
36
  logout(): void;
36
37
  startResetPassword(values: StartResetPasswordRequest): Promise<AxiosResponse<any, any>>;
37
38
  resetPassword(values: ResetPasswordRequest): Promise<void>;
38
- _initUserData({ user, token }: {
39
+ _initUserData({ user, token, sessionID, }: {
39
40
  user: AccountUser;
40
41
  token: string;
42
+ sessionID: string;
41
43
  }): void;
42
44
  _setAuthData(token: string): void;
43
45
  _clearAuthData(): void;
package/dist/index.js CHANGED
@@ -4821,6 +4821,7 @@ const Ie = "Authorization", ar = () => ({
4821
4821
  is_enabled: !1,
4822
4822
  is_email_verified: !1
4823
4823
  },
4824
+ sessionID: null,
4824
4825
  isAuthenticated: !1,
4825
4826
  isLoaded: !1
4826
4827
  }), ie = new Ye(ar()), cn = new le({
@@ -4841,7 +4842,8 @@ class un {
4841
4842
  try {
4842
4843
  const { data: r } = await this.httpClient.get("/api/auth/account/me");
4843
4844
  ie.setState(() => ({
4844
- user: r,
4845
+ user: r.user,
4846
+ sessionID: r.session_id,
4845
4847
  isAuthenticated: !0,
4846
4848
  isLoaded: !0
4847
4849
  }));
@@ -4853,8 +4855,8 @@ class un {
4853
4855
  }
4854
4856
  }
4855
4857
  async login(r) {
4856
- const t = await this.httpClient.post("/api/auth/account/login", r), { user: o, token: n } = t.data;
4857
- return this._initUserData({ user: o, token: n }), o;
4858
+ const t = await this.httpClient.post("/api/auth/account/login", r), { user: o, token: n, session_id: s } = t.data;
4859
+ return this._initUserData({ user: o, token: n, sessionID: s }), o;
4858
4860
  }
4859
4861
  register(r) {
4860
4862
  return this.httpClient.post("/api/auth/account/register", r);
@@ -4873,11 +4875,16 @@ class un {
4873
4875
  const t = await this.httpClient.post("/api/auth/account/reset-password", r);
4874
4876
  this._initUserData(t.data);
4875
4877
  }
4876
- _initUserData({ user: r, token: t }) {
4878
+ _initUserData({
4879
+ user: r,
4880
+ token: t,
4881
+ sessionID: o
4882
+ }) {
4877
4883
  this._setAuthData(t), ie.setState(() => ({
4878
4884
  user: r,
4879
4885
  isAuthenticated: !0,
4880
- isLoaded: !0
4886
+ isLoaded: !0,
4887
+ sessionID: o
4881
4888
  }));
4882
4889
  }
4883
4890
  _setAuthData(r) {
@@ -106,4 +106,4 @@ See https://react.dev/link/invalid-hook-call for tips about how to debug and fix
106
106
  *
107
107
  * This source code is licensed under the ISC license.
108
108
  * See the LICENSE file in the root directory of this source tree.
109
- */const Xo=rr("eye",[["path",{d:"M2.062 12.348a1 1 0 0 1 0-.696 10.75 10.75 0 0 1 19.876 0 1 1 0 0 1 0 .696 10.75 10.75 0 0 1-19.876 0",key:"1nclc0"}],["circle",{cx:"12",cy:"12",r:"3",key:"1v7zrd"}]]);function Jo(e){const[r,t]=O.useState(!1);return T.jsx("div",{className:"flex flex-col gap-6",children:T.jsxs(_o,{children:[T.jsxs(wo,{children:[T.jsx(ko,{className:"text-2xl",children:e.loginFormTitle}),T.jsx(Oo,{children:e.loginFormDescription})]}),T.jsx(So,{children:T.jsx(Go,{...e.form,children:T.jsx("form",{onSubmit:e.form.handleSubmit(e.submit),children:T.jsxs("div",{className:"flex flex-col gap-6",children:[T.jsxs("div",{className:"grid gap-2",children:[T.jsx(Wt,{htmlFor:"email",children:e.emailLabel}),T.jsx(qt,{control:e.form.control,name:"email",render:({field:o})=>T.jsx(Jt,{children:T.jsx(Zt,{children:T.jsx(Qt,{id:"email",type:"email",placeholder:"m@example.com",required:!0,...o})})})})]}),T.jsxs("div",{className:"grid gap-2",children:[T.jsxs("div",{className:"flex items-center",children:[T.jsx(Wt,{htmlFor:"password",children:e.passwordLabel}),e.resetPasswordLinkComponent?e.resetPasswordLinkComponent():null]}),T.jsx(qt,{control:e.form.control,name:"password",render:({field:o})=>T.jsx(Jt,{children:T.jsx(Zt,{className:"relative",children:T.jsxs("div",{className:"relative",children:[r?T.jsx(qo,{className:"absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-500 z-10 cursor-pointer",onClick:()=>{t(!1)}}):T.jsx(Xo,{className:"absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-500 z-10 cursor-pointer",onClick:()=>{t(!0)}}),T.jsx(Qt,{type:r?"text":"password",placeholder:"********",...o,className:"pr-10"})]})})})})]}),T.jsx(xo,{type:"submit",className:"w-full",children:e.submitButtonLabel})]})})})})]})})}const me=new WeakMap,je=new WeakMap,Te={current:[]};let Ke=!1;const Ae=new Set,or=new Map;function nr(e){const r=Array.from(e).sort((t,o)=>t instanceof le&&t.options.deps.includes(o)?1:o instanceof le&&o.options.deps.includes(t)?-1:0);for(const t of r){if(Te.current.includes(t))continue;Te.current.push(t),t.recompute();const o=je.get(t);if(o)for(const n of o){const s=me.get(n);s&&nr(s)}}}function Zo(e){e.listeners.forEach(r=>r({prevVal:e.prevState,currentVal:e.state}))}function Qo(e){e.listeners.forEach(r=>r({prevVal:e.prevState,currentVal:e.state}))}function en(e){if(Ae.add(e),!Ke)try{for(Ke=!0;Ae.size>0;){const r=Array.from(Ae);Ae.clear();for(const t of r){const o=or.get(t)??t.prevState;t.prevState=o,Zo(t)}for(const t of r){const o=me.get(t);o&&(Te.current.push(t),nr(o))}for(const t of r){const o=me.get(t);if(o)for(const n of o)Qo(n)}}}finally{Ke=!1,Te.current=[],or.clear()}}function tn(e){return typeof e=="function"}class qe{constructor(r,t){this.listeners=new Set,this.subscribe=o=>{var n,s;this.listeners.add(o);const d=(s=(n=this.options)==null?void 0:n.onSubscribe)==null?void 0:s.call(n,o,this);return()=>{this.listeners.delete(o),d?.()}},this.prevState=r,this.state=r,this.options=t}setState(r){var t,o,n;this.prevState=this.state,(t=this.options)!=null&&t.updateFn?this.state=this.options.updateFn(this.prevState)(r):tn(r)?this.state=r(this.prevState):this.state=r,(n=(o=this.options)==null?void 0:o.onUpdate)==null||n.call(o),en(this)}}class le{constructor(r){this.listeners=new Set,this._subscriptions=[],this.lastSeenDepValues=[],this.getDepVals=()=>{const t=[],o=[];for(const n of this.options.deps)t.push(n.prevState),o.push(n.state);return this.lastSeenDepValues=o,{prevDepVals:t,currDepVals:o,prevVal:this.prevState??void 0}},this.recompute=()=>{var t,o;this.prevState=this.state;const{prevDepVals:n,currDepVals:s,prevVal:d}=this.getDepVals();this.state=this.options.fn({prevDepVals:n,currDepVals:s,prevVal:d}),(o=(t=this.options).onUpdate)==null||o.call(t)},this.checkIfRecalculationNeededDeeply=()=>{for(const s of this.options.deps)s instanceof le&&s.checkIfRecalculationNeededDeeply();let t=!1;const o=this.lastSeenDepValues,{currDepVals:n}=this.getDepVals();for(let s=0;s<n.length;s++)if(n[s]!==o[s]){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 o,n;this.listeners.add(t);const s=(n=(o=this.options).onSubscribe)==null?void 0:n.call(o,t,this);return()=>{this.listeners.delete(t),s?.()}},this.options=r,this.state=r.fn({prevDepVals:void 0,prevVal:void 0,currDepVals:this.getDepVals().currDepVals})}registerOnGraph(r=this.options.deps){for(const t of r)if(t instanceof le)t.registerOnGraph(),this.registerOnGraph(t.options.deps);else if(t instanceof qe){let o=me.get(t);o||(o=new Set,me.set(t,o)),o.add(this);let n=je.get(this);n||(n=new Set,je.set(this,n)),n.add(t)}}unregisterFromGraph(r=this.options.deps){for(const t of r)if(t instanceof le)this.unregisterFromGraph(t.options.deps);else if(t instanceof qe){const o=me.get(t);o&&o.delete(this);const n=je.get(this);n&&n.delete(t)}}}const Xe="Authorization",sr=()=>({user:{id:null,email:null,roles:[],is_enabled:!1,is_email_verified:!1},isAuthenticated:!1,isLoaded:!1}),ce=new qe(sr()),rn=new le({fn:()=>!!ce.state.user?.roles.find(e=>e.name==="admin"),deps:[ce]});class on{accountStore;httpClient;constructor(r){this.httpClient=r,this.accountStore=ce}initAuthData(){const r=localStorage.getItem("token");r&&(this.httpClient.defaults.headers.common[Xe]=r)}async load(){try{const{data:r}=await this.httpClient.get("/api/auth/account/me");ce.setState(()=>({user:r,isAuthenticated:!0,isLoaded:!0}))}catch{ce.setState(r=>({...r,isLoaded:!0}))}}async login(r){const t=await this.httpClient.post("/api/auth/account/login",r),{user:o,token:n}=t.data;return this._initUserData({user:o,token:n}),o}register(r){return this.httpClient.post("/api/auth/account/register",r)}async activate(r){const t=await this.httpClient.post("/api/auth/account/activate",r);this._initUserData(t.data)}logout(){ce.setState(()=>({...sr(),isLoaded:!0})),this._clearAuthData()}startResetPassword(r){return this.httpClient.post("/api/auth/account/start-reset-password",r)}async resetPassword(r){const t=await this.httpClient.post("/api/auth/account/reset-password",r);this._initUserData(t.data)}_initUserData({user:r,token:t}){this._setAuthData(t),ce.setState(()=>({user:r,isAuthenticated:!0,isLoaded:!0}))}_setAuthData(r){localStorage.setItem("token",r),this.httpClient.defaults.headers.common[Xe]=r}_clearAuthData(){localStorage.removeItem("token"),delete this.httpClient.defaults.headers.common[Xe]}}te.Auth=on,te.LoginForm=Jo,te.isAdmin=rn,Object.defineProperty(te,Symbol.toStringTag,{value:"Module"})}));
109
+ */const Xo=rr("eye",[["path",{d:"M2.062 12.348a1 1 0 0 1 0-.696 10.75 10.75 0 0 1 19.876 0 1 1 0 0 1 0 .696 10.75 10.75 0 0 1-19.876 0",key:"1nclc0"}],["circle",{cx:"12",cy:"12",r:"3",key:"1v7zrd"}]]);function Jo(e){const[r,t]=O.useState(!1);return T.jsx("div",{className:"flex flex-col gap-6",children:T.jsxs(_o,{children:[T.jsxs(wo,{children:[T.jsx(ko,{className:"text-2xl",children:e.loginFormTitle}),T.jsx(Oo,{children:e.loginFormDescription})]}),T.jsx(So,{children:T.jsx(Go,{...e.form,children:T.jsx("form",{onSubmit:e.form.handleSubmit(e.submit),children:T.jsxs("div",{className:"flex flex-col gap-6",children:[T.jsxs("div",{className:"grid gap-2",children:[T.jsx(Wt,{htmlFor:"email",children:e.emailLabel}),T.jsx(qt,{control:e.form.control,name:"email",render:({field:o})=>T.jsx(Jt,{children:T.jsx(Zt,{children:T.jsx(Qt,{id:"email",type:"email",placeholder:"m@example.com",required:!0,...o})})})})]}),T.jsxs("div",{className:"grid gap-2",children:[T.jsxs("div",{className:"flex items-center",children:[T.jsx(Wt,{htmlFor:"password",children:e.passwordLabel}),e.resetPasswordLinkComponent?e.resetPasswordLinkComponent():null]}),T.jsx(qt,{control:e.form.control,name:"password",render:({field:o})=>T.jsx(Jt,{children:T.jsx(Zt,{className:"relative",children:T.jsxs("div",{className:"relative",children:[r?T.jsx(qo,{className:"absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-500 z-10 cursor-pointer",onClick:()=>{t(!1)}}):T.jsx(Xo,{className:"absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-500 z-10 cursor-pointer",onClick:()=>{t(!0)}}),T.jsx(Qt,{type:r?"text":"password",placeholder:"********",...o,className:"pr-10"})]})})})})]}),T.jsx(xo,{type:"submit",className:"w-full",children:e.submitButtonLabel})]})})})})]})})}const me=new WeakMap,je=new WeakMap,Te={current:[]};let Ke=!1;const Ae=new Set,or=new Map;function nr(e){const r=Array.from(e).sort((t,o)=>t instanceof le&&t.options.deps.includes(o)?1:o instanceof le&&o.options.deps.includes(t)?-1:0);for(const t of r){if(Te.current.includes(t))continue;Te.current.push(t),t.recompute();const o=je.get(t);if(o)for(const n of o){const s=me.get(n);s&&nr(s)}}}function Zo(e){e.listeners.forEach(r=>r({prevVal:e.prevState,currentVal:e.state}))}function Qo(e){e.listeners.forEach(r=>r({prevVal:e.prevState,currentVal:e.state}))}function en(e){if(Ae.add(e),!Ke)try{for(Ke=!0;Ae.size>0;){const r=Array.from(Ae);Ae.clear();for(const t of r){const o=or.get(t)??t.prevState;t.prevState=o,Zo(t)}for(const t of r){const o=me.get(t);o&&(Te.current.push(t),nr(o))}for(const t of r){const o=me.get(t);if(o)for(const n of o)Qo(n)}}}finally{Ke=!1,Te.current=[],or.clear()}}function tn(e){return typeof e=="function"}class qe{constructor(r,t){this.listeners=new Set,this.subscribe=o=>{var n,s;this.listeners.add(o);const d=(s=(n=this.options)==null?void 0:n.onSubscribe)==null?void 0:s.call(n,o,this);return()=>{this.listeners.delete(o),d?.()}},this.prevState=r,this.state=r,this.options=t}setState(r){var t,o,n;this.prevState=this.state,(t=this.options)!=null&&t.updateFn?this.state=this.options.updateFn(this.prevState)(r):tn(r)?this.state=r(this.prevState):this.state=r,(n=(o=this.options)==null?void 0:o.onUpdate)==null||n.call(o),en(this)}}class le{constructor(r){this.listeners=new Set,this._subscriptions=[],this.lastSeenDepValues=[],this.getDepVals=()=>{const t=[],o=[];for(const n of this.options.deps)t.push(n.prevState),o.push(n.state);return this.lastSeenDepValues=o,{prevDepVals:t,currDepVals:o,prevVal:this.prevState??void 0}},this.recompute=()=>{var t,o;this.prevState=this.state;const{prevDepVals:n,currDepVals:s,prevVal:d}=this.getDepVals();this.state=this.options.fn({prevDepVals:n,currDepVals:s,prevVal:d}),(o=(t=this.options).onUpdate)==null||o.call(t)},this.checkIfRecalculationNeededDeeply=()=>{for(const s of this.options.deps)s instanceof le&&s.checkIfRecalculationNeededDeeply();let t=!1;const o=this.lastSeenDepValues,{currDepVals:n}=this.getDepVals();for(let s=0;s<n.length;s++)if(n[s]!==o[s]){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 o,n;this.listeners.add(t);const s=(n=(o=this.options).onSubscribe)==null?void 0:n.call(o,t,this);return()=>{this.listeners.delete(t),s?.()}},this.options=r,this.state=r.fn({prevDepVals:void 0,prevVal:void 0,currDepVals:this.getDepVals().currDepVals})}registerOnGraph(r=this.options.deps){for(const t of r)if(t instanceof le)t.registerOnGraph(),this.registerOnGraph(t.options.deps);else if(t instanceof qe){let o=me.get(t);o||(o=new Set,me.set(t,o)),o.add(this);let n=je.get(this);n||(n=new Set,je.set(this,n)),n.add(t)}}unregisterFromGraph(r=this.options.deps){for(const t of r)if(t instanceof le)this.unregisterFromGraph(t.options.deps);else if(t instanceof qe){const o=me.get(t);o&&o.delete(this);const n=je.get(this);n&&n.delete(t)}}}const Xe="Authorization",sr=()=>({user:{id:null,email:null,roles:[],is_enabled:!1,is_email_verified:!1},sessionID:null,isAuthenticated:!1,isLoaded:!1}),ce=new qe(sr()),rn=new le({fn:()=>!!ce.state.user?.roles.find(e=>e.name==="admin"),deps:[ce]});class on{accountStore;httpClient;constructor(r){this.httpClient=r,this.accountStore=ce}initAuthData(){const r=localStorage.getItem("token");r&&(this.httpClient.defaults.headers.common[Xe]=r)}async load(){try{const{data:r}=await this.httpClient.get("/api/auth/account/me");ce.setState(()=>({user:r.user,sessionID:r.session_id,isAuthenticated:!0,isLoaded:!0}))}catch{ce.setState(r=>({...r,isLoaded:!0}))}}async login(r){const t=await this.httpClient.post("/api/auth/account/login",r),{user:o,token:n,session_id:s}=t.data;return this._initUserData({user:o,token:n,sessionID:s}),o}register(r){return this.httpClient.post("/api/auth/account/register",r)}async activate(r){const t=await this.httpClient.post("/api/auth/account/activate",r);this._initUserData(t.data)}logout(){ce.setState(()=>({...sr(),isLoaded:!0})),this._clearAuthData()}startResetPassword(r){return this.httpClient.post("/api/auth/account/start-reset-password",r)}async resetPassword(r){const t=await this.httpClient.post("/api/auth/account/reset-password",r);this._initUserData(t.data)}_initUserData({user:r,token:t,sessionID:o}){this._setAuthData(t),ce.setState(()=>({user:r,isAuthenticated:!0,isLoaded:!0,sessionID:o}))}_setAuthData(r){localStorage.setItem("token",r),this.httpClient.defaults.headers.common[Xe]=r}_clearAuthData(){localStorage.removeItem("token"),delete this.httpClient.defaults.headers.common[Xe]}}te.Auth=on,te.LoginForm=Jo,te.isAdmin=rn,Object.defineProperty(te,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@service_laboratory/auth",
3
- "version": "2.0.8",
3
+ "version": "2.0.9",
4
4
  "type": "module",
5
5
  "main": "./dist/index.umd.cjs",
6
6
  "module": "./dist/index.js",