@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
2 lines • 2.61 kB
JavaScript
import{copy as e}from"../../../libs/i18n/copy.mjs";import t from"../../../constants/constants.mjs";import n from"../../../utils/helpers/generate-secret.mjs";import r from"../../../libs/repositories/user-tokens.mjs";import i from"../../../libs/repositories/users.mjs";import a from"../../../utils/helpers/hash-user-token.mjs";import o from"../../../libs/formatters/index.mjs";import{scrypt as s}from"@noble/hashes/scrypt.js";const c=async(c,l)=>{if(c.config.auth.password.enabled===!1)return{error:{type:`basic`,status:400,message:e(`server:core.auth.password.authentication.disabled.message`)},data:void 0};let u=new r(c.db.client,c.config.db),d=new i(c.db.client,c.config.db),f=a(l.token),p=await u.selectSingle({select:[`id`,`user_id`],where:[{key:`token`,operator:`=`,value:f},{key:`token_type`,operator:`=`,value:t.userTokens.invitation},{key:`expiry_date`,operator:`>`,value:new Date().toISOString()},{key:`revoked_at`,operator:`is`,value:null},{key:`consumed_at`,operator:`is`,value:null}],validation:{enabled:!0,defaultError:{message:e(`server:core.tokens.not.found.message`),status:404}}});if(p.error)return p;let m=await d.selectSingle({select:[`id`,`invitation_accepted`],where:[{key:`id`,operator:`=`,value:p.data.user_id}],validation:{enabled:!0,defaultError:{status:404,message:e(`server:core.user.not.found.message`)}}});if(m.error)return m;if(o.formatBoolean(m.data?.invitation_accepted))return{error:{type:`basic`,status:400,name:e(`server:core.auth.invitations.user.already.accepted.name`),message:e(`server:core.auth.invitations.user.already.accepted.message`)},data:void 0};let h=new Date().toISOString(),g=await u.updateSingle({data:{consumed_at:h,revoked_at:h,revoke_reason:t.userTokenRevokeReasons.invitationAccepted,expiry_date:h},where:[{key:`id`,operator:`=`,value:p.data.id},{key:`token_type`,operator:`=`,value:t.userTokens.invitation},{key:`expiry_date`,operator:`>`,value:h},{key:`revoked_at`,operator:`is`,value:null},{key:`consumed_at`,operator:`is`,value:null}],returning:[`id`]});if(g.error)return g;if(!g.data)return{error:{type:`basic`,message:e(`server:core.tokens.not.found.message`),status:404},data:void 0};let{secret:_,encryptSecret:v}=n(c.config.secrets.encryption),y=Buffer.from(s(l.password,_,t.scrypt)).toString(`base64`),b=await d.updateSingle({data:{password:y,secret:v,invitation_accepted:!0,updated_at:new Date().toISOString()},where:[{key:`id`,operator:`=`,value:p.data.user_id}],returning:[`id`],validation:{enabled:!0,defaultError:{status:400}}});return b.error?b:{error:void 0,data:void 0}};export{c as default};
//# sourceMappingURL=accept-invitation.mjs.map