@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
2 lines • 2.42 kB
JavaScript
import{copy as e}from"../../libs/i18n/copy.mjs";import t from"../../constants/constants.mjs";import n from"../../libs/repositories/user-auth-providers.mjs";import r from"../../libs/repositories/users.mjs";import i from"../security-audit/log-security-audit.mjs";import a from"../../libs/formatters/index.mjs";const o=async(o,s)=>{let c=new r(o.db.client,o.config.db),l=new n(o.db.client,o.config.db),u=o.config.auth.password.enabled===!0,[d,f,p]=await Promise.all([c.selectSinglePreset({tenantKey:o.request.tenantKey,where:[{key:`id`,operator:`=`,value:s.targetUserId}],validation:{enabled:!0,defaultError:{status:404,message:e(`server:core.user.not.found.message`)}}}),l.selectSingle({select:[`id`],where:[{key:`user_id`,operator:`=`,value:s.targetUserId},{key:`provider_key`,operator:`=`,value:s.providerKey}],validation:{enabled:!0,defaultError:{status:404,name:e(`server:core.auth.providers.link.not.found.name`),message:e(`server:core.auth.providers.link.not.found.message`)}}}),l.count({where:[{key:`user_id`,operator:`=`,value:s.targetUserId}]})]);if(d.error)return d;if(f.error)return f;if(p.error)return p;let m=a.parseCount(p.data?.count)<=1,h=!!d.data.password;if(m&&u===!1)return{error:{type:`basic`,status:400,name:e(`server:core.auth.providers.cannot.remove.last.link.name`),message:e(`server:core.auth.providers.cannot.remove.last.link.password.disabled.message`)},data:void 0};if(m&&h===!1)return{error:{type:`basic`,status:400,name:e(`server:core.auth.providers.cannot.remove.last.link.name`),message:e(`server:core.auth.providers.cannot.remove.last.link.no.password.message`)},data:void 0};let[g,_,v]=await Promise.all([l.deleteSingle({returning:[`id`],where:[{key:`user_id`,operator:`=`,value:s.targetUserId},{key:`provider_key`,operator:`=`,value:s.providerKey}],validation:{enabled:!0,defaultError:{status:404,name:e(`server:core.auth.providers.link.not.found.name`),message:e(`server:core.auth.providers.link.not.found.message`)}}}),c.updateSingle({data:{updated_at:new Date().toISOString()},where:[{key:`id`,operator:`=`,value:s.targetUserId}],returning:[`id`],validation:{enabled:!0,defaultError:{status:500}}}),i(o,{userId:s.targetUserId,action:t.securityAudit.actions.authProviderUnlink,performedBy:s.auth.id,previousValue:s.providerKey,newValue:`unlinked`})]);return g.error?g:_.error?_:v.error?v:{error:void 0,data:void 0}};export{o as default};
//# sourceMappingURL=unlink-auth-provider.mjs.map