CustomSessionStorage from database? I get a different id....

Solved
rodrigograca31
Tourist
4 2 0

Hi.
Im trying to make a custom session storage so that I can actually deploy my app and have "thousands" of stores using it....
For that I cant use memorry session storage right? or whenever it restarts it would break.......

So Im following this tutorial on how to save and retrieve the session from a db:


My current problem is that my `loadCallback` function isnt able to retrieve the session back from the DB because the provided `id` changes...
lets say I have `store.com`... it saves that with all the other fields. but when its time to retrieve it the id will be `store.com` and then suddenly `store.com_123456789` 

It adds for example `_77896089824` and it makes no sense!
Top be fair in that video the same happens and its saved correctly to the DB.... but for some reason doesnt happen for me....

This is my current code:
As you see now Im trying the `db.raw` approach to make it look more like the video.... (had to change "ON DUPLICATE" to "ON CONFLICT" tho)
but my other approach should work.... (tries to find a given store by its shop name and then if it finds it it updates it otherwise inserts)

 

 

const db = require("./db_config.js");

const { Session } = require("@shopify/shopify-api/dist/auth/session");

let domain_id = "";

async function storeCallback(session) {
	console.log("storeCall", session);

	try {
		let data = session;

		if (data.id.indexOf(`${data.shop}`) > -1) {
			domain_id = data.id;
		}

		// await db("shopify_sessions")
		// 	.where({ shop: data.shop })
		// 	.then(async (shop) => {
		// 		if (shop.length > 0) {
		// 			console.log("YES");
		// 			await db("shopify_sessions")
		// 				.where({ shop: data.shop })
		// 				.update({
		// 					access_token: data.accessToken,
		// 					state: data.state,
		// 					session_id: data.id,
		// 					domain_id: domain_id,
		// 					scope: data.scope,
		// 				});
		// 			return true;
		// 		} else {
		// 			console.log("NO");
		// 			await db("shopify_sessions").insert({
		// 				shop: data.shop,
		// 				session_id: data.id,
		// 				domain_id: domain_id,
		// 				access_token: data.accessToken,
		// 				state: data.state,
		// 				isOnline: data.isOnline,
		// 				scope: data.scope,
		// 			});
		// 			return true;
		// 		}
		// 	})
		// 	.catch((err) => {
		// 		console.log(err);
		// 	});

		const abc = await db.raw(
			`INSERT INTO shopify_sessions (shop, session_id, domain_id, access_token, state, isOnline, scope) VALUES ('${data.shop}','${data.id}','${domain_id}','${data.accessToken}','${data.state}',${data.isOnline},'${data.scope}') ON CONFLICT (shop) DO UPDATE SET access_token='${data.accessToken}', state='${data.state}', session_id='${data.id}',domain_id='${domain_id}',scope='${data.scope}'`
		);
		console.log("ABC");
		console.log(abc);

		return true;
	} catch (e) {
		console.error(e);
	}
}
async function loadCallback(id) {
	console.log("loadCallback", id);
	if (id === undefined) {
		return false;
	}

	try {
		let data = await db("shopify_sessions")
			.where({ session_id: id })
			.orWhere({ domain_id: id });

		if (data.length > 0) {
			let session = new Session(id);

			session.shop = data[0].shop;
			session.state = data[0].state;
			session.scope = data[0].scope;
			session.isOnline = !!data[0].isOnline;
			session.accessToken = data[0].access_token;

			// session.id =
			const date = new Date();
			date.setDate(date.getDate() + 1);
			session.expires = date;

			if (session.expires && typeof session.exipres === "string") {
				session.expires = new Date(session.expires);
			}
			return session;
		} else {
			console.log("not found");
			return false;
		}

		// let data2 = await db("shopify_sessions")
		// 	.where({ session_id: id })
		// 	.orWhere({ domain_id: id })
		// 	.toSQL()
		// 	.toNative();

		// console.log("SQL ");
		// console.log(data2);
		console.log(data);
		return data;
	} catch (e) {
		console.error(e);
	}
}
async function deleteCallback(id) {
	try {
		return false;
	} catch (e) {
		console.error(e);
	}
}

module.exports = {
	storeCallback,
	loadCallback,
	deleteCallback,
};

 

 


My acessMod is offline...

 

 

createShopifyAuth({
			accessMode: "offline",

 

 

so I dont have this object property `onlineAccessInfo`

My final question is: how am I supposed to identify and retrieve the correct session of it adds "_NUMBER" to the id...? 
I thought about doing a `.split("_")[0]` but thats hacky and domains can actually contain underscores and that could cause issues.....



Accepted Solution (1)

Accepted Solutions
rodrigograca31
Tourist
4 2 0

This is an accepted solution.

I fixed it by setting the session loading as offline....... (because my sessions are `accessMode: "offline",` in order to save the accessToken to use later in webhooks)
on my `server.js` I added `false`

const session = await Shopify.Utils.loadCurrentSession(
			ctx.req,
			ctx.res,
			false
		);

I also found out there was a function called `loadOfflineSession` which can also be used but it requires `shop` to be passed and I couldn't find `shop` anywhere in `ctx` ....

View solution in original post

Reply 1 (1)
rodrigograca31
Tourist
4 2 0

This is an accepted solution.

I fixed it by setting the session loading as offline....... (because my sessions are `accessMode: "offline",` in order to save the accessToken to use later in webhooks)
on my `server.js` I added `false`

const session = await Shopify.Utils.loadCurrentSession(
			ctx.req,
			ctx.res,
			false
		);

I also found out there was a function called `loadOfflineSession` which can also be used but it requires `shop` to be passed and I couldn't find `shop` anywhere in `ctx` ....

View solution in original post