UNPKG

@nocobase/plugin-auth

Version:

User authentication management, including password, SMS, and support for Single Sign-On (SSO) protocols, with extensibility.

10 lines (9 loc) 18.9 kB
/** * This file is part of the NocoBase (R) project. * Copyright (c) 2020-2024 NocoBase Co., Ltd. * Authors: NocoBase Team. * * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. * For more information, please refer to: https://www.nocobase.com/agreement. */ "use strict";(self.webpackChunk_nocobase_plugin_auth=self.webpackChunk_nocobase_plugin_auth||[]).push([["847"],{958:function(e,t,n){n.r(t),n.d(t,{Options:function(){return E},useRedirect:function(){return m},useSignUp:function(){return P},useSignIn:function(){return d},SignInForm:function(){return b},SignUpForm:function(){return O}});var r=n(772),o=n(156),i=n.n(o),a=n(748),s=n(128),l=n(505),u=n(581);function c(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function p(e,t,n,r,o,i,a){try{var s=e[i](a),l=s.value}catch(e){n(e);return}s.done?t(l):Promise.resolve(l).then(r,o)}function m(){arguments.length>0&&void 0!==arguments[0]&&arguments[0];var e,t=(0,s.useNavigate)(),n=(e=(0,s.useSearchParams)(),function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n,r,o=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=o){var i=[],a=!0,s=!1;try{for(o=o.call(e);!(a=(n=o.next()).done)&&(i.push(n.value),i.length!==t);a=!0);}catch(e){s=!0,r=e}finally{try{a||null==o.return||o.return()}finally{if(s)throw r}}return i}}(e,1)||function(e,t){if(e){if("string"==typeof e)return c(e,1);var n=Object.prototype.toString.call(e).slice(8,-1);if("Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n)return Array.from(n);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return c(e,t)}}(e,1)||function(){throw TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}())[0];return(0,o.useCallback)(function(){t(n.get("redirect")||"/admin",{replace:!0})},[t,n])}var d=function(e){var t=(0,l.useForm)(),n=(0,r.useAPIClient)(),o=m(),i=(0,r.useCurrentUserContext)().refreshAsync;return{run:function(){var r;return(r=function(){return function(e,t){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){var l=[i,s];if(n)throw TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&l[0]?r.return:l[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,l[1])).done)return o;switch(r=0,o&&(l=[2&l[0],o.value]),l[0]){case 0:case 1:o=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,r=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!(o=(o=a.trys).length>0&&o[o.length-1])&&(6===l[0]||2===l[0])){a=0;continue}if(3===l[0]&&(!o||l[1]>o[0]&&l[1]<o[3])){a.label=l[1];break}if(6===l[0]&&a.label<o[1]){a.label=o[1],o=l;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(l);break}o[2]&&a.ops.pop(),a.trys.pop();continue}l=t.call(e,a)}catch(e){l=[6,e],r=0}finally{n=o=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}}}(this,function(r){switch(r.label){case 0:return[4,t.submit()];case 1:return r.sent(),[4,n.auth.signIn(t.values,e)];case 2:return r.sent(),[4,i()];case 3:return r.sent(),o(),[2]}})},function(){var e=this,t=arguments;return new Promise(function(n,o){var i=r.apply(e,t);function a(e){p(i,n,o,a,s,"next",e)}function s(e){p(i,n,o,a,s,"throw",e)}a(void 0)})})()}}},f=function(e){var t=e.showForgotPassword;return{type:"object",name:"passwordForm","x-component":"FormV2",properties:{account:{type:"string","x-component":"Input","x-validator":'{{(value) => {\n if (!value) {\n return t("Please enter your username or email");\n }\n if (value.includes(\'@\')) {\n if (!/^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$/.test(value)) {\n return t("Please enter a valid email");\n }\n } else {\n return /^[^@.<>"\'/]{1,50}$/.test(value) || t("Please enter a valid username");\n }\n }}}',"x-decorator":"FormItem","x-component-props":{placeholder:'{{t("Username/Email")}}',style:{}}},password:{type:"string","x-component":"Password",required:!0,"x-decorator":"FormItem","x-component-props":{placeholder:'{{t("Password")}}',style:{},showForgotPassword:t}},actions:{type:"void","x-component":"div",properties:{submit:{title:'{{t("Sign in")}}',type:"void","x-component":"Action","x-component-props":{htmlType:"submit",block:!0,type:"primary",useAction:"{{ useBasicSignIn }}",style:{width:"100%"}}}}},links:{type:"void","x-component":"div","x-component-props":{style:{display:"flex",justifyContent:"space-between"}},properties:{signUp:{type:"void","x-component":"Link","x-component-props":{to:"{{ signUpLink }}"},"x-content":'{{t("Create an account")}}',"x-visible":"{{ allowSignUp }}"},forgotPassword:{type:"void","x-component":"Link","x-component-props":{to:'{{"/forgot-password?name=" + authenticator.name}}'},"x-content":'{{t("Forgot password")}}',"x-visible":t}}}}}},b=function(e){var t=(0,a.o$)().t,o=e.authenticator,s=o.authType,l=o.name,c=o.options,p=!!(0,r.useLazy)(function(){return u("imported_-1ja1ffa_component",n.e("257").then(n.bind(n,852)))},"useSignUpForms")()[s]&&null!=c&&!!c.allowSignUp,m="/signup?name=".concat(l);return i().createElement(r.SchemaComponent,{schema:f({showForgotPassword:!!(null==c?void 0:c.enableResetPassword)}),scope:{useBasicSignIn:function(){return d(l)},allowSignUp:p,signUpLink:m,t:t,authenticator:o}})},y=n(875),v=n(721),h=n(238),x=n(446);function g(e,t,n,r,o,i,a){try{var s=e[i](a),l=s.value}catch(e){n(e);return}s.done?t(l):Promise.resolve(l).then(r,o)}function w(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),r.forEach(function(t){var r;r=n[t],t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r})}return e}function S(e,t){return t=null!=t?t:{},Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):(function(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);n.push.apply(n,r)}return n})(Object(t)).forEach(function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}),e}var P=function(e){var t=(0,s.useNavigate)(),n=(0,l.useForm)(),o=(0,r.useAPIClient)(),i=(0,h.useTranslation)().t;return{run:function(){var r;return(r=function(){var r;return function(e,t){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){var l=[i,s];if(n)throw TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&l[0]?r.return:l[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,l[1])).done)return o;switch(r=0,o&&(l=[2&l[0],o.value]),l[0]){case 0:case 1:o=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,r=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!(o=(o=a.trys).length>0&&o[o.length-1])&&(6===l[0]||2===l[0])){a=0;continue}if(3===l[0]&&(!o||l[1]>o[0]&&l[1]<o[3])){a.label=l[1];break}if(6===l[0]&&a.label<o[1]){a.label=o[1],o=l;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(l);break}o[2]&&a.ops.pop(),a.trys.pop();continue}l=t.call(e,a)}catch(e){l=[6,e],r=0}finally{n=o=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}}}(this,function(a){switch(a.label){case 0:return[4,n.submit()];case 1:return a.sent(),[4,o.auth.signUp(n.values,null==e?void 0:e.authenticator)];case 2:return a.sent(),v.message.success((null==e||null==(r=e.message)?void 0:r.success)||i("Sign up successfully, and automatically jump to the sign in page")),setTimeout(function(){t("/signin")},2e3),[2]}})},function(){var e=this,t=arguments;return new Promise(function(n,o){var i=r.apply(e,t);function a(e){g(i,n,o,a,s,"next",e)}function s(e){g(i,n,o,a,s,"throw",e)}a(void 0)})})()}}},O=function(e){var t=e.authenticatorName,n=(0,a.o$)().t,u=(0,h.useTranslation)("lm-collections").t,c=(0,x.u)(t).options,p=c.signupForm,m=(0,o.useMemo)(function(){return p.filter(function(e){return e.show}).reduce(function(e,t){var r=S(w({},t.uiSchema),{title:t.uiSchema.title?u(l.Schema.compile(t.uiSchema.title,{t:n})):""});return e[t.field]=S(w({},r),{required:t.required,"x-decorator":"FormItem"}),e},{})},[p]);if(!(null==c?void 0:c.allowSignUp))return i().createElement(s.Navigate,{to:"/not-found",replace:!0});var d={type:"object",name:(0,y.uid)(),"x-component":"FormV2",properties:S(w({},m),{password:{type:"string",required:!0,title:'{{t("Password")}}',"x-component":"Password","x-decorator":"FormItem","x-validator":{password:!0},"x-component-props":{checkStrength:!0,style:{}},"x-reactions":[{dependencies:[".confirm_password"],fulfill:{state:{selfErrors:'{{$deps[0] && $self.value && $self.value !== $deps[0] ? t("Password mismatch") : ""}}'}}}]},confirm_password:{type:"string",required:!0,"x-component":"Password","x-decorator":"FormItem",title:'{{t("Confirm password")}}',"x-validator":{password:!0},"x-component-props":{style:{}},"x-reactions":[{dependencies:[".password"],fulfill:{state:{selfErrors:'{{$deps[0] && $self.value && $self.value !== $deps[0] ? t("Password mismatch") : ""}}'}}}]},actions:{type:"void","x-component":"div",properties:{submit:{title:'{{t("Sign up")}}',type:"void","x-component":"Action","x-component-props":{block:!0,type:"primary",htmlType:"submit",useAction:"{{ useBasicSignUp }}",style:{width:"100%"}}}}},link:{type:"void","x-component":"div",properties:{link:{type:"void","x-component":"Link","x-component-props":{to:"/signin"},"x-content":'{{t("Log in with an existing account")}}'}}}})};return i().createElement(r.SchemaComponent,{schema:d,scope:{useBasicSignUp:function(){return P({authenticator:t})},t:n}})},j=n(632);function F(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function C(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function T(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),r.forEach(function(t){C(e,t,n[t])})}return e}function k(e,t){return t=null!=t?t:{},Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):(function(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);n.push.apply(n,r)}return n})(Object(t)).forEach(function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}),e}function A(e){return function(e){if(Array.isArray(e))return F(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||function(e,t){if(e){if("string"==typeof e)return F(e,void 0);var n=Object.prototype.toString.call(e).slice(8,-1);if("Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n)return Array.from(n);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return F(e,t)}}(e)||function(){throw TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}var I=function(){var e=(0,r.useRecord)(),t=(0,r.useCollectionManager)().getCollection("users").fields.filter(function(e){var t;return!e.hidden&&!e.target&&e.interface&&!(null==(t=e.uiSchema)?void 0:t["x-read-pretty"])}),n=t.map(function(e){var t;return{value:e.name,label:null==(t=e.uiSchema)?void 0:t.title}}),s=(0,o.useMemo)(function(){var n=((null==(s=e.options)||null==(a=s.public)?void 0:a.signupForm)||[]).filter(function(e){return t.find(function(t){return t.name===e.field})}),r=!0,o=!1,i=void 0;try{for(var a,s,l,u=t[Symbol.iterator]();!(r=(l=u.next()).done);r=!0)!function(){var e=l.value;n.find(function(t){return t.field===e.name})||n.push({field:e.name,show:"username"===e.name,required:"username"===e.name})}()}catch(e){o=!0,i=e}finally{try{r||null==u.return||u.return()}finally{if(o)throw i}}return n},[t,e]);return(0,o.useEffect)(function(){var t;e.options=k(T({},e.options),{public:k(T({},null==(t=e.options)?void 0:t.public),{signupForm:s})})},[e,s]),i().createElement(r.SchemaComponent,{components:{ArrayTable:j.ArrayTable},schema:{type:"void",properties:{"public.signupForm":{title:'{{t("Sign up form")}}',type:"array","x-decorator":"FormItem","x-component":"ArrayTable","x-component-props":{bordered:!1},"x-validator":"{{ (value) => {\n const check = value?.some((item) => ['username', 'email'].includes(item.field) && item.show && item.required);\n if (!check) {\n return t('At least one of the username or email fields is required');\n }\n} }}",default:s,items:{type:"object","x-decorator":"ArrayItems.Item",properties:{column0:{type:"void","x-component":"ArrayTable.Column","x-component-props":{width:20,align:"center"},properties:{sort:{type:"void","x-component":"ArrayTable.SortHandle"}}},column1:{type:"void","x-component":"ArrayTable.Column","x-component-props":{width:100,title:(0,a.KQ)("Field")},properties:{field:{type:"string","x-decorator":"FormItem","x-component":"Select",enum:n,"x-read-pretty":!0}}},column2:{type:"void","x-component":"ArrayTable.Column","x-component-props":{width:80,title:(0,a.KQ)("Show")},properties:{show:{type:"boolean","x-decorator":"FormItem","x-component":"Checkbox","x-reactions":{dependencies:[".required"],fulfill:{state:{value:"{{ $deps[0] || $self.value }}"}}}}}},column3:{type:"void","x-component":"ArrayTable.Column","x-component-props":{width:80,title:(0,a.KQ)("Required")},properties:{required:{type:"boolean","x-decorator":"FormItem","x-component":"Checkbox","x-reactions":{dependencies:[".show"],fulfill:{state:{value:"{{ !$deps[0] ? false : $self.value }}"}}}}}}}}}}}})},$=function(){var e=(0,a.o$)().t;return[(0,r.useGlobalVariable)("$env"),(0,r.useCurrentUserVariable)({maxDepth:1}).currentUserSettings,(0,r.useSystemSettingsVariable)().systemSettings,{value:"$resetLink",label:e("Reset password link")},{value:"$resetLinkExpiration",label:e("Reset link expiration (minutes)")}].filter(Boolean)},E=function(){var e=(0,a.o$)().t,t=$(),n=i().createElement("span",null,e("No notification channels found. Please "),i().createElement(s.Link,{to:"/admin/settings/notification-manager/channels"},e("add one first")),".");return i().createElement(r.SchemaComponent,{scope:{t:e},components:{Alert:v.Alert,SignupFormSettings:I,FormTab:j.FormTab},schema:{type:"object",properties:{notice:{type:"void","x-decorator":"FormItem","x-component":"Alert","x-component-props":{showIcon:!0,message:'{{t("The authentication allows users to sign in via username or email.")}}'}},collapse:{type:"void","x-component":"FormTab",properties:{basic:{type:"void","x-component":"FormTab.TabPane","x-component-props":{tab:(0,a.KQ)("Sign up settings")},properties:C({"public.allowSignUp":{"x-decorator":"FormItem",type:"boolean",title:'{{t("Allow to sign up")}}',"x-component":"Checkbox",default:!0}},(0,y.uid)(),{type:"void","x-component":"SignupFormSettings"})},forgetPassword:{type:"void","x-component":"FormTab.TabPane","x-component-props":{tab:(0,a.KQ)("Forgot password")},properties:{"public.enableResetPassword":{"x-decorator":"FormItem",type:"boolean",title:'{{t("Enable forget password")}}',"x-component":"Checkbox",default:!1},divider1:{type:"void","x-component":function(){return i().createElement(v.Divider,{orientation:"left",orientationMargin:"0"},e("1. Select notification channel"))},"x-reactions":[{dependencies:[".public.enableResetPassword"],fulfill:{state:{visible:"{{$deps[0]}}"}}}]},notificationChannel:{type:"string",title:'{{t("Notification channel (Email)")}}',required:!0,"x-decorator":"FormItem","x-component":"RemoteSelect","x-component-props":{multiple:!1,manual:!1,fieldNames:{label:"title",value:"name"},service:{resource:"notificationChannels",action:"list",params:{filter:{notificationType:"email"}}},notFoundContent:n},"x-reactions":[{dependencies:[".public.enableResetPassword"],fulfill:{state:{visible:"{{$deps[0]}}"}}}],description:'{{t("The notification channel used to send the reset password email, only support email channel")}}'},divider2:{type:"void","x-component":function(){return i().createElement(v.Divider,{orientation:"left",orientationMargin:"0"},e("2. Configure reset email"))},"x-reactions":[{dependencies:[".public.enableResetPassword"],fulfill:{state:{visible:"{{$deps[0]}}"}}}]},emailSubject:{type:"string",required:!0,title:'{{t("Subject")}}',"x-decorator":"FormItem","x-component":"Variable.TextArea","x-component-props":{scope:A(t)},"x-reactions":[{dependencies:[".public.enableResetPassword"],fulfill:{state:{visible:"{{$deps[0]}}",initialValue:'{{t("defaultResetPasswordEmailSubject")}}'}}}],default:'{{t("defaultResetPasswordEmailSubject")}}'},emailContentType:{type:"string",title:'{{t("Content type")}}',required:!0,"x-decorator":"FormItem","x-component":"Radio.Group",enum:[{label:"HTML",value:"html"},{label:'{{t("Plain text")}}',value:"text"}],default:"html","x-reactions":[{dependencies:[".public.enableResetPassword"],fulfill:{state:{visible:"{{$deps[0]}}",initialValue:"html"}}}]},emailContentHTML:{type:"string",required:!0,title:'{{t("Content")}}',"x-decorator":"FormItem","x-component":"Variable.RawTextArea","x-component-props":{scope:A(t),placeholder:"Hi,",autoSize:{minRows:10}},"x-reactions":[{dependencies:[".public.enableResetPassword",".emailContentType"],fulfill:{state:{visible:'{{$deps[0] && $deps[1] === "html"}}',initialValue:'{{t("defaultResetPasswordEmailContentHTML")}}'}}}],default:'{{t("defaultResetPasswordEmailContentHTML")}}'},emailContentText:{type:"string",required:!0,title:'{{t("Content")}}',"x-decorator":"FormItem","x-component":"Variable.RawTextArea","x-component-props":{scope:A(t),autoSize:{minRows:10}},"x-reactions":[{dependencies:[".public.enableResetPassword",".emailContentType"],fulfill:{state:{visible:'{{$deps[0] && $deps[1] === "text"}}',initialValue:'{{t("defaultResetPasswordEmailContentText")}}'}}}],default:'{{t("defaultResetPasswordEmailContentText")}}'},resetTokenExpiresIn:{type:"number",title:'{{t("Reset link expiration")}}',"x-decorator":"FormItem","x-component":"InputNumber","x-component-props":{suffix:e("Minutes"),style:{width:"100%"}},default:120,required:!0,"x-reactions":[{dependencies:[".public.enableResetPassword"],fulfill:{state:{visible:"{{$deps[0]}}",initialValue:120}}}]}}}}}}}})}}}]);