@agnostack/next-shopify
Version:
Please contact agnoStack via info@agnostack.com for any questions
67 lines • 3.57 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleAuth = void 0;
const utils_1 = require("../../utils");
const shared_1 = require("../../../shared");
// NOTE: typically handling: "/api/auth/online" and "/api/auth/offline"
const handleAuth = (serverRuntimeConfig, data) => {
const { PAGE_PATHS, API_PATHS, API_URLS, APP_CONFIG, } = serverRuntimeConfig;
const { returnResponse = true } = data !== null && data !== void 0 ? data : {};
return (req, res, params) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const { isOnline } = params !== null && params !== void 0 ? params : {};
const _b = (_a = req.query) !== null && _a !== void 0 ? _a : {}, { reAuth: _reAuth } = _b, query = __rest(_b, ["reAuth"]);
const { embedded, state, host, shop: _shop } = query;
const reAuth = (0, shared_1.isTrue)(_reAuth, 'undefined');
const shopify = (0, utils_1.getInitializedShopify)(serverRuntimeConfig);
const shop = shopify.utils.sanitizeShop(_shop);
const handleResponse = (body, statusCode = 307) => {
if (returnResponse) {
return res.redirect(statusCode, body);
}
return { statusCode, body };
};
if ((0, shared_1.stringEmpty)(shop)) {
return handleResponse(`${PAGE_PATHS[shared_1.PAGE_ROUTE_NAMES.ERROR]}?code=no_shop_provided&type=handleAuth`);
}
if (embedded === '1') {
const callbackUrl = (0, utils_1.getCallback)(API_URLS, { reAuth, isOnline });
// TODO: explore objectToQuerystring
const redirectUriParams = new URLSearchParams(Object.assign({ shop,
host }, state && { state })).toString();
const queryParams = new URLSearchParams(Object.assign(Object.assign({}, query), { shop, redirectUri: `${callbackUrl}?${redirectUriParams}` })).toString();
return handleResponse(`${PAGE_PATHS[shared_1.PAGE_ROUTE_NAMES.EXIT]}?${queryParams}`);
}
return (0, utils_1.beginAuth)({
shop,
state,
isOnline,
callbackPath: (0, utils_1.getCallback)(API_PATHS, { reAuth, isOnline }),
rawRequest: req,
rawResponse: res,
config: APP_CONFIG,
});
});
};
exports.handleAuth = handleAuth;
//# sourceMappingURL=auth.js.map