UNPKG

next-firebase-auth

Version:

Simple Firebase authentication for all Next.js rendering strategies

1 lines 1.62 kB
import{get}from"lodash/object";const funcName="setAuthCookies",originalPropertyName="AuthUser",newPropertyName="user",findParentVarDeclaratorPath=e=>{const t=e.value.type;return["ExpressionStatement","BlockStatement"].includes(t)||!e.parentPath?null:"VariableDeclarator"===t?e:findParentVarDeclaratorPath(e.parentPath)},findFirstUseOfVariableInPath=(e,t,r)=>{let a;return e(t).find(e.Identifier).forEach((e=>{get(e,"value.loc.identifierName")===r&&(a=e)})),a},findResultingVarObjPath=(e,t)=>{const{type:r}=t.value.id||{};if(!r)return null;if("ObjectPattern"===r)return t;if("Identifier"===r){const r=t.value.id.loc.identifierName,a=findFirstUseOfVariableInPath(e,t.parentPath.parentPath.parentPath,r);return findResultingVarObjPath(e,findParentVarDeclaratorPath(a.parentPath))}return null};export default function transformer(e,t,r){const{jscodeshift:a}=t,n=a(e.source);let i=!1;return n.find(a.ImportDeclaration).forEach((e=>{e.node.source.value.match(/^next-firebase-auth$/)&&e.node.specifiers.forEach((e=>{"ImportSpecifier"===e.type&&e.imported.name===funcName&&(i=!0)}))})),i?n.find(a.CallExpression,{callee:{name:funcName}}).forEach((e=>{const t=get(e,"parentPath.parentPath.value.arguments[0]");let r=e;if(t&&["FunctionExpression","ArrowFunctionExpression"].includes(t.type)){const e=t.params[0];r=findFirstUseOfVariableInPath(a,t.body,e.loc.identifierName)}const n=findParentVarDeclaratorPath(r);if(!n)return;const i=findResultingVarObjPath(a,n);if(!i||!i.value)return;const o=i.value.id.properties.find((e=>"AuthUser"===e.key.name));o&&(o.key.name="user",o.shorthand=!1)})).toSource(r.printOptions):e.source}