Reputation: 1798
I am trying to use the node module shopify-express: https://github.com/Shopify/shopify-express using a custom strategy to store the shop tokens because the sqlstrategy doesnt work with postgres due to bad SQL statement (already have issue on github).
In the documentation they say I need to have a class as so:
class Strategy {
// shop refers to the shop's domain name
getShop({ shop }): Promise<{accessToken: string}>
// shop refers to the shop's domain name
storeShop({ shop, accessToken }): Promise<{accessToken: string}>
}
I made the following class:
class Strategy {
// shop refers to the shop's domain name
// Promise<{accessToken: string}>
getShop(shopInfo) {
const { shop } = shopInfo;
console.log('get shop info', shopInfo)
console.log('check domain', shop)
return db.Shop.find({
where: {
shopify_domain: shop
}
})
.then(foundShop => {
return { accessToken: foundShop.access_token }
})
}
// shop refers to the shop's domain name
// Promise<{accessToken: string}>
storeShop(shopInfo) {
const { shop, accessToken } = shopInfo;
console.log('storing store', 'shop variable', shop, 'access token variable', accessToken);
return db.Shop.find({
where: {
shopify_domain: shop
}
})
.then(foundShop => {
if (foundShop) {
foundShop.access_token = accessToken
return foundShop.save()
.then(foundShop => {
return { accessToken: foundShop.access_token }
})
} else {
return db.Shop.create({ shopify_domain: shop, access_token: accessToken }).then(newShop => {
return { accessToken: newShop.access_token }
})
}
})
}
}
What is happening is it is inserting correctly but freezing. I believe this is because I am not returning a promise? I am still getting my head around them so not sure if I am doing it correctly or not.
IF IT HELPS: this is the shopInfo variable on the storeShop method:
{
accessToken: 'f444ca7789e9bcfb0fb2dc6026a79ece',
shop: 'jldesigndevstore.myshopify.com'
}
(err, token) => {
if (err) {
console.error('🔴 Error storing shop access token', err);
}
if (request.session) {
request.session.accessToken = accessToken;
request.session.shop = shop;
} else {
console.warn('Session not present on request, please install a session middleware.');
}
afterAuth(request, response);
}
Upvotes: 1
Views: 131
Reputation: 1798
So turns out the documentation was incorrect. Went into the node_module and saw the Memory strategy was different. This is the Class that worked using Sequelize:
class SequelizeStrategy {
async getShop({ shop }) {
let foundShop = await db.Shop.find({
where: {
shopify_domain: shop
}
})
.catch(err => {
console.log('get shop error', err)
})
return done(null, {accessToken: foundShop.access_token});
}
async storeShop({ shop, accessToken }, done) {
let storedShop = await db.Shop.find({
where: {
shopify_domain: shop
}
})
.then(foundShop => {
if (foundShop) {
foundShop.access_token = accessToken
foundShop.save()
return foundShop;
} else {
return db.Shop.create({ shopify_domain: shop, access_token: accessToken })
}
})
.catch(err => {
console.log('store shop error happened', err);
return done(err);
})
return done(null, accessToken);
}
}
Upvotes: 2