Unable to create the custom session storage.

RobertVilbert
Shopify Partner
2 0 2

Hi All,

 

I am new to shopify platform.

 

I have created a sample embedded app on to my store. I am trying to create the custom session storage i am getting the below error.

 

 

// @TS-check
import { resolve } from "path";
import express from "express";
import cookieParser from "cookie-parser";
import { Shopify, ApiVersion } from "@shopify/shopify-api";
import "dotenv/config";

import applyAuthMiddleware from "./middleware/auth.js";
import verifyRequest from "./middleware/verify-request.js";

import fs from 'fs';
import { Session } from "@shopify/shopify-api/dist/auth/session";

const USE_ONLINE_TOKENS = true;
const TOP_LEVEL_OAUTH_COOKIE = "shopify_top_level_oauth";

const PORT = parseInt(process.env.PORT || "8081", 10);
const isTest = process.env.NODE_ENV === "test" || !!process.env.VITE_TEST_BUILD;


// build custom session
function storeCallback(session) {
    console.log('storeCallback', session);
     fs.writeFileSync('./session.json', JSON.stringify(session));
    return true;
}

function loadCallback(id) {
    console.log('loadCallback', id);
         const sessionResult = fs.readFileSync('./session.json', 'utf-8')
         return Object.assign(
            new Session, JSON.parse(sessionResult)
        )
}

function deleteCallback(id) {
    console.log('deleteCallback', id);
}


const sessionStorage = new Shopify.Session.CustomSessionStorage(storeCallback, loadCallback, deleteCallback);

Shopify.Context.initialize({
    API_KEY: process.env.SHOPIFY_API_KEY,
    API_SECRET_KEY: process.env.SHOPIFY_API_SECRET,
    SCOPES: process.env.SCOPES.split(","),
    HOST_NAME: process.env.HOST.replace(/https:\/\//, ""),
    API_VERSION: ApiVersion.April22,
    IS_EMBEDDED_APP: true,
    // This should be replaced with your preferred storage strategy
    //SESSION_STORAGE: new Shopify.Session.MemorySessionStorage(),
    SESSION_STORAGE: sessionStorage
});

 

 

┃ Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/Users/robertvilbert/banner/node_modules/@shopify/shopify-api/dist/auth/session' is not supported resolving ES modules imported from /Users/robertvilbert/banner/server/index.js
┃ Did you mean to import @Shopify/shopify-api/dist/auth/session/index.js?
┃ at new NodeError (node:internal/errors:371:5)
┃ at finalizeResolution (node:internal/modules/esm/resolve:414:17)
┃ at moduleResolve (node:internal/modules/esm/resolve:983:10)
┃ at defaultResolve (node:internal/modules/esm/resolve:1080:11)
┃ at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
┃ at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
┃ at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:79:40)
┃ at link (node:internal/modules/esm/module_job:78:36) {
┃ code: 'ERR_UNSUPPORTED_DIR_IMPORT',
┃ url: 'file:///Users/robertvilbert/banner/node_modules/@shopify/shopify-api/dist/auth/session'

 

 

shopify cli version:

2.15.2

Node version

v16.14.2

 



Could you please help me on this. 

Replies 0 (0)