@tachybase/plugin-auth-sms 1.3.18 → 1.3.20

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,4 +1,4 @@
1
- (function(e,t){typeof exports=="object"&&typeof module!="undefined"?t(exports,require("@tachybase/client"),require("@tachybase/module-auth/client"),require("react/jsx-runtime"),require("react-i18next"),require("antd"),require("react"),require("@tachybase/schema")):typeof define=="function"&&define.amd?define(["exports","@tachybase/client","@tachybase/module-auth/client","react/jsx-runtime","react-i18next","antd","react","@tachybase/schema"],t):(e=typeof globalThis!="undefined"?globalThis:e||self,t(e["@tachybase/plugin-auth-sms"]={},e["@tachybase/client"],e["@tachybase/module-auth"],e.jsxRuntime,e["react-i18next"],e.antd,e.react,e["@tachybase/schema"]))})(this,function(e,t,a,o,d,i,h,l){"use strict";var b=(e,t,a)=>new Promise((o,d)=>{var i=r=>{try{l(a.next(r))}catch(x){d(x)}},h=r=>{try{l(a.throw(r))}catch(x){d(x)}},l=r=>r.done?o(r.value):Promise.resolve(r.value).then(i,h);l((a=a.apply(e,t)).next())});const r="SMS",x="auth-sms";function A(){return d.useTranslation(x)}const M=()=>{const{t:s}=A();return o.jsx(t.SchemaComponent,{scope:{t:s},schema:{type:"object",properties:{sms:{type:"void",properties:{public:{type:"object",properties:{autoSignup:{"x-decorator":"FormItem",type:"boolean",title:'{{t("Sign up automatically when the user does not exist")}}',"x-component":"Checkbox"},agreeMust:{"x-decorator":"FormItem",type:"boolean",title:'{{t("Must agree to the agreement")}}',"x-component":"Checkbox"},agreeCode:{"x-decorator":"FormItem",type:"string",title:'{{t("Agree to the agreement text (html supported)")}}',"x-component":"Input.TextArea"}}}}}}}})};function v({targetFieldName:s="phone",actionType:u,value:p,onChange:n}){const{t:m}=d.useTranslation(),g=t.useAPIClient(),f=l.useForm(),[c,I]=h.useState(0),S=h.useRef(null);h.useEffect(()=>{c<=0&&S.current&&clearInterval(S.current)},[c]);function k(){return b(this,null,function*(){if(!(c>0))try{const{data:{data:y}}=yield g.resource("verifications").create({values:{type:u,phone:f.values[s]}});i.message.success(m("Operation succeeded")),p&&n("");const q=y.expiresAt?Math.ceil((Date.parse(y.expiresAt)-Date.now())/1e3):60;I(q),S.current=setInterval(()=>{I(P=>P-1)},1e3)}catch(y){console.error(y)}})}return o.jsxs("fieldset",{className:t.css`
1
+ (function(e,t){typeof exports=="object"&&typeof module!="undefined"?t(exports,require("@tachybase/client"),require("@tachybase/module-auth/client"),require("react/jsx-runtime"),require("react-i18next"),require("antd"),require("react"),require("@tachybase/schema")):typeof define=="function"&&define.amd?define(["exports","@tachybase/client","@tachybase/module-auth/client","react/jsx-runtime","react-i18next","antd","react","@tachybase/schema"],t):(e=typeof globalThis!="undefined"?globalThis:e||self,t(e["@tachybase/plugin-auth-sms"]={},e["@tachybase/client"],e["@tachybase/module-auth"],e.jsxRuntime,e["react-i18next"],e.antd,e.react,e["@tachybase/schema"]))})(this,function(e,t,a,o,x,i,d,h){"use strict";var b=(e,t,a)=>new Promise((o,x)=>{var i=r=>{try{h(a.next(r))}catch(l){x(l)}},d=r=>{try{h(a.throw(r))}catch(l){x(l)}},h=r=>r.done?o(r.value):Promise.resolve(r.value).then(i,d);h((a=a.apply(e,t)).next())});const r="SMS",l="auth-sms";function C(){return x.useTranslation(l)}const M=()=>{const{t:s}=C();return o.jsx(t.SchemaComponent,{scope:{t:s},schema:{type:"object",properties:{sms:{type:"void",properties:{public:{type:"object",properties:{autoSignup:{"x-decorator":"FormItem",type:"boolean",title:'{{t("Sign up automatically when the user does not exist")}}',"x-component":"Checkbox"},agreeMust:{"x-decorator":"FormItem",type:"boolean",title:'{{t("Must agree to the agreement")}}',"x-component":"Checkbox"},agreeCode:{"x-decorator":"FormItem",type:"string",title:'{{t("Agree to the agreement text (html supported)")}}',"x-component":"Input.TextArea"}}}}}}}})};function v({targetFieldName:s="phone",actionType:u,value:p,onChange:n}){const{t:m}=C(),g=t.useAPIClient(),f=h.useForm(),[c,A]=d.useState(0),S=d.useRef(null);d.useEffect(()=>{c<=0&&S.current&&clearInterval(S.current)},[c]);function k(){return b(this,null,function*(){if(!(c>0))try{const{data:{data:y}}=yield g.resource("verifications").create({values:{type:u,phone:f.values[s]}});i.message.success(m("Operation succeeded")),p&&n("");const q=y.expiresAt?Math.ceil((Date.parse(y.expiresAt)-Date.now())/1e3):60;A(q),S.current=setInterval(()=>{A(P=>P-1)},1e3)}catch(y){console.error(y)}})}return o.jsxs("fieldset",{className:t.css`
2
2
  display: flex;
3
3
  gap: 0.5em;
4
- `,children:[o.jsx(i.Input,{value:p,onChange:n,placeholder:m("Verification code")}),o.jsx(i.Button,{onClick:k,disabled:c>0,children:c>0?m("Retry after {{count}} seconds",{count:c}):m("Send code")})]})}const F=({value:s,onChange:u,htmlCode:p=""})=>o.jsx(i.Checkbox,{checked:s,onChange:n=>u(n.target.checked),children:o.jsx("span",{dangerouslySetInnerHTML:{__html:p}})}),T={type:"object",name:"phoneForm","x-component":"Form",properties:{phone:{type:"string",required:!0,"x-component":"Input","x-validator":"phone","x-decorator":"FormItem","x-component-props":{placeholder:'{{t("Phone")}}',style:{}}},code:{type:"string",required:!0,"x-component":"VerificationCode","x-component-props":{actionType:"auth:signIn",targetFieldName:"phone"},"x-decorator":"FormItem"},agree:{type:"boolean","x-component":"HtmlAgreementCheckbox","x-component-props":{htmlCode:"{{ agreeCode }}"},"x-visible":"{{ agreeMust }}"},actions:{title:'{{t("Sign in")}}',type:"void","x-component":"Action","x-component-props":{htmlType:"submit",block:!0,type:"primary",useAction:"{{ useSMSSignIn }}",style:{width:"100%"}},"x-reactions":[{dependencies:["agree"],fulfill:{state:{disabled:"{{ agreeMust && $deps[0] !== true }}"}}}]},tip:{type:"void","x-component":"div","x-content":'{{t("User will be registered automatically if not exists.", {ns: "auth-sms"})}}',"x-component-props":{style:{color:"#ccc"}},"x-visible":"{{ autoSignup }}"}}},j=s=>{const u=s.authenticator,{name:p,options:n}=u,m=!!(n!=null&&n.autoSignup),g=!!n.agreeMust,f=n.agreeCode,c=()=>a.useSignIn(p);return o.jsx(t.SchemaComponent,{schema:T,scope:{useSMSSignIn:c,autoSignup:m,agreeMust:g,agreeCode:f},components:{VerificationCode:v,HtmlAgreementCheckbox:F}})};class C extends t.Plugin{load(){return b(this,null,function*(){this.app.pm.get(a).registerType(r,{components:{SignInForm:j,AdminSettingsForm:M}})})}}e.SmsAuthPlugin=C,e.default=C,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
4
+ `,children:[o.jsx(i.Input,{value:p,onChange:n,placeholder:m("Verification code")}),o.jsx(i.Button,{onClick:k,disabled:c>0,children:c>0?m("Retry after {{count}} seconds",{count:c}):m("Send code")})]})}const F=({value:s,onChange:u,htmlCode:p=""})=>o.jsx(i.Checkbox,{checked:s,onChange:n=>u(n.target.checked),children:o.jsx("span",{dangerouslySetInnerHTML:{__html:p}})}),T={type:"object",name:"phoneForm","x-component":"Form",properties:{phone:{type:"string",required:!0,"x-component":"Input","x-validator":"phone","x-decorator":"FormItem","x-component-props":{placeholder:'{{t("Phone")}}',style:{}}},code:{type:"string",required:!0,"x-component":"VerificationCode","x-component-props":{actionType:"auth:signIn",targetFieldName:"phone"},"x-decorator":"FormItem"},agree:{type:"boolean","x-component":"HtmlAgreementCheckbox","x-component-props":{htmlCode:"{{ agreeCode }}"},"x-visible":"{{ agreeMust }}"},actions:{title:'{{t("Sign in")}}',type:"void","x-component":"Action","x-component-props":{htmlType:"submit",block:!0,type:"primary",useAction:"{{ useSMSSignIn }}",style:{width:"100%"}},"x-reactions":[{dependencies:["agree"],fulfill:{state:{disabled:"{{ agreeMust && $deps[0] !== true }}"}}}]},tip:{type:"void","x-component":"div","x-content":'{{t("User will be registered automatically if not exists.", {ns: "auth-sms"})}}',"x-component-props":{style:{color:"#ccc"}},"x-visible":"{{ autoSignup }}"}}},j=s=>{const u=s.authenticator,{name:p,options:n}=u,m=!!(n!=null&&n.autoSignup),g=!!n.agreeMust,f=n.agreeCode,c=()=>a.useSignIn(p);return o.jsx(t.SchemaComponent,{schema:T,scope:{useSMSSignIn:c,autoSignup:m,agreeMust:g,agreeCode:f},components:{VerificationCode:v,HtmlAgreementCheckbox:F}})};class I extends t.Plugin{load(){return b(this,null,function*(){this.app.pm.get(a).registerType(r,{components:{SignInForm:j,AdminSettingsForm:M}})})}}e.SmsAuthPlugin=I,e.default=I,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
@@ -1,12 +1,12 @@
1
1
  module.exports = {
2
2
  "react": "18.3.1",
3
- "@tachybase/client": "1.3.18",
4
- "@tachybase/module-auth": "1.3.18",
5
- "@tachybase/schema": "1.3.18",
3
+ "@tachybase/client": "1.3.20",
4
+ "@tachybase/module-auth": "1.3.20",
5
+ "@tachybase/schema": "1.3.20",
6
6
  "antd": "5.22.5",
7
- "react-i18next": "15.2.0",
8
- "@tachybase/plugin-otp": "1.3.18",
9
- "@tachybase/server": "1.3.18",
10
- "@tachybase/auth": "1.3.18",
11
- "@tachybase/database": "1.3.18"
7
+ "@tachybase/plugin-otp": "1.3.20",
8
+ "@tachybase/server": "1.3.20",
9
+ "@tachybase/auth": "1.3.20",
10
+ "@tachybase/database": "1.3.20",
11
+ "react-i18next": "15.2.0"
12
12
  };
@@ -21,7 +21,7 @@ __export(plugin_exports, {
21
21
  default: () => plugin_default
22
22
  });
23
23
  module.exports = __toCommonJS(plugin_exports);
24
- var import_path = require("path");
24
+ var import_node_path = require("node:path");
25
25
  var import_server = require("@tachybase/server");
26
26
  var import_constants = require("../constants");
27
27
  var import_sms_auth = require("./sms-auth");
@@ -31,7 +31,7 @@ class SmsAuthPlugin extends import_server.Plugin {
31
31
  async load() {
32
32
  this.db.addMigrations({
33
33
  namespace: "sms-auth",
34
- directory: (0, import_path.resolve)(__dirname, "migrations"),
34
+ directory: (0, import_node_path.resolve)(__dirname, "migrations"),
35
35
  context: {
36
36
  plugin: this
37
37
  }
@@ -54,6 +54,19 @@ class SmsAuthPlugin extends import_server.Plugin {
54
54
  return true;
55
55
  }
56
56
  });
57
+ verificationPlugin.interceptors.register("auth:changePassword", {
58
+ manual: true,
59
+ getReceiver: (ctx) => {
60
+ return ctx.action.params.values.phone;
61
+ },
62
+ expiresIn: 120,
63
+ validate: async (ctx, phone) => {
64
+ if (!phone) {
65
+ throw new Error(ctx.t("Not a valid cellphone number, please re-enter"));
66
+ }
67
+ return true;
68
+ }
69
+ });
57
70
  this.app.authManager.registerTypes(import_constants.authType, {
58
71
  auth: import_sms_auth.SMSAuth
59
72
  });
@@ -3,4 +3,5 @@ import { Model } from '@tachybase/database';
3
3
  export declare class SMSAuth extends BaseAuth {
4
4
  constructor(config: AuthConfig);
5
5
  validate(): Promise<Model<any, any>>;
6
+ changePassword(): Promise<any>;
6
7
  }
@@ -74,6 +74,45 @@ class SMSAuth extends import_auth.BaseAuth {
74
74
  });
75
75
  return user;
76
76
  }
77
+ async changePassword() {
78
+ const ctx = this.ctx;
79
+ const {
80
+ values: { newPassword, phone, oldPassword, code }
81
+ } = ctx.action.params;
82
+ const verificationPlugin = ctx.app.getPlugin("otp");
83
+ const currentUser = ctx.auth.user;
84
+ if (!currentUser) {
85
+ ctx.throw(401);
86
+ }
87
+ const user = await this.userRepository.findOne({
88
+ where: {
89
+ phone: currentUser.phone
90
+ }
91
+ });
92
+ if (!user) {
93
+ ctx.throw(404, ctx.t("User not found", { ns: import_constants.namespace }));
94
+ }
95
+ const pwd = this.userCollection.getField("password");
96
+ let passwordValid = false;
97
+ let codeValid = false;
98
+ if (user.password && oldPassword) {
99
+ passwordValid = await pwd.verify(oldPassword, user.password);
100
+ }
101
+ if (code && phone) {
102
+ try {
103
+ await verificationPlugin.intercept(ctx, async () => {
104
+ codeValid = true;
105
+ });
106
+ } catch (e) {
107
+ }
108
+ }
109
+ if (!passwordValid && !codeValid) {
110
+ ctx.throw(401, ctx.t("The old password or verification code is incorrect", { ns: import_constants.namespace }));
111
+ }
112
+ user.password = newPassword;
113
+ await user.save();
114
+ return currentUser;
115
+ }
77
116
  }
78
117
  // Annotate the CommonJS export names for ESM import in node:
79
118
  0 && (module.exports = {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tachybase/plugin-auth-sms",
3
3
  "displayName": "Auth: SMS",
4
- "version": "1.3.18",
4
+ "version": "1.3.20",
5
5
  "description": "SMS authentication.",
6
6
  "keywords": [
7
7
  "Authentication"
@@ -13,17 +13,17 @@
13
13
  "antd": "5.22.5",
14
14
  "react": "~18.3.1",
15
15
  "react-i18next": "^15.2.0",
16
- "@tachybase/schema": "1.3.18"
16
+ "@tachybase/schema": "1.3.20"
17
17
  },
18
18
  "peerDependencies": {
19
- "@tachybase/auth": "1.3.18",
20
- "@tachybase/client": "1.3.18",
21
- "@tachybase/actions": "1.3.18",
22
- "@tachybase/database": "1.3.18",
23
- "@tachybase/module-auth": "1.3.18",
24
- "@tachybase/plugin-otp": "1.3.18",
25
- "@tachybase/server": "1.3.18",
26
- "@tachybase/test": "1.3.18"
19
+ "@tachybase/actions": "1.3.20",
20
+ "@tachybase/auth": "1.3.20",
21
+ "@tachybase/client": "1.3.20",
22
+ "@tachybase/database": "1.3.20",
23
+ "@tachybase/module-auth": "1.3.20",
24
+ "@tachybase/plugin-otp": "1.3.20",
25
+ "@tachybase/server": "1.3.20",
26
+ "@tachybase/test": "1.3.20"
27
27
  },
28
28
  "description.zh-CN": "通过短信验证码认证身份。",
29
29
  "displayName.zh-CN": "认证:短信",