@plasmicapp/auth-react
Advanced tools
Comparing version 0.0.13 to 0.0.14
@@ -0,0 +0,0 @@ import { PlasmicUser } from '@plasmicapp/auth-api'; |
export * from '@plasmicapp/auth-api'; | ||
export * from './hooks'; |
@@ -0,8 +1,191 @@ | ||
"use strict"; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default")); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var __async = (__this, __arguments, generator) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator = generator.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
'use strict' | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
usePlasmicAuth: () => usePlasmicAuth | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
__reExport(src_exports, require("@plasmicapp/auth-api"), module.exports); | ||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./auth-react.cjs.production.min.js') | ||
} else { | ||
module.exports = require('./auth-react.cjs.development.js') | ||
// src/hooks.ts | ||
var import_auth_api = require("@plasmicapp/auth-api"); | ||
var import_query = require("@plasmicapp/query"); | ||
var storageUserKey = (appId) => `$user.${appId}`; | ||
var isBrowser = typeof window !== "undefined"; | ||
function getCallbackParams() { | ||
const params = new URLSearchParams(window.location.search); | ||
const error = params.get("error"); | ||
const code = params.get("code"); | ||
const state = params.get("state"); | ||
return { | ||
isCallbackError: !!error, | ||
isCodeExchange: !!code && !!state, | ||
error, | ||
code, | ||
state | ||
}; | ||
} | ||
function getCodeVerifier() { | ||
try { | ||
return localStorage.getItem("code_verifier"); | ||
} catch (err) { | ||
return null; | ||
} | ||
} | ||
function removeCallbackParams() { | ||
try { | ||
window.history.replaceState({}, "", location.pathname); | ||
} catch (err) { | ||
console.error(`Error while removing callback params: ${err}`); | ||
} | ||
} | ||
function isContinueToSameLocation(continueTo) { | ||
const pathname = window.location.pathname; | ||
const origin = window.location.origin; | ||
return continueTo === pathname || continueTo === origin + pathname; | ||
} | ||
function handleCallback(opts) { | ||
return __async(this, null, function* () { | ||
const { host, appId, code, state, codeVerifier } = opts; | ||
let continueTo = "/"; | ||
try { | ||
if (state) { | ||
const parsedState = JSON.parse(state); | ||
continueTo = parsedState.continueTo; | ||
} | ||
} catch (err) { | ||
console.error(`Error while parsing state: ${err}`); | ||
} | ||
const result = yield (0, import_auth_api.getPlasmicAppUser)({ | ||
host, | ||
appId, | ||
code, | ||
codeVerifier | ||
}); | ||
if (result.error) { | ||
console.log(`Error while performing code exchange: ${result.error}`); | ||
return void 0; | ||
} | ||
localStorage.setItem(storageUserKey(appId), result.token); | ||
if (!isContinueToSameLocation(continueTo)) { | ||
window.location.assign(continueTo); | ||
} else { | ||
removeCallbackParams(); | ||
} | ||
return { token: result.token, user: result.user }; | ||
}); | ||
} | ||
function checkAlreadyLoggedUser(opts) { | ||
return __async(this, null, function* () { | ||
const { appId, host } = opts; | ||
const token = localStorage.getItem(storageUserKey(appId)); | ||
if (!token) { | ||
return { user: null, token: null }; | ||
} | ||
const { user, error } = yield (0, import_auth_api.getPlasmicAppUserFromToken)({ | ||
host, | ||
token | ||
}); | ||
if (error) { | ||
localStorage.removeItem(storageUserKey(appId)); | ||
console.log(`Error while checking logged user`); | ||
return { user: null, token: null }; | ||
} | ||
return { user, token }; | ||
}); | ||
} | ||
function usePlasmicAuth(opts) { | ||
const { host, appId } = opts; | ||
const authKey = `$csq$plasmic-auth-${appId}`; | ||
const { data: userData, isLoading } = (0, import_query.useMutablePlasmicQueryData)( | ||
authKey, | ||
() => __async(this, null, function* () { | ||
if (!appId || !isBrowser) { | ||
return { user: null, token: null }; | ||
} | ||
try { | ||
const callbackParams = getCallbackParams(); | ||
if (callbackParams.isCallbackError || callbackParams.isCodeExchange) { | ||
if (callbackParams.isCallbackError) { | ||
removeCallbackParams(); | ||
console.error(`Error: ${callbackParams.error}`); | ||
return { user: null, token: null }; | ||
} else { | ||
const codeVerifier = getCodeVerifier(); | ||
if (!codeVerifier) { | ||
removeCallbackParams(); | ||
console.error("No code verifier found"); | ||
return { user: null, token: null }; | ||
} else { | ||
const result = yield handleCallback({ | ||
host, | ||
appId, | ||
code: callbackParams.code, | ||
state: callbackParams.state, | ||
codeVerifier | ||
}); | ||
if (!result) { | ||
removeCallbackParams(); | ||
return { user: null, token: null }; | ||
} | ||
return result; | ||
} | ||
} | ||
} else { | ||
return yield checkAlreadyLoggedUser({ | ||
appId, | ||
host | ||
}); | ||
} | ||
} catch (err) { | ||
console.error(`Error while handling auth: ${err}`); | ||
} | ||
return { user: null, token: null }; | ||
}) | ||
); | ||
return { | ||
user: userData == null ? void 0 : userData.user, | ||
token: userData == null ? void 0 : userData.token, | ||
isUserLoading: isLoading | ||
}; | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"types": "./dist/index.d.ts", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.esm.js", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
"import": "./dist/index.esm.js", | ||
"require": "./dist/index.js" | ||
} | ||
@@ -19,7 +21,7 @@ }, | ||
"scripts": { | ||
"start": "tsdx watch", | ||
"build": "tsdx build", | ||
"yalcp": "yalc publish --push", | ||
"build": "yarn build:types && yarn build:index", | ||
"build:types": "yarn tsc", | ||
"build:index": "node ../../build.mjs ./src/index.ts", | ||
"test": "yarn --cwd=../.. test", | ||
"lint": "tsdx lint", | ||
"lint": "eslint", | ||
"prepare": "if-env PREPARE_NO_BUILD=true || yarn build", | ||
@@ -29,37 +31,14 @@ "size": "size-limit", | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "tsdx lint" | ||
} | ||
}, | ||
"prettier": { | ||
"printWidth": 80, | ||
"semi": true, | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
}, | ||
"name": "@plasmicapp/auth-react", | ||
"author": "Chung Wu", | ||
"module": "dist/auth-react.esm.js", | ||
"size-limit": [ | ||
{ | ||
"path": "dist/auth-react.cjs.production.min.js", | ||
"path": "dist/index.js", | ||
"limit": "10 KB" | ||
}, | ||
{ | ||
"path": "dist/auth-react.esm.js", | ||
"limit": "10 KB" | ||
} | ||
], | ||
"devDependencies": { | ||
"@size-limit/preset-small-lib": "^4.11.0", | ||
"husky": "^6.0.0", | ||
"size-limit": "^4.11.0", | ||
"tsdx": "^0.14.1", | ||
"tslib": "^2.2.0" | ||
}, | ||
"dependencies": { | ||
"@plasmicapp/auth-api": "0.0.10", | ||
"@plasmicapp/auth-api": "0.0.11", | ||
"@plasmicapp/isomorphic-unfetch": "1.0.3", | ||
"@plasmicapp/query": "0.1.72" | ||
"@plasmicapp/query": "0.1.73" | ||
}, | ||
@@ -69,3 +48,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "3b71c9fba9c00aea3d7197feefc45da8813b7e7a", | ||
"gitHead": "061bfb393e0e5faabf92ef07b21e60107a5e7971", | ||
"peerDependencies": { | ||
@@ -72,0 +51,0 @@ "react": ">=16.8.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
0
2
1
37370
10
563
1
+ Added@plasmicapp/auth-api@0.0.11(transitive)
+ Added@plasmicapp/query@0.1.73(transitive)
- Removed@plasmicapp/auth-api@0.0.10(transitive)
- Removed@plasmicapp/query@0.1.72(transitive)
Updated@plasmicapp/auth-api@0.0.11
Updated@plasmicapp/query@0.1.73